Signing events
Every POST /v1/events carries two proofs: the API key in Authorization says whose request it is, and the signature says the body was not changed on the way. The signature follows the open Standard Webhooks scheme, the same one we use to sign webhooks.
What to sign
Section titled “What to sign”| Header | What it holds |
|---|---|
webhook-id |
the sending ID; passing event_id is convenient |
webhook-timestamp |
sending time, Unix seconds |
webhook-signature |
v1, and the base64 signature |
The signature is HMAC-SHA256 of the string {webhook-id}.{webhook-timestamp}.{body}:
- Key: the event signing secret (
whsec_…), the part afterwhsec_, decoded from base64. - Body: exactly the bytes sent in the request. Serialize JSON once, sign that string and send the same string. If your HTTP client serializes the object itself, the signature will not match.
- Time: the sending time, not
occurred_at. Sign a retry an hour later anew, with a new timestamp.
// app/lib/aapp.ts: only on the server, never in a client componentimport { createHmac } from 'node:crypto';
const API = 'https://test-api.zirva.xyz';
export async function sendEvent(event: Record<string, unknown> & { event_id: string }) { const body = JSON.stringify(event); const timestamp = Math.floor(Date.now() / 1000).toString(); const key = Buffer.from(process.env.AAPP_SIGNING_SECRET!.slice('whsec_'.length), 'base64'); const signature = createHmac('sha256', key) .update(`${event.event_id}.${timestamp}.${body}`) .digest('base64');
const res = await fetch(`${API}/v1/events`, { method: 'POST', headers: { Authorization: `Bearer ${process.env.AAPP_API_KEY}`, 'Content-Type': 'application/json', 'webhook-id': event.event_id, 'webhook-timestamp': timestamp, 'webhook-signature': `v1,${signature}`, }, body, signal: AbortSignal.timeout(10_000), }); return { status: res.status, body: await res.json() };}
await sendEvent({ event_id: 'evt_90114', type: 'order.paid', user_ref: '4f9a', order_id: 'ord_78123', occurred_at: new Date().toISOString(), amount: 25.0, currency: 'AZN', items: [{ sku: 'game-123', category: 'rpg', price: 25.0, qty: 1 }],});import java.net.URI;import java.net.http.HttpClient;import java.net.http.HttpRequest;import java.net.http.HttpResponse;import java.nio.charset.StandardCharsets;import java.time.Duration;import java.time.Instant;import java.util.Base64;import javax.crypto.Mac;import javax.crypto.spec.SecretKeySpec;
public class AappEvents { static final String API = "https://test-api.zirva.xyz";
public static void main(String[] args) throws Exception { String id = "evt_90114"; String body = """ {"event_id":"evt_90114","type":"order.paid","user_ref":"4f9a","order_id":"ord_78123",\ "occurred_at":"%s","amount":25.00,"currency":"AZN"}""".formatted(Instant.now()); String timestamp = Long.toString(Instant.now().getEpochSecond());
String secret = System.getenv("AAPP_SIGNING_SECRET"); // whsec_… byte[] key = Base64.getDecoder().decode(secret.substring("whsec_".length())); Mac mac = Mac.getInstance("HmacSHA256"); mac.init(new SecretKeySpec(key, "HmacSHA256")); String signature = Base64.getEncoder().encodeToString( mac.doFinal((id + "." + timestamp + "." + body).getBytes(StandardCharsets.UTF_8)));
HttpRequest request = HttpRequest.newBuilder(URI.create(API + "/v1/events")) .timeout(Duration.ofSeconds(10)) .header("Authorization", "Bearer " + System.getenv("AAPP_API_KEY")) .header("Content-Type", "application/json") .header("webhook-id", id) .header("webhook-timestamp", timestamp) .header("webhook-signature", "v1," + signature) .POST(HttpRequest.BodyPublishers.ofString(body, StandardCharsets.UTF_8)) .build(); HttpResponse<String> response = HttpClient.newHttpClient() .send(request, HttpResponse.BodyHandlers.ofString()); System.out.println(response.statusCode() + " " + response.body()); }}package main
import ( "bytes" "crypto/hmac" "crypto/sha256" "encoding/base64" "encoding/json" "fmt" "io" "net/http" "os" "strconv" "strings" "time")
const api = "https://test-api.zirva.xyz"
func main() { event := map[string]any{ "event_id": "evt_90114", "type": "order.paid", "user_ref": "4f9a", "order_id": "ord_78123", "occurred_at": time.Now().UTC().Format(time.RFC3339), "amount": 25.00, "currency": "AZN", } body, _ := json.Marshal(event) id := "evt_90114" timestamp := strconv.FormatInt(time.Now().Unix(), 10)
key, err := base64.StdEncoding.DecodeString(strings.TrimPrefix(os.Getenv("AAPP_SIGNING_SECRET"), "whsec_")) if err != nil { panic(err) } mac := hmac.New(sha256.New, key) mac.Write([]byte(id + "." + timestamp + "." + string(body))) signature := base64.StdEncoding.EncodeToString(mac.Sum(nil))
req, _ := http.NewRequest(http.MethodPost, api+"/v1/events", bytes.NewReader(body)) req.Header.Set("Authorization", "Bearer "+os.Getenv("AAPP_API_KEY")) req.Header.Set("Content-Type", "application/json") req.Header.Set("webhook-id", id) req.Header.Set("webhook-timestamp", timestamp) req.Header.Set("webhook-signature", "v1,"+signature)
client := &http.Client{Timeout: 10 * time.Second} res, err := client.Do(req) if err != nil { panic(err) } defer res.Body.Close() out, _ := io.ReadAll(res.Body) fmt.Println(res.StatusCode, string(out))}API=https://test-api.zirva.xyzBODY='{"event_id":"evt_90114","type":"order.paid","user_ref":"4f9a","order_id":"ord_78123","occurred_at":"2026-11-29T10:00:00Z","amount":25.00,"currency":"AZN"}'ID=evt_90114TS=$(date +%s)KEY_HEX=$(printf %s "${AAPP_SIGNING_SECRET#whsec_}" | base64 -d | od -An -tx1 | tr -d ' \n')SIG=$(printf %s "$ID.$TS.$BODY" | openssl dgst -sha256 -mac HMAC -macopt hexkey:$KEY_HEX -binary | base64)
curl -sS "$API/v1/events" \ -H "Authorization: Bearer $AAPP_API_KEY" \ -H 'Content-Type: application/json' \ -H "webhook-id: $ID" \ -H "webhook-timestamp: $TS" \ -H "webhook-signature: v1,$SIG" \ -d "$BODY"All four examples were checked against the official Standard Webhooks library. You can use the library too: Webhook.sign(id, timestamp, body) produces the same signature.
Signature errors
Section titled “Signature errors”| Response | What it means | What to do |
|---|---|---|
401 invalid_api_key |
the API key is missing, wrong or revoked | check the Authorization: Bearer aapp_… header and the key’s mode |
401 invalid_signature |
the signature did not match | check that you signed the same bytes you sent, with the secret of the same mode as the key |
401 timestamp_out_of_tolerance |
the timestamp is more than 5 minutes from our time | sync the server clock (NTP) and sign right before sending |
An event with a signature error does not enter the log. After fixing it, send it again with the same event_id.
Rotating the secret
Section titled “Rotating the secret”You can rotate the secret without downtime. We issue a new secret, and for a while both are valid. During that time, send two signatures separated by a space: v1,<signed with old> v1,<signed with new>. Any valid one is enough. Once the new secret is on all your servers, ask us to revoke the old one.