Connect API
Getting Started

Best Practices

How Spoke delivers webhooks (at least once, unordered, with retries) and how to write a handler that de-duplicates events and reads the order's latest state.

API Versions

All the events sent by Spoke are versioned. The version defines how the body of the message you receive should be handled, and what fields it contains.

Every event carries version: "v1", the current version of the Connect API. Connect has no version setting for webhooks.

Spoke will not bump the version for every type of change, see API Versioning for more details on this.

Event types

Read only the fields your logic needs, and ignore the rest. Spoke adds fields to the Order model and values to its enums without changing the version, and data gets them at the same time. Treat an enum value you do not recognize as unknown rather than as an error.

You also should ignore any events not important for your use case. New event types appear in Connect as new options under Select the events to receive, and Spoke sends one only after you select it. If a type arrives that you do not handle, answer 2xx and skip it, so Spoke does not retry it.

To check what event types Spoke sends, take a look at the Webhook Events section.

Delivery attempts

If your endpoint is down, does not answer within 30 seconds, or does not answer with a 2xx code, Spoke will keep retrying the delivery of the message for around an hour. The retries have an exponential backoff independent for each message.

After the 1-hour period is over, Spoke will stop trying to deliver the message and discard it. If your endpoint was down for longer than that, read the orders with GET /orders to catch up. See List orders and follow their progress.

Answer with a 2xx as soon as you have stored the event, and process it afterwards. A slow endpoint can time out, which Spoke treats as a failure and retries, so the same event arrives again.

Monitor your endpoint yourself. Spoke does not turn off a failing endpoint: new events keep going to it, and each goes through the same retries.

Changes that happen while webhooks are off, or while an event is not selected, are never sent afterwards.

Handling the Events

Due to the distributed nature of our internal systems, there may be some edge cases happening.

Out Of Order Delivery

The delivery of the messages to your endpoint may come out of order from the actual order the events themselves occurred.

data is the order as Spoke read it when it queued the event, usually seconds after the change. The event type says what happened, but data can already show a later step. For example, an order added to a route that starts a moment later can get an order.scheduled event whose data already shows progress.routeStarted as true.

A retry sends the same data as the first attempt, even when it comes up to an hour later, so a retried event can show an older state than a later event you already handled. Applying its data can move your copy backwards. When that matters, compare the order's progress against what you have stored, or read the order with GET /orders/{id}.

Duplicate Delivery

Some messages may be delivered more than once, for example when Spoke retries after a failure or republishes after an internal error. We recommend your systems handle the received messages in an idempotent manner.

created is when Spoke recorded the change on the order, in milliseconds since epoch, and it stays the same on every retry. Two deliveries with the same order id (data.id), type, and created are the same event. Store those three values for the events you handled. When one arrives again, skip the work and answer 2xx:

Skip a duplicate event
const eventKey = `${event.data.id}:${event.type}:${event.created}`

if (await alreadyHandled(eventKey)) {
  return response.sendStatus(200)
}

Two order.attempted_delivery events for the same order with different created values are two attempts.

Some changes also send events again, with a new created:

  • One change to an order can send several events at once. For example, an order added to a route that has already started gets order.scheduled, order.out_for_delivery, and order.tracking_link_added together.
  • If the courier takes an order off its route and later puts it on a started route, Spoke sends order.out_for_delivery and order.tracking_link_added again. If the order was moved to unassigned in between, Spoke also sends order.scheduled again.
  • When you accept a courier's invitation, Spoke adds the orders the courier had already planned for you. Each one sends the events its current state matches, all at once, even if its route started or its delivery was attempted before you joined. Their created time is when the orders were added, not when the change happened.

Secure connections

Spoke will outright refuse any non-encrypted connection. Make sure HTTPS is enabled on your endpoint with a certificate from a public certificate authority. Spoke rejects self-signed certificates, and URLs on private or internal addresses.

Redirects and other HTTP statuses

Spoke will consider any status different from 2xx an error. This also means Spoke will not follow any redirects, considering a redirect status an error, and will keep retrying until the status is 2xx or the retry window expires.

Was this page helpful?