---
title: CP-HMAC request signing
description: How to sign Coinetic API requests: the CP-HMAC Authorization header, the string to sign, nonces, allowed clock skew and HMAC-SHA256 test vectors.
url: https://coinetic.money/docs/authentication/
lang: en
---

# CP-HMAC request signing

Every request to `/v1/*` is signed with your API key secret. The secret itself never goes over the wire. The official clients handle this for you; this page is for anyone writing their own.

## Header

```http
Authorization: CP-HMAC key=<key_id>, ts=<unix_seconds>, nonce=<nonce>, sig=<hex>
```

| Field | Format |
| --- | --- |
| `key` | Key ID: `ak_` + 16–40 characters `[a-z0-9]` |
| `ts` | Current time in Unix seconds. Allowed skew from the server clock: **±5 minutes** |
| `nonce` | Single-use string `[A-Za-z0-9_-]{16,64}`; a reused nonce is rejected |
| `sig` | 64 lowercase hex characters |

## What gets signed

```
string = METHOD + "\n" + PATH_WITH_QUERY + "\n" + ts + "\n" + nonce + "\n" + hex(sha256(body))
sig    = hex(HMAC-SHA256(secret, string))
```

- `METHOD`: `GET`, `POST`, and so on.
- `PATH_WITH_QUERY`: the path and query string exactly as they'll be sent: `/v1/invoices?status=paid&limit=50`. The scheme and host aren't signed.
- The body is signed byte for byte. Sign the exact JSON string you'll send, not a re-serialized object.
- For requests without a body, `hex(sha256(""))` = `e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855`.

**Node.js** — `sign.js`

```js
import { createHash, createHmac, randomBytes } from "node:crypto";

export function sign(keyId, secret, method, pathWithQuery, body = "") {
  const ts = Math.floor(Date.now() / 1000).toString();
  const nonce = randomBytes(16).toString("hex");
  const bodyHash = createHash("sha256").update(body).digest("hex");
  const sig = createHmac("sha256", secret)
    .update(`${method}\n${pathWithQuery}\n${ts}\n${nonce}\n${bodyHash}`)
    .digest("hex");
  return `CP-HMAC key=${keyId}, ts=${ts}, nonce=${nonce}, sig=${sig}`;
}
```

**PHP** — `sign.php`

```php
function cp_sign(string $keyId, string $secret, string $method, string $pathWithQuery, string $body = ''): string {
    $ts = (string) time();
    $nonce = bin2hex(random_bytes(16));
    $str = $method . "\n" . $pathWithQuery . "\n" . $ts . "\n" . $nonce . "\n" . hash('sha256', $body);
    $sig = hash_hmac('sha256', $str, $secret);
    return "CP-HMAC key=$keyId, ts=$ts, nonce=$nonce, sig=$sig";
}
```

**Python** — `sign.py`

```py
import hashlib, hmac, secrets, time

def cp_sign(key_id: str, secret: str, method: str, path_with_query: str, body: bytes = b"") -> str:
    ts = str(int(time.time()))
    nonce = secrets.token_hex(16)
    msg = f"{method}\n{path_with_query}\n{ts}\n{nonce}\n{hashlib.sha256(body).hexdigest()}"
    sig = hmac.new(secret.encode(), msg.encode(), hashlib.sha256).hexdigest()
    return f"CP-HMAC key={key_id}, ts={ts}, nonce={nonce}, sig={sig}"
```

**Go** — `sign.go`

```go
sig := cryptopayclient.SignRequest(secret, "POST", "/v1/invoices", ts, nonce, body)
req.Header.Set("Authorization", fmt.Sprintf("CP-HMAC key=%s, ts=%d, nonce=%s, sig=%s", keyID, ts, nonce, sig))
```

## Test vectors

Check your implementation against these values. All four official clients are tested against the same ones.

| Secret | `test-secret-0123456789abcdef0123456789` |
| --- | --- |
| Request 1 | `POST /v1/invoices`, ts `1760000000`, nonce `nonce-0000000001`, body `{"order_id":"WISH-1"}` |
| sig 1 | `2c769f6a5be7b80871e03851ccfe84002130ec48a45348bd821752a6d45960ec` |
| Request 2 | `GET /v1/invoices/inv_abcdefabcdefabcdef`, ts `1760000000`, nonce `nonce-0000000002`, no body |
| sig 2 | `a12924acf8a772c6f3fadb8874ff69e9934046ccd2fa5563b23fec3e98e1665b` |

## Getting a 401?

Every signature failure returns `401 unauthorized` with the same message; the server won't tell you exactly what's wrong. Common causes:

- the server clock has drifted by more than 5 minutes (enable NTP);
- a different path was signed: missing the query string, or with parameters in a different order;
- the body was re-serialized after signing;
- a nonce was reused;
- the key has been revoked.

`403 merchant_suspended` means the signature is valid, but the merchant is suspended.

## Keys

- Create keys in the dashboard under “Keys & webhooks.” The secret is shown **only once**.
- Up to 10 keys can be active at once. To rotate: create a new key, switch your server over, then revoke the old one. Revocation takes effect immediately.
- Keep the secret in a secrets manager or a server environment variable, never in code or git.
- The rate limit is about 20 requests per second per merchant, with headroom for bursts; exceeding it returns `429 rate_limited`.
