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:

HeaderValue
X-Gophr-TimestampThe time we signed the webhook, in Unix seconds, e.g. 1764230400
X-Gophr-SignatureThe signature, prefixed by the signature scheme, e.g. v1=d2de7f926421291731f86fd63003b1e8220245c718335e89989a6a2e44c20363
X-Gophr-Config-Public-ReferenceThe 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

  1. Take the X-Gophr-Timestamp value, a full stop (.) and the raw request body, exactly as received: {timestamp}.{body}.
  2. Compute an HMAC-SHA256 of that string, using the whole webhook secret (including the whsec_ prefix) as the key.
  3. 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

  1. Read X-Gophr-Config-Public-Reference and 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.
  2. Read X-Gophr-Timestamp and reject the webhook if it is more than 5 minutes away from your current time. This stops an old webhook being replayed.
  3. 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.
  4. Compare your signature with the v1= value in X-Gophr-Signature using a constant-time comparison, such as crypto.timingSafeEqual in Node.js or hash_equals in PHP. Reject the webhook if they don't match.
  5. Respond with a 2xx status 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_id and update your copy, so processing a repeat does no harm. Don't drop a webhook just because you've seen its status before: 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 its delivery_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).

InputValue
Secretwhsec_0000000000000000000000000000000000000000000000000000000000000000
Timestamp1764230400
Body{"delivery_id":"74afea12-24aa-44c7-a55b-b2dc2a9a02d3","status":"DELIVERED"}
Signaturev1=d2de7f926421291731f86fd63003b1e8220245c718335e89989a6a2e44c20363

You can also send real signed webhooks from the sandbox with the progress endpoint.


Did this page help you?