Skip to main content

Signed Webhooks

Sign requests to a mapped webhook with HMAC-SHA-256, so the bot accepts them and posts the message to Discord.

Why Use Signed Webhooks?

  1. Proves that each request came from a service holding the signing secret, so nobody else can post through your route.
  2. Rejects requests whose timestamp is more than five minutes away from the server's clock, so a captured request can't be sent again later.
  3. Queues each accepted message in the database, so the worker still posts it after a busy moment or a restart.

Requirements

Endpoint and Headers

Send a POST request to the exact path you configured, with the raw JSON body and two headers.

http
POST /webhooks/price-alert HTTP/1.1
Host: bot.example.com
Content-Type: application/json
X-Stonker-Timestamp: 1792684800
X-Stonker-Signature: sha256=15d6b9d6363377cdc90373d41faab297a3d14be396656dd3431cda8b0f748e1f

{"alert":{"symbol":"TEST","price":123.45}}
  • X-Stonker-Timestamp — The current Unix time in seconds, as 10 digits.
  • X-Stonker-Signature — The hex HMAC-SHA-256 of the timestamp, a period, and the raw body, with an optional sha256= prefix and either letter case.
  • Body — A JSON object of up to 128 KiB, sent exactly as signed.

Compute the Signature

Sign the string <timestamp>.<raw body> with the signing secret, using the same bytes you send as the body.

To check your code, sign the body {"alert":{"symbol":"TEST","price":123.45}} with the timestamp 1792684800 and the secret a-webhook-signing-secret-with-at-least-32-characters. The result is 15d6b9d6363377cdc90373d41faab297a3d14be396656dd3431cda8b0f748e1f, the signature shown above.

Map JSON Fields to Variables

Each webhook variable has an ID, a path into the JSON body, and a type, and the message uses it as @ID@.

  • ID — Uppercase letters and digits, starting with a letter, with single underscores between parts, such as SYMBOL or PRICE_2.
  • Path — Up to 10 dot-separated keys, such as alert.symbol, and arrays aren't supported.
  • Missing values — A missing field, or a value of the wrong kind, shows as "Unavailable".
TypeAcceptsShows
stringText, a number, or true/falseThe value as text, with Markdown characters escaped, up to 500 characters
booleantrue or false"Yes" or "No"
ts-secondsA Unix time in secondsA Discord timestamp in each reader's time zone
ts-millisA Unix time in millisecondsA Discord timestamp in each reader's time zone
usd-dollarsA number of dollarsA US dollar amount, such as $123.45
usd-centsA number of centsA US dollar amount, such as $123.45 for 12345

The message is the route's content, then its footer on a second line, with %YEAR% filled in as the current year in UTC. After the variables are filled in, it has to be 1 to 2,000 characters.

Responses

StatusBodyMeaning
202{"accepted":true,"duplicate":false}The message was queued.
200{"accepted":true,"duplicate":true}The same request was already queued.
400"Webhook body must be valid JSON." or "Webhook body must be a JSON object."The body isn't a JSON object.
401"Invalid or expired webhook signature."The signature, the timestamp, or the secret doesn't match.
404"Not found."No route matches the path, the feature is off, or its settings fail their checks, such as a missing signing secret.
405"Method not allowed."The request wasn't a POST.
503"Discord delivery is not available while the bot is stopped or unconfigured."The bot is stopped or has no Bot Token.
413{"error":{"code":"payload_too_large","message":"The request body is too large."}}The body is over 128 KiB.
415{"error":{"code":"unsupported_encoding","message":"Send the request body as UTF-8 JSON."}}The request used a Content-Encoding the web service can't read.
422"Rendered webhook content must contain between 1 and 2000 characters."The message is empty or over 2,000 characters after the variables are filled in.

Replay Protection

Troubleshooting

  • HTTP 401 — Check that the server's clock is correct, that the timestamp is in seconds, and that the body wasn't reformatted after you signed it.
  • HTTP 404 on a configured path — The path has to match exactly, including a trailing slash, and the Web applications feature has to be on with settings that pass their checks.
  • HTTP 503 — Start the bot, or save a Bot Token.
  • "Unavailable" in the message — A variable's path or type doesn't match the JSON you sent.