Integrations

Webhook Signing and Delivery

Verify webhook requests and understand how failed deliveries are retried.

#Webhook signing

By default when add a new webhook, the system will provide you with a secret key. This key is used to create a signature for the payload sent over from our API.

We recommend that you validate the requests sent by the webhook to make sure that the data came from us and wasn't tamped with.

You can find the signature for the payload under a header called tracksim-signature, this signature is a hash generated by the payload.

If you use Python, the official `TrackSim` package can verify it for you:

py
from tracksim import verify_webhook_signature

if not verify_webhook_signature(request.get_data(), request.headers.get("tracksim-signature"), secret):
    abort(401)

In JavaScript or TypeScript, the official `@tracksim/sdk` package does the same. Pass the raw request body, not re-serialised JSON:

js
import { verifyWebhookSignature, SIGNATURE_HEADER } from "@tracksim/sdk";

if (!(await verifyWebhookSignature(rawBody, req.get(SIGNATURE_HEADER), secret))) {
  return res.sendStatus(401);
}

In PHP, use the official `tracksim/tracksim-php` package. It also ships a Laravel webhook controller:

php
use TrackSim\Webhook;

if (! Webhook::verifySignature(file_get_contents('php://input'), $_SERVER['HTTP_TRACKSIM_SIGNATURE'] ?? null, $secret)) {
    http_response_code(401);
    exit;
}

Below is an example in Python on how we generate our signatures.

py
import json, hmac, hashlib

data = json.dumps(request.data)
secret = "<webhook_secret>"
signature = hmac.new(secret.encode(), msg=data.encode(), digestmod=hashlib.sha256).hexdigest()

Using the following python you can verify the signature created matches:

py
import json, hmac, hashlib

data = json.dumps(request.data)
secret = "<webhook_secret>"
signature = hmac.new(secret.encode(), msg=data.encode(), digestmod=hashlib.sha256).hexdigest()

if signature == request.headers['tracksim-signature']:
  print("The Signature matches")

request.data is the payload of JSON received when by the POST method request.

secret is the key generated when connecting an settings page

hmac.new is the python function which generates the hash signature found in header tracksim-signature.

#Webhook retry system

Our system is designed to retry sending webhooks up to 3 times if the webhook fails to send.

After each try the delay is increased before trying again:

  • first attempt: 5 minute delay
  • second attempt: 10 minute delay
  • third and final attempt: 20 minute delay

Our the third attempt our system won't try again automatically. You can manually resend the webhook via the portal if you wish.

We will consider all HTTP status codes in the 200 range as a successful attempt. If you return any other status code, we will mark the webhook as failed and try again later.

You can see the history of all webhooks sent in your webhook log on the settings game.