Verifying Requests
Storeep signs every request it sends to your server. Check the signature before you trust the body, so you know the request really comes from Storeep and was not changed or replayed.
What Is Signed
| Request | Signed with | Where you find the key |
|---|---|---|
| App webhooks | Your app's signing key | The Credentials tab of your app, in Settings, then App development. |
| UI requests | Your app's signing key | The same tab. One key signs both. |
| Store webhooks | The store webhook key | Settings, then Webhooks, in the store's dashboard. |
All three are checked the same way. Both keys look alike: whsec_ followed by 64 characters.
Headers
Each request is a POST with a JSON body and these headers:
| Header | Description |
|---|---|
Storeep-Timestamp | The Unix time in seconds when this attempt was signed. A retry is signed again with a new time. |
Storeep-Signature | The time and one or two signatures: t=1764331200,v1=5257a8... |
Storeep-Event-Id | App webhooks only. The event id, the same on every retry of one event. Store it and skip an id you already handled. |
Signature Format
Each v1 value is the hex HMAC-SHA256 of the text <timestamp>.<raw body>, keyed with your key. The key is the whole string, whsec_ prefix included. Always sign the raw bytes you received, never a body you parsed and encoded again.
t=1764331200,v1=5257a869e7ecebeda32affa62cdca3fa51cad7e77a0e56ff536d0ce8e108d8bdVerifying a Request
- Read the raw body
Keep the exact bytes of the request body before any JSON parsing.
- Split the header
Split
Storeep-Signatureon commas, then each part on its first=. Keeptand everyv1. - Check the time
Refuse the request when
tis more than 5 minutes away from your clock, so a captured request cannot be replayed later. - Compare the signatures
Compute the HMAC-SHA256 of
t + "." + bodywith your key, and compare it with eachv1in constant time. Accept the request when any of them matches. - Answer quickly
Answer a webhook with a
2xxstatus within 10 seconds, then do the slow work in the background.
The two handlers below receive an app webhook. The examples need PHP 8.4 or Node.js 18. queue_event() is your own function: it skips an event id it already stored, then hands the event to a background job. In the Node.js example it is called queueEvent().
PHP
<?php
# the signing key of your app, kept in the environment and never in the code
$signing_key = getenv('STOREEP_SIGNING_KEY');
# the raw body exactly as storeep signed it, read before any json parsing changes it
$body = file_get_contents('php://input');
# t=...,v1=...[,v1=...], missing when the request did not come from storeep
$header = @$_SERVER['HTTP_STOREEP_SIGNATURE'];
# a request without the header never came from storeep
if (is_string($header))
{
# the parts of the header: the signing time and one or two signatures
$parts = explode(separator: ',', string: $header);
# the signing time, null until the header names it
$timestamp = null;
# every v1 signature, there are two for 24 hours after a key rotation
$signatures = [];
# reads each part of the header
foreach ($parts as $part)
{
# the name and the value of one part, split on its first equals sign only
$pair = explode(separator: '=', string: $part, limit: 2);
# a part without an equals sign gets an empty value instead of a missing one
[$name, $value] = array_pad(array: $pair, length: 2, value: '');
# the signing time, in seconds
if ($name === 't')
{
# kept as a number so it can be compared with your clock
$timestamp = (int) $value;
}
# one signature, kept with the others since any of them may match
if ($name === 'v1') $signatures[] = $value;
}
# a header without a time is malformed
if ($timestamp !== null)
{
# how far the signing time is from your clock, in seconds
$age = abs(time() - $timestamp);
# a request older than 5 minutes may be a captured one replayed later
if ($age <= 300)
{
# what storeep signed: the time, a dot and the raw body
$signed = $timestamp . '.' . $body;
# the signature your key gives for that text
$expected = hash_hmac(algo: 'sha256', data: $signed, key: $signing_key);
# true when a received signature matches, compared in constant time so a refusal leaks nothing
$is_matching = fn (string $signature): bool => hash_equals(known_string: $expected, user_string: $signature);
# any v1 may match, there are two for 24 hours after a key rotation
if (array_any(array: $signatures, callback: $is_matching))
{
# the event: its id, type, store_id, created_at and data
$event = json_decode(json: $body, associative: true);
# your own function: skips an event id it already stored, then queues the slow work, storeep waits 10 seconds at most
queue_event($event);
# tells storeep the event arrived
http_response_code(200);
}
# a wrong signature means someone else sent it, or the key is not this app's
else http_response_code(401);
}
# a stale request is refused
else http_response_code(400);
}
# a malformed header is refused
else http_response_code(400);
}
# a request without a signature is refused
else http_response_code(400);Node.js
// the hmac and the constant time comparison that node ships with
import { createHmac, timingSafeEqual } from 'node:crypto';
// the web framework that receives the request
import express from 'express';
// the signing key of your app, kept in the environment and never in the code
const signingKey = process.env.STOREEP_SIGNING_KEY;
// the server that receives storeep's webhooks
const app = express();
// compares a received signature with the expected one in constant time, so a refusal leaks nothing
function isSameSignature (signature, expected)
{
// the received signature as bytes, the form timingSafeEqual compares
const received = Buffer.from(signature);
// a different length never matches, and checking it first keeps timingSafeEqual from throwing
return received.length === expected.length && timingSafeEqual(received, expected);
}
// checks the signature of one webhook, answers, then does the slow work
function receiveWebhook (request, response)
{
// t=...,v1=...[,v1=...], empty when the request did not come from storeep
const header = request.get('Storeep-Signature') ?? '';
// the signing time, null until the header names it
let timestamp = null;
// every v1 signature, there are two for 24 hours after a key rotation
const signatures = [];
// reads each part of the header
for (const part of header.split(','))
{
// where the name ends, the value after it is kept whole
const index = part.indexOf('=');
// the part's name, t or v1
const name = part.slice(0, index);
// the part's value
const value = part.slice(index + 1);
// the signing time, in seconds
if (name == 't')
{
// kept as a number so it can be compared with your clock
timestamp = Number(value);
}
// one signature, kept with the others since any of them may match
if (name == 'v1') signatures.push(value);
}
// how far the signing time is from your clock, in seconds
const age = Math.abs(Date.now() / 1000 - timestamp);
// a header without a time is malformed, and a request older than 5 minutes may be a captured one replayed later
if (timestamp === null || age > 300)
{
// tells storeep the request was refused
response.sendStatus(400);
// nothing below runs for a refused request
return;
}
// the time and the dot that come before the body
const prefix = Buffer.from(`${timestamp}.`);
// what storeep signed: the time, a dot and the raw body
const signed = Buffer.concat([prefix, request.body]);
// the signature your key gives for those bytes, as hex text
const expectedHex = createHmac('sha256', signingKey).update(signed).digest('hex');
// the expected signature as bytes, the form timingSafeEqual compares
const expected = Buffer.from(expectedHex);
// any v1 may match, there are two for 24 hours after a key rotation
const isValid = signatures.some((signature) => isSameSignature(signature, expected));
// a wrong signature means someone else sent it, or the key is not this app's
if (!isValid)
{
// tells storeep the request was refused
response.sendStatus(401);
// nothing below runs for a refused request
return;
}
// the event: its id, type, store_id, created_at and data
const event = JSON.parse(request.body);
// answers first, storeep waits 10 seconds at most
response.sendStatus(200);
// your own function: skips an event id it already stored, then does the slow work
queueEvent(event);
}
// express.raw keeps the body as the exact bytes storeep signed, json parsing would change them
app.post('/storeep/webhooks', express.raw({ type: 'application/json' }), receiveWebhook);
// listens for storeep's requests
app.listen(3000);A UI request is checked the same way, then answered with a page instead of a bare 200. UI Extensions has that handler in full.
When a Key Is Rotated
- For 24 hours after a rotation,
Storeep-Signaturecarries twov1values: the first made with the new key, the second with the previous one. - Accept the request when any
v1matches, as the examples do. Your server then keeps working with the old key until you deploy the new one. - After 24 hours the header carries one
v1, made with the new key. - For an app, the overlap ends at once when you click Stop the previous secrets now on the Credentials tab.
- The session token of a UI request is signed with the new signing key at once, with no overlap. Deploy the new key right after you rotate.
- A store webhook key rotates the same way, with Rotate key in Settings, then Webhooks.
Store Webhooks
A webhook a merchant adds in Settings, then Webhooks, or through the Store Webhooks API, carries Storeep-Timestamp and Storeep-Signature, signed with the store webhook key. It has no Storeep-Event-Id. Its address is the one the merchant typed, for example https://example.com/webhooks/orders.
- Only a
2xxanswer counts as delivered. Redirects are not followed. - Storeep waits 10 seconds for the answer.
- A
5xx, a429, a timeout or a refused connection is retried 5 times: after 30 seconds, then 1, 2, 4 and 8 minutes. - Any other status is a final failure and is not retried.
- After 5 failed deliveries in a row, the store owner gets an email naming the webhook. One success sets the count back to zero.
- A store webhook is never switched off. It keeps receiving events until the merchant disables or deletes it.
- Events of a store that is behind on its Storeep fees wait until it pays.