Skip to main content
GET
/
api
/
subscriptions
/
{id}
/
events
Eventos da Assinatura
curl --request GET \
  --url https://garu.com.br/api/subscriptions/{id}/events \
  --header 'Authorization: Bearer <token>'
{
  "id": 123,
  "subscriptionId": 123,
  "eventType": "<string>",
  "eventData": {},
  "createdAt": "<string>"
}

Visão Geral

Consulte o histórico completo de eventos de uma assinatura, incluindo pagamentos, mudanças de status, períodos de teste e cancelamentos.

Exemplo de Requisição

curl -X GET https://garu.com.br/api/subscriptions/100/events \
  -H "Authorization: Bearer sk_test_sua_chave"

Resposta de Sucesso (200 OK)

[
  {
    "id": 789,
    "subscriptionId": 100,
    "eventType": "payment_success",
    "eventData": {
      "amount": 4990,
      "transactionId": "tx_abc123",
      "paidAt": "2024-02-15T10:00:00.000Z"
    },
    "createdAt": "2024-02-15T10:00:00.000Z"
  },
  {
    "id": 788,
    "subscriptionId": 100,
    "eventType": "subscription.activated",
    "eventData": {},
    "createdAt": "2024-01-15T10:30:00.000Z"
  },
  {
    "id": 787,
    "subscriptionId": 100,
    "eventType": "subscription.created",
    "eventData": {
      "priceId": 456,
      "trialDays": 7
    },
    "createdAt": "2024-01-08T10:30:00.000Z"
  }
]

Tipos de Evento

TipoDescrição
subscription.createdAssinatura criada
subscription.activatedAssinatura ativada após primeiro pagamento
trial_startedPeríodo de teste iniciado
trial_endedPeríodo de teste encerrado
payment_successPagamento recorrente bem-sucedido
payment_failedTentativa de pagamento falhou
subscription.pausedAssinatura pausada
subscription.resumedAssinatura retomada após pausa
subscription.canceledAssinatura cancelada
subscription.expiredAssinatura expirou

Campos do Evento

id
number
ID único do evento.
subscriptionId
number
ID da assinatura relacionada.
eventType
string
Tipo do evento (veja tabela acima).
eventData
object
Dados adicionais específicos do evento.
createdAt
string
Data e hora do evento (ISO 8601).

Exemplo: Construindo um Billing History

async function getBillingHistory(subscriptionId) {
  const response = await fetch(
    `https://garu.com.br/api/subscriptions/${subscriptionId}/events`,
    {
      headers: {
        'Authorization': `Bearer ${process.env.GARU_API_KEY}`
      }
    }
  );

  const events = await response.json();

  // Filtrar apenas eventos de pagamento
  const payments = events.filter(e =>
    e.eventType === 'payment_success' || e.eventType === 'payment_failed'
  );

  return payments.map(payment => ({
    date: payment.createdAt,
    status: payment.eventType === 'payment_success' ? 'Pago' : 'Falhou',
    amount: payment.eventData.amount,
    transactionId: payment.eventData.transactionId
  }));
}

// Uso
const history = await getBillingHistory(100);
console.log('Histórico de Pagamentos:');
history.forEach(p => {
  console.log(`${p.date}: R$ ${p.amount.toFixed(2)} - ${p.status}`);
});

Próximos Passos

Detalhes da Assinatura

Consulte os detalhes completos da assinatura

Webhooks

Receba eventos em tempo real