> ## Documentation Index
> Fetch the complete documentation index at: https://exchanger-api.iexexchanger.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Sign a request

> Sign private Exchanger API calls with HMAC v2, verify a test signature in Node.js or PHP, and sign the exact JSON bytes or contents of a file upload.

Send a Bearer token and HMAC v2 signature with every request to `/api/v3/private/*`. You receive the token and secret when you [create an API key](https://exchanger-api.iexexchanger.com/en/start/access.md). Public `GET /api/v3/ping` is available without credentials.

## Private request headers

| Header | Value |
| - | - |
| `Authorization` | `Bearer <token>`. Use the full token. |
| `X-Api-Signature-Version` | `v2`. |
| `X-Api-Timestamp` | Unix time in seconds or ISO 8601 with a time zone. Sign the same string. The default tolerance is 300 seconds in either direction. The exchanger can configure it. |
| `X-Api-Nonce` | A fresh string for each HTTP attempt. Use 8 to 128 ASCII letters, digits, or `.`, `_`, `:`, and `-`. A UUID works. |
| `X-Api-Signature` | The HMAC-SHA256 digest in hex. The samples use `sha256=<hex>`. |
| `Idempotency-Key` | If the method supports or requires a key for the operation, send it and include it in the signature. See [idempotency](https://exchanger-api.iexexchanger.com/en/guides/reliability/idempotency.md). |

Send each signature header and the idempotency key at most once.

Use the actual HTTP method from the reference. The same scheme applies to `GET`, `POST`, `PATCH`, and `DELETE` on private routes. For a request without a body, such as checking the key, use the SHA-256 of an empty string.

## Canonical string

1. Assemble the eight fields in the order shown below. Put one LF (`\n`) between adjacent fields. Preserve empty fields, including the eighth. With an empty key, the string ends with one LF after the nonce.
2. Compute HMAC-SHA256 over those bytes. Use the secret string as issued.
3. Add `X-Api-Signature` to the request. Check that the path, query, body, and headers match the values you signed.

| Position | Field |
| - | - |
| 1 | `v2` |
| 2 | Uppercase HTTP method |
| 3 | Path starting with `/` and including `/api/v3`, with no origin or query |
| 4 | Canonical query string, or an empty string |
| 5 | SHA-256 of the body in lowercase hex. Use the SHA-256 of an empty string for a request without a body |
| 6 | `X-Api-Timestamp` value |
| 7 | `X-Api-Nonce` value |
| 8 | `Idempotency-Key` value with leading and trailing whitespace removed, or an empty string |

Encode query names and values according to RFC 3986. Form `name=value` pairs, sort the encoded pairs lexicographically, and join them with `&`. Encode a space as `%20`. Keep list indexes, such as `filter[status][0]`, and encode the entire name. If the URL uses `[]` for list entries, use indexes in list order in the canonical query. For predictable parsing, send each scalar field name once and use the field names in the reference.

### JSON and file uploads

1. Serialize JSON once. Compute SHA-256 over those bytes and send the same bytes with `Content-Type: application/json`. The API also accepts JSON media types of the form `application/*+json`.
2. For an upload, send `multipart/form-data` with one file in the `file` field. Compute SHA-256 over the file contents. Multipart boundaries and part headers are outside the digest. Send other parameters in the path or query as specified by the operation. Additional multipart form fields cause a 401 with `invalid_signature`, even when the file digest is correct.

### Retry a request

Generate a fresh timestamp, nonce, and signature for each HTTP attempt. When retrying the same operation, reuse its `Idempotency-Key` and the signed parameters. The nonce identifies one attempt. The idempotency key links the retries.

## Make a signed request with Node.js

The [quickstart](https://exchanger-api.iexexchanger.com/en/start/quickstart.md) has an HTTP request that verifies your key on your exchanger. To check the signing calculation locally, first save the example below as `sign.mjs` or `sign.php`. Run `node sign.mjs` or `php sign.php`. The code prints the signature without sending a request. For a real request, replace the example secret, timestamp, and nonce.

```javascript Node.js theme={null}
import { createHash, createHmac } from 'node:crypto';

function encodeRfc3986(value) {
  return encodeURIComponent(value).replace(/[!'()*]/g, (character) => (
    '%' + character.charCodeAt(0).toString(16).toUpperCase()
  ));
}

function signRequest({ method, path, queryPairs, bodyBytes, timestamp, nonce, idempotencyKey, secret }) {
  const query = queryPairs
    .map(([name, value]) => encodeRfc3986(name) + '=' + encodeRfc3986(value))
    .sort()
    .join('&');
  const bodyHash = createHash('sha256').update(bodyBytes).digest('hex');
  const canonical = [
    'v2',
    method.toUpperCase(),
    path,
    query,
    bodyHash,
    timestamp,
    nonce,
    idempotencyKey.trim(),
  ].join('\n');

  return 'sha256=' + createHmac('sha256', secret).update(canonical).digest('hex');
}

console.log(signRequest({
  method: 'GET',
  path: '/api/v3/private/orders',
  queryPairs: [['per_page', '5'], ['page', '2']],
  bodyBytes: Buffer.alloc(0),
  timestamp: '1767225600',
  nonce: 'docs-vector-2026',
  idempotencyKey: '',
  secret: 'example-hmac-secret',
}));
```

```php PHP theme={null}
<?php

declare(strict_types=1);

function signRequest(
    string $method,
    string $path,
    array $queryPairs,
    string $bodyBytes,
    string $timestamp,
    string $nonce,
    string $idempotencyKey,
    string $secret,
): string {
    $queryParts = [];
    foreach ($queryPairs as [$name, $value]) {
        $queryParts[] = rawurlencode($name).'='.rawurlencode($value);
    }

    sort($queryParts, SORT_STRING);

    $canonical = implode("\n", [
        'v2',
        strtoupper($method),
        $path,
        implode('&', $queryParts),
        hash('sha256', $bodyBytes),
        $timestamp,
        $nonce,
        trim($idempotencyKey),
    ]);

    return 'sha256='.hash_hmac('sha256', $canonical, $secret);
}

echo signRequest(
    method: 'GET',
    path: '/api/v3/private/orders',
    queryPairs: [['per_page', '5'], ['page', '2']],
    bodyBytes: '',
    timestamp: '1767225600',
    nonce: 'docs-vector-2026',
    idempotencyKey: '',
    secret: 'example-hmac-secret',
), PHP_EOL;
```

### Test vector

Use the secret `example-hmac-secret`, request `GET /api/v3/private/orders?per_page=5&page=2`, an empty body, and no idempotency key. Set timestamp to `1767225600` and nonce to `docs-vector-2026`.

```text theme={null}
v2
GET
/api/v3/private/orders
page=2&per_page=5
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
1767225600
docs-vector-2026
```

One LF follows `docs-vector-2026`. The expected header is shown below.

```http theme={null}
X-Api-Signature: sha256=63dba9118cba7c2d56043170fbde8bf2e4dae9814635a6587fc16cc73e0af0e8
```

This HTTP request shows all the headers with a sample token and a fixed timestamp.

```http theme={null}
GET /api/v3/private/orders?per_page=5&page=2 HTTP/1.1
Host: your-exchange
Accept: application/json
Authorization: Bearer example-token
X-Api-Signature-Version: v2
X-Api-Timestamp: 1767225600
X-Api-Nonce: docs-vector-2026
X-Api-Signature: sha256=63dba9118cba7c2d56043170fbde8bf2e4dae9814635a6587fc16cc73e0af0e8
```

For a request to your exchanger, use its address, valid credentials, the current time, and a fresh nonce. Recalculate the signature for those values.

## Signature errors

| `error.code` | What to check |
| - | - |
| `signature_required` | The signature headers, hex format, and whether the key has an HMAC secret. |
| `invalid_signature` | The version, duplicate headers, token and secret, method, path, query, body bytes, and idempotency key. |
| `signature_expired` | The timestamp format and the integration server's clock. |
| `signature_replay` | A fresh nonce for each attempt. |

On `403`, check the [key's scopes and IP allow-list](https://exchanger-api.iexexchanger.com/en/start/access.md). See [API errors](https://exchanger-api.iexexchanger.com/en/guides/reliability/errors.md) for reading `error.code` and deciding whether to retry.

> [accordion] AI prompt for HMAC v2 signing
>
> ```text theme={null}
> Implement and test an Exchanger API v3 signing client in [STACK]. Read the current signing contract and retry rules first.
>
> Signing: https://exchanger-api.iexexchanger.com/en/start/authentication
> First request: https://exchanger-api.iexexchanger.com/en/start/quickstart
> Retries: https://exchanger-api.iexexchanger.com/en/guides/reliability/idempotency
>
> Join eight fields with LF in this order: v2, the uppercase method, the path including /api/v3 without the origin or query, the canonical query, the SHA-256 body digest in hex, the timestamp, the nonce, and Idempotency-Key trimmed at both ends. Preserve empty fields. Encode and sort query pairs according to the contract's RFC 3986 rules and preserve list indexes. Calculate HMAC-SHA256 over the UTF-8 canonical string with the HMAC secret as issued. Send X-Api-Signature-Version: v2, X-Api-Timestamp, X-Api-Nonce, and X-Api-Signature as sha256=<hex>. Read the Bearer token from client configuration and send it separately in Authorization.
>
> Hash the same bytes the transport sends. Serialize JSON once. For multipart, hash the contents of the single file field instead of the multipart body. Generate a fresh timestamp and nonce for each attempt. Reuse the Idempotency-Key and original input when retrying the same write.
>
> Verify the published test vector offline. Test empty query and body, a changed JSON byte, query sorting, list order, file hashing, and a changed Idempotency-Key. Use mocked responses to test signature_required, invalid_signature, signature_expired, and signature_replay. Use synthetic data and a mock transport. Return the code, tests, and their actual results.
> ```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.