---
title: API authentication and request signing
description: HMAC-SHA256 signing for the GaaPay merchant API: the exact string to sign, replay protection with nonces, and working Node, Python and PHP examples.
url: https://gaapay.com/docs/auth
updated: 2026-12-04
language: en
---

Every merchant API request carries four headers. The signing string is joined by newlines and the **order is fixed**:

```
payload = timestamp + "\n" + nonce + "\n" + METHOD + "\n" + path + "\n" + raw_body
signature = hex( HMAC_SHA256(app_secret, payload) )
```

- `timestamp` — Unix seconds. Rejected if it drifts more than 5 minutes.
- `nonce` — unique per request. The server deduplicates over a sliding window; a repeat is rejected (replay protection).
- `path` — path including the query string, e.g. `/api/v1/orders?limit=20`.
- `raw_body` — the exact request body string (empty for GET). **Never re-serialize before signing** — key order and float formatting will differ.

```
// Node.js
import { createHmac, randomUUID } from 'node:crypto';

function signedFetch(path, body) {
  const ts = Math.floor(Date.now() / 1000);
  const nonce = randomUUID();
  const raw = body ? JSON.stringify(body) : '';
  const sig = createHmac('sha256', process.env.GAAPAY_SECRET)
    .update([ts, nonce, body ? 'POST' : 'GET', path, raw].join('\n'))
    .digest('hex');
  return fetch(`https://api.gaapay.com${path}`, {
    method: body ? 'POST' : 'GET',
    headers: {
      'content-type': 'application/json',
      'x-gaapay-app': process.env.GAAPAY_APP,
      'x-gaapay-timestamp': String(ts),
      'x-gaapay-nonce': nonce,
      'x-gaapay-signature': sig,
    },
    body: raw || undefined,
  });
}
```

```
# Python
import hmac, hashlib, time, uuid

def sign(method, path, body: str, secret: str):
    ts, nonce = str(int(time.time())), str(uuid.uuid4())
    payload = f"{ts}\n{nonce}\n{method}\n{path}\n{body}"
    sig = hmac.new(secret.encode(), payload.encode(), hashlib.sha256).hexdigest()
    return {"x-gaapay-timestamp": ts, "x-gaapay-nonce": nonce, "x-gaapay-signature": sig}
```

```
<?php
$ts = time(); $nonce = bin2hex(random_bytes(8)); $raw = json_encode($body);
$payload = $ts."\n".$nonce."\nPOST\n".$path."\n".$raw;
$sig = hash_hmac('sha256', $payload, $secret);
// headers: x-gaapay-app / x-gaapay-timestamp / x-gaapay-nonce / x-gaapay-signature
```
