Guide

Authentication

HMAC-SHA256 request signing — every request, every time.

Every API request is signed with HMAC-SHA256. This proves you hold the secret and that the request hasn't been tampered with.

Required Headers

Every request needs these five headers:

HeaderValue
X-Key-IdYour API key ID (ak_test_... / ak_live_...)
X-TimestampISO-8601 UTC timestamp
X-NonceUnique string per request (UUID works)
X-Body-HashSHA-256 hex of the request body
X-SignatureBase64-encoded HMAC-SHA256 signature

How to Sign

1. Build the canonical string

Six values, joined by newlines:

{METHOD}\n{PATH}\n{SORTED_QUERY}\n{TIMESTAMP}\n{NONCE}\n{BODY_HASH}
PartRules
METHODUppercase: POST, GET
PATHLeading /, no trailing /
SORTED_QUERYQuery params sorted by key (ASCII). Empty string if none.
TIMESTAMPSame as X-Timestamp header
NONCESame as X-Nonce header
BODY_HASHSHA-256 hex of body. No body = SHA-256 of empty string

2. Sign it

signature = base64( hmac_sha256( base64_decode(secret), canonical_string ) )

Your secret is Base64-encoded. Decode it first, then use it as the HMAC key.

3. Send it

Include all five headers. Done.

Timestamps must be within 5 minutes of the server time. Each nonce can only be used once. This prevents replay attacks.

Example

POST/v1/checkout-sessions with body {"mode":"payment","amount":5000,"currency":"USD"}:

Code
1POST
2/v1/checkout-sessions
3
42026-04-07T18:30:00.000Z
5550e8400-e29b-41d4-a716-446655440000
6a3f2b8c1... ← SHA-256 hex of body

The empty third line = no query parameters.

Code Examples

Node.js

JavaScript
1import crypto from "crypto";
2
3function signRequest({ method, path, body, keyId, secret }) {
4 const timestamp = new Date().toISOString();
5 const nonce = crypto.randomUUID();
6 const bodyStr = body ? JSON.stringify(body) : "";
7 const bodyHash = crypto.createHash("sha256").update(bodyStr).digest("hex");
8
9 const canonical = [method, path, "", timestamp, nonce, bodyHash].join("\n");
10
11 const sig = crypto
12 .createHmac("sha256", Buffer.from(secret, "base64"))
13 .update(canonical)
14 .digest("base64");
15
16 return {
17 "Content-Type": "application/json",
18 "X-Key-Id": keyId,
19 "X-Timestamp": timestamp,
20 "X-Nonce": nonce,
21 "X-Body-Hash": bodyHash,
22 "X-Signature": sig,
23 };
24}

Python

Python
1import hashlib, hmac, base64, json
2from datetime import datetime, timezone
3from uuid import uuid4
4
5def sign_request(method, path, body, key_id, secret):
6 timestamp = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%S.000Z")
7 nonce = str(uuid4())
8 body_str = json.dumps(body) if body else ""
9 body_hash = hashlib.sha256(body_str.encode()).hexdigest()
10
11 canonical = f"{method}\n{path}\n\n{timestamp}\n{nonce}\n{body_hash}"
12
13 sig = base64.b64encode(
14 hmac.new(base64.b64decode(secret), canonical.encode(), hashlib.sha256).digest()
15 ).decode()
16
17 return {
18 "Content-Type": "application/json",
19 "X-Key-Id": key_id,
20 "X-Timestamp": timestamp,
21 "X-Nonce": nonce,
22 "X-Body-Hash": body_hash,
23 "X-Signature": sig,
24 }

PHP

PHP
1function signRequest(string $method, string $path, ?array $body, string $keyId, string $secret): array {
2 $timestamp = gmdate('Y-m-d\TH:i:s.000\Z');
3 $nonce = bin2hex(random_bytes(16));
4 $bodyStr = $body ? json_encode($body) : '';
5 $bodyHash = hash('sha256', $bodyStr);
6
7 $canonical = implode("\n", [$method, $path, '', $timestamp, $nonce, $bodyHash]);
8
9 $sig = base64_encode(hash_hmac('sha256', $canonical, base64_decode($secret), true));
10
11 return [
12 'Content-Type' => 'application/json',
13 'X-Key-Id' => $keyId,
14 'X-Timestamp' => $timestamp,
15 'X-Nonce' => $nonce,
16 'X-Body-Hash' => $bodyHash,
17 'X-Signature' => $sig,
18 ];
19}

Powered by CX Pay