Webhook Signature Verification
Check that a webhook really came from Gophr and was not changed on the way.
Every webhook we send is signed. Each webhook carries a timestamp and a signature that you check with your integration's webhook signing secret. Your API key is never sent to your webhook URL.
Your webhook secret
Every API integration has one webhook signing secret, which looks like whsec_ followed by 64 hexadecimal characters. We show it once, when you create the integration in the web app (if our team set the integration up for you, they'll send it to you). Store it somewhere safe, such as a secrets manager, and never put it in client-side code or logs. The secret can't be changed or shown again. If you lose it, create a new API integration (which gets a new secret), create new keys on it and book new jobs with those. Webhooks for jobs already booked on the old integration are still signed with the old secret, so check those jobs with GET /jobs/{job_id} instead.
All API keys in the same integration share the integration's webhook signing secret.
Signature headers
On top of the headers sent with every webhook, a signed webhook has:
| Header | Value |
|---|---|
X-Gophr-Timestamp | The time we signed the webhook, in Unix seconds, e.g. 1764230400 |
X-Gophr-Signature | The signature, prefixed by the signature scheme, e.g. v1=d2de7f926421291731f86fd63003b1e8220245c718335e89989a6a2e44c20363 |
X-Gophr-Config-Public-Reference | The public reference of the API integration that sent the webhook |
There is no API-KEY header.
HTTP header names are case-insensitive, so read them without depending on their case.
How the signature is made
- Take the
X-Gophr-Timestampvalue, a full stop (.) and the raw request body, exactly as received:{timestamp}.{body}. - Compute an HMAC-SHA256 of that string, using the whole webhook secret (including the
whsec_prefix) as the key. - Write the result as lowercase hexadecimal and put
v1=in front of it.
v1 is the signature scheme, not the API version. If you ever see a scheme you don't recognise, ignore it.
Verifying a webhook
- Read
X-Gophr-Config-Public-Referenceand look up the webhook secret you stored for that API integration. This matters if several integrations send webhooks to the same URL. If you don't recognise the reference, reject the webhook. - Read
X-Gophr-Timestampand reject the webhook if it is more than 5 minutes away from your current time. This stops an old webhook being replayed. - Compute the expected signature as above, using the raw body. Don't parse the JSON and re-encode it first: any change to spacing or key order changes the signature.
- Compare your signature with the
v1=value inX-Gophr-Signatureusing a constant-time comparison, such ascrypto.timingSafeEqualin Node.js orhash_equalsin PHP. Reject the webhook if they don't match. - Respond with a
2xxstatus code once the webhook is accepted.
Reject a webhook by responding with a 4xx status code, and don't act on its contents.
Handling duplicates
A webhook can occasionally arrive more than once, and a repeated webhook can have a new timestamp and signature. We don't retry webhooks your endpoint rejects (see Delivery and retries), so only reject a webhook when its signature or timestamp fails the checks above.
- Status update webhooks: treat each one as the latest state of its
delivery_idand update your copy, so processing a repeat does no harm. Don't drop a webhook just because you've seen itsstatusbefore: a delivery can go back to an earlier status, for example when a courier is unassigned and another accepts, and the later webhook carries the new courier's details. - ETA update webhooks: these are sent repeatedly for the same delivery and have no
status, so don't deduplicate them. Treat each one as the latest ETA and courier location for itsdelivery_id. - Booking and job update webhooks: handle them so that processing the same one twice does no harm, for example by updating your copy of the job rather than adding a new record.
X-Gophr-Event is not part of the signature. To decide what a webhook means, use the signed body.
Examples
Node.js (Express)
const crypto = require('crypto');
const express = require('express');
const app = express();
const TOLERANCE_SECONDS = 5 * 60;
// Look up the secret you stored for this API integration.
function findWebhookSecret(configReference) {
return process.env[`GOPHR_WEBHOOK_SECRET_${configReference}`];
}
app.post('/gophr/webhooks', express.raw({ type: 'application/json' }), (req, res) => {
const secret = findWebhookSecret(req.get('X-Gophr-Config-Public-Reference'));
const timestamp = req.get('X-Gophr-Timestamp');
const signatureHeader = req.get('X-Gophr-Signature') || '';
if (!secret || !/^\d+$/.test(timestamp || '')) {
return res.sendStatus(400);
}
if (Math.abs(Math.floor(Date.now() / 1000) - Number(timestamp)) > TOLERANCE_SECONDS) {
return res.sendStatus(400);
}
const expected = crypto
.createHmac('sha256', secret)
.update(`${timestamp}.${req.body.toString('utf8')}`)
.digest('hex');
const received = signatureHeader
.split(',')
.map((part) => part.trim())
.find((part) => part.startsWith('v1='))
?.slice(3) || '';
const expectedBuffer = Buffer.from(expected, 'utf8');
const receivedBuffer = Buffer.from(received, 'utf8');
if (expectedBuffer.length !== receivedBuffer.length || !crypto.timingSafeEqual(expectedBuffer, receivedBuffer)) {
return res.sendStatus(400);
}
const payload = JSON.parse(req.body.toString('utf8'));
// Process the webhook. For status updates, skip any delivery_id + status you have already handled.
return res.sendStatus(200);
});PHP
<?php
const TOLERANCE_SECONDS = 300;
$body = file_get_contents('php://input');
$timestamp = $_SERVER['HTTP_X_GOPHR_TIMESTAMP'] ?? '';
$signatureHeader = $_SERVER['HTTP_X_GOPHR_SIGNATURE'] ?? '';
$configReference = $_SERVER['HTTP_X_GOPHR_CONFIG_PUBLIC_REFERENCE'] ?? '';
// Look up the secret you stored for this API integration.
$secret = findWebhookSecret($configReference);
if ($secret === null || ctype_digit($timestamp) === false) {
http_response_code(400);
exit;
}
if (abs(time() - (int) $timestamp) > TOLERANCE_SECONDS) {
http_response_code(400);
exit;
}
$expected = hash_hmac('sha256', $timestamp . '.' . $body, $secret);
$received = '';
foreach (explode(',', $signatureHeader) as $part) {
$part = trim($part);
if (str_starts_with($part, 'v1=')) {
$received = substr($part, 3);
}
}
if (hash_equals($expected, $received) === false) {
http_response_code(400);
exit;
}
$payload = json_decode($body, true);
// Process the webhook. For status updates, skip any delivery_id + status you have already handled.
http_response_code(200);Testing your verification
With the values below, your code should produce the signature shown. The timestamp is in the past, so skip the 5-minute check when testing with these values (the samples above would otherwise reject it).
| Input | Value |
|---|---|
| Secret | whsec_0000000000000000000000000000000000000000000000000000000000000000 |
| Timestamp | 1764230400 |
| Body | {"delivery_id":"74afea12-24aa-44c7-a55b-b2dc2a9a02d3","status":"DELIVERED"} |
| Signature | v1=d2de7f926421291731f86fd63003b1e8220245c718335e89989a6a2e44c20363 |
You can also send real signed webhooks from the sandbox with the progress endpoint.
Updated about 3 hours ago
