Try it

Webhook signature (HMAC)

Sortyra team · · 2 min read

Anyone who knows a webhook address can call it. An HMAC signature therefore adds a check code to every message, calculated from the message content and a secret key. The receiving system recalculates it and processes the message only if the codes match.

What is a webhook signature

A webhook is a message one system sends to another when an event happens – for example a new order from a marketplace. So that the recipient knows the message is genuine, the sender signs it:

  1. The sender and the recipient share a secret signing key.
  2. From the message content and the key, the sender calculates an HMAC (Hash-based Message Authentication Code), most often with the SHA-256 algorithm.
  3. It sends the signature in a header together with the message.
  4. The recipient calculates the signature again from the received message and its own key and compares it with the header.

If the signatures differ, someone has forged or altered the message – and it is not processed.

Why a timestamp

A timestamp (the time of sending) is added to the signature. The string that is signed is "timestamp.body". This lets the recipient reject old messages that someone might have intercepted and sent again (a replay attack) – for example anything older than 5 minutes.

How to verify the signature

Messages from Sortyra come with:

  • the x-sortyra-timestamp header – the timestamp in seconds,
  • the x-sortyra-signature header – the signature in the form sha256=<hex>.

How to verify:

  1. Take the raw body of the message exactly as it arrived (before any JSON processing).
  2. Join it into the string timestamp.body.
  3. Calculate HMAC-SHA256 with the signing key and write the result in hexadecimal with the prefix sha256=.
  4. Compare it with the header using a constant-time comparison function.
  5. Check that the timestamp is not too old.

What to watch out for

  • The key is secret. Do not store it in public code or send it by email.
  • Sign the raw body. If you parse the JSON and serialise it again, the order or whitespace changes and the signature will not match.
  • Use https only.

How Sortyra helps

Sortyra signs alert webhooks as well as marketplace orders sent to the Shop URL for orders. The signing key is shown only once, when it is created – pass it on to whoever manages your shop. See Marketplaces: connection, offers, orders and Alerts.

Frequently asked questions

What if I lose the signing key?

The key is shown only once and we cannot show it again. For alert webhooks, add a new channel, which gets a new key; for marketplace orders, write to us via the contact page.

Isn't a secret webhook address enough?

No. The address can turn up in logs or in a browser. The signature verifies the content of every message and also protects it against tampering.

Try it on your own feed