Guide

Catch up on missed events without gaps or repeats

Webhooks are push: the sender calls you, and if you are down, the call is your problem. A pull feed turns that into a question you can ask later: what happened since I last looked?

6 min read · Last checked

In short

Key points

  • Events are kept for 30 days. Within that window you can recover from a deploy, an outage or a firewall that blocks inbound calls.
  • Pull and push carry the same JSON body for an event, so one handler can serve both.
  • Store the cursor from each response after you have handled the page. Pass it as after next time.
  • A cursor older than 30 days stops working with 410 and cursor_expired. Start over with since.

The problem with push alone

A webhook sender typically retries for a while and then gives up. If your endpoint was down for longer than that, or you deploy in a way that drops requests, you have a hole in your record of what happened: a tenant created, a plan changed, a limit reached. You often learn about it from a customer.

There are two sound answers. One is to reconcile by reading current state, such as asking for a customer’s runtime context. The other is to read the event history itself. They complement each other: state tells you what is true now, events tell you what happened and in what order.

List events with a cursor

The runtime API lists the events for one application and environment. You authenticate with an organization API key that has the webhooks.manage scope, and the key must be allowed to address the environment you name. A key tied to one environment reads that environment only.

The first call has no cursor. Every response returns nextCursor and hasMore. Handle the page, store the cursor, and send it as after on the next call. When hasMore is true, call again straight away.

List events after a cursor (HTTP)
GET /api/v1/runtime/applications/{applicationId}/events?environment=Production&after={cursor}&types=tenant.created,tenant.deleted&limit=100
X-TenancyEngine-Api-Key: te_...

Ordered, complete, and a little late

Events come back in order, with no gaps and no repeats, even while new events arrive. Events from the last few seconds appear after a short delay of about three seconds. That delay is what stops a late-committing event from being skipped, so do not read it as lag to work around.

The cursor is opaque. Do not build one and do not edit one. If you need to start from a point in time rather than a cursor, use since with an ISO time on your first call.

Handle a page, then store the cursor

The order of those two steps is the whole safety property. Store the cursor only after the page is handled. If you crash in between, you read the page again, which is safe as long as your handler is idempotent. If you stored first and crashed, you would lose the page.

Make the handler idempotent by recording the event id. Each event carries a unique id, so a duplicate delivery, whether by push and pull together or by a replayed page, is recognised and skipped.

Catch-up loop (pseudocode)
cursor = load_cursor()            // null on the very first run
loop:
  page = list_events(after = cursor, limit = 100)
  for event in page.events:
    if not seen(event.id):          // idempotent on the event id
      handle(event)
      mark_seen(event.id)
  cursor = page.nextCursor
  save_cursor(cursor)               // only after the page is handled
  if not page.hasMore: break

When the cursor is too old

If you wait longer than 30 days between calls, the cursor stops working and you get 410 with the error cursor_expired. Start again without a cursor, or with since. Anything older than 30 days is gone from the feed, so for events you must keep longer, write them to your own store as you handle them.

Keep verifying everything

Pull does not replace the rest of your webhook hygiene. Authenticate with a key that has only the scope it needs, treat the event body as input to validate rather than as trusted, and filter what you act on to the tenant it belongs to. For push, keep verifying the signature on every request.

What the feed does not include

The feed lists events that belong to one tenant and events for one application. Organization-wide totals that belong to no single application, such as billing and usage roll-ups, are not in it. Receive those by webhook. An event with no tenant that belongs to one application is filed under Production.

Honest limits

What to know before you rely on this

  • Events are kept for 30 days. Older ones cannot be read.
  • Organization-wide events with no application, such as billing and usage roll-ups, are delivered by webhook only.
  • There is no published SDK method for the feed. It is plain HTTP, so any language can call it.

Questions people ask

Can I rely on pull instead of webhooks?

You can. It suits servers behind a firewall that blocks inbound calls, and you can use it alone. Using push for speed and pull to recover from downtime is a common pattern.

Will I see the same event twice?

Not from the feed itself, which returns events without repeats. You can still see a duplicate if you also receive the same event by webhook, so deduplicate on the event id.

Which key should I use?

An organization API key with the webhooks.manage scope that is allowed to address the environment you ask for. Prefer a key tied to one environment.

Sources

Keep reading

Try it on the free plan

One app and five customers, no credit card required.

Request access More guides