Skip to main content

Webhooks

Webhooks let Paxon tell your system when something happens, such as a shipment being shipped or an inbound being checked, so you don't have to poll the API. You register a URL for an event, and Paxon sends an HTTP POST request to that URL every time the event occurs.

How it works​

  1. Subscribe. Register a webhook with POST v3/webhooks: one webhook per event, with the targetUrl that should receive it. The available events are listed under Events.
  2. Receive. When the event occurs, Paxon sends a POST request with a JSON body to your targetUrl.
  3. Verify. Your endpoint checks the signature to confirm that the request comes from Paxon and was not changed on the way.
  4. Acknowledge. Your endpoint responds with a 2xx status code within 15 seconds.
  5. Process. Your system processes the event, preferably after it has responded.
POST v3/webhooks
{
"data": {
"type": "webhook",
"attributes": {
"event": "shipment.shipped.3",
"isActive": true,
"targetUrl": "https://example.com/webhooks/shipment/shipped"
}
}
}

Use PUT v3/webhooks/{id} with isActive: false to pause a webhook, and DELETE v3/webhooks/{id} to remove it. The targetUrl must be reachable from the internet over HTTPS.

The request​

Each delivery is an HTTP POST with Content-Type: application/json.

HeaderDescription
X-Event-IdUnique identifier (UUID) of the event. It is the same for every delivery attempt of that event, so use it to detect duplicates.
X-Hook0-SignatureSignature to verify that the request comes from Paxon. See Verifying the signature.

The body follows the same layout as the response of the corresponding GET endpoint: the resource is in data, and related resources are in included at the top level. Some events add attributes.timestamp, the date and time (UTC) at which the event occurred. The payload of each event is described under Events.

Delivery behaviour​

In short, the webhook engine works as follows:

  • At least once. Every event is delivered at least once, and sometimes more than once.
  • No guaranteed order. Events are sent as they occur, but they can arrive in a different order, especially after a retry.
  • Status code only. Only the HTTP status code of your response counts. Any 2xx is a success; everything else is a failure.
  • 15 seconds. A response that takes longer than 15 seconds counts as a failure.
  • Retries. A failed delivery is retried with increasing intervals, for up to 24 attempts or 8 days.
  • Resending. After the last attempt the event is not delivered again automatically. For some event types Paxon can resend events on request; for others this isn't possible.

Responding​

Paxon only looks at the HTTP status code of your response. The body is ignored.

Your responseResult
Any 2xx, for example 200 OK, 202 Accepted or 204 No ContentDelivered.
4xx, including 400, 401, 404 and 410Failed, retried.
429 Too Many RequestsFailed, retried. If you send a Retry-After header, the next attempt waits at least that long.
5xxFailed, retried.
No response within 15 secondsFailed, retried.
Connection, DNS or TLS errorFailed, retried.

A 4xx is not treated as a permanent rejection: the event is retried like any other failure. If you don't want an event any more, deactivate or delete the webhook instead of rejecting its deliveries.

tip

Respond first, process later. Store the event (for example in a queue or table), return 202 Accepted, and process it in the background. Slow processing is the most common cause of timeouts, and every timeout leads to the same event being delivered again.

Retries​

A failed delivery is retried with increasing intervals. The waiting time before each next attempt is:

After failureNext attempt after
13 seconds
210 seconds
33 minutes
430 minutes
51 hour
63 hours
75 hours
8 and later10 hours

A small random delay is added to each interval. Retries stop after 24 attempts or 8 days, whichever comes first. After that the event is not delivered again automatically.

note

Paxon can resend missed events on request for some event types, but not for all of them. Don't rely on resending: make sure your endpoint is available, and use the GET endpoints to catch up on the current state of orders, shipments and inbounds after an outage.

Because of retries, the same event can arrive more than once, and events can arrive in a different order than they occurred.

Duplicates and ordering​

  • Duplicates. Every attempt of the same event carries the same X-Event-Id. Store the ids you have processed and skip an event whose id you have already seen. Keep them for at least 8 days, the length of the retry window.
  • Ordering. Don't rely on the order in which requests arrive. Use the attributes.timestamp of the event, or the state of the resource itself, to decide which information is the most recent. When in doubt, retrieve the current state with the corresponding GET endpoint.

Verifying the signature​

The X-Hook0-Signature header has three parts, separated by commas:

X-Hook0-Signature: t=1765443663,h=content-type x-event-id x-event-type,v1=85da0586ae0b711d...
PartDescription
tUnix timestamp (seconds) at which the request was signed.
hSpace-separated names of the request headers that are included in the signature, in order.
v1HMAC-SHA256 signature, hex-encoded.

To verify a request:

  1. Split the header into t, h and v1.
  2. Take the values of the headers listed in h, in the same order, and join them with ..
  3. Build the signed string: {t}.{h}.{header values}.{raw request body}.
  4. Compute the HMAC-SHA256 of that string with the secret key of your webhook, and hex-encode it.
  5. Compare the result with v1 using a constant-time comparison.
  6. Reject the request if t is more than 5 minutes away from the current time, to prevent replays.

Always use the raw request body exactly as received. Parsing and re-serializing the JSON changes it, and the signature won't match.

Node.js (Express)
const crypto = require('crypto');
const express = require('express');

const app = express();
const secret = process.env.PAXON_WEBHOOK_SECRET;

app.post('/webhooks/shipment/shipped', express.raw({ type: 'application/json' }), (req, res) => {
const parts = Object.fromEntries(
req.get('X-Hook0-Signature').split(',').map((part) => part.split(/=(.*)/s).slice(0, 2))
);

const headerNames = parts.h.split(' ');
const headerValues = headerNames.map((name) => req.get(name) ?? '').join('.');
const signed = `${parts.t}.${parts.h}.${headerValues}.${req.body.toString('utf8')}`;
const expected = crypto.createHmac('sha256', secret).update(signed).digest('hex');

const valid =
expected.length === parts.v1.length &&
crypto.timingSafeEqual(Buffer.from(expected), Buffer.from(parts.v1));
const fresh = Math.abs(Date.now() / 1000 - Number(parts.t)) <= 300;

if (!valid || !fresh) {
return res.sendStatus(401);
}

// Store the event for processing, then respond quickly.
queue.push({ id: req.get('X-Event-Id'), body: JSON.parse(req.body) });
res.sendStatus(202);
});

Checklist​

  • Use an HTTPS targetUrl that is reachable from the internet.
  • Verify the signature of every request and reject requests older than 5 minutes.
  • Respond with a 2xx within 15 seconds, and process the event afterwards.
  • Detect duplicates with X-Event-Id.
  • Order events by their timestamp, not by their arrival.
  • Deactivate or delete webhooks you no longer need instead of rejecting their deliveries.