# Authentication

Authenticate with an API key and secret. The official SDKs sign requests automatically. Use the following headers when building a custom client.

## Headers

| Header               | Value                                            |
| -------------------- | ------------------------------------------------ |
| `Authorization`      | `Key <api_key>`                                  |
| `x-armada-timestamp` | Current Unix time in milliseconds                |
| `x-armada-signature` | Lowercase hex HMAC-SHA256 of the request payload |
| `Content-Type`       | `application/json` for requests with a JSON body |

## Computing the signature

```text
payload = timestamp + "." + method + "." + path + "." + body
signature = hex(HMAC-SHA256(secret, payload))
```

- `method`: uppercase, such as `GET` or `POST`.
- `path`: pathname and exact query string, excluding the origin.
- `body`: the exact UTF-8 bytes sent. Use an empty string when there is no body.
- `timestamp`: must be within 30 seconds of the server's clock. Sign immediately before sending.

Encode the signature as lowercase hexadecimal. Uppercase hex is rejected.

This Node example signs and sends a wallet request without an SDK:

```javascript
import { createHmac } from 'node:crypto';

const method = 'GET';
const path = '/v2/wallet';
const body = '';
const timestamp = String(Date.now());
const signature = createHmac('sha256', process.env.ARMADA_API_SECRET)
	.update(`${timestamp}.${method}.${path}.${body}`)
	.digest('hex');

const response = await fetch(`https://api.armadadelivery.com${path}`, {
	method,
	headers: {
		Authorization: `Key ${process.env.ARMADA_API_KEY}`,
		'x-armada-timestamp': timestamp,
		'x-armada-signature': signature
	}
});
if (!response.ok) throw new Error(`HTTP ${response.status}: ${await response.text()}`);
console.log(await response.json());
```

## Test vector

Use these fixed inputs to test a signing implementation locally. The timestamp is intentionally fixed and cannot be used for a live request.

```json
{
  "secret": "00000000-0000-0000-0000-000000000000",
  "timestamp": "1776182400000",
  "method": "POST",
  "path": "/v2/deliveries",
  "body": "{\"reference\":\"order-1\",\"payment\":{\"amount\":4.5,\"type\":\"paid\"}}",
  "signature": "834a2a959cb0faba10124884ae728535c9c1cf29a44cb6fbfc39405d583c236f"
}
```

## Troubleshooting

For `401`, check the key and secret, clock, query string and body bytes. Serialize JSON once; sign and send that same string. For `403`, check the endpoint's permission and the response message.

## Replace credentials

Create a new key to replace your credentials:

1. Create a new key in **Automated Ordering**. It starts in Test mode with all permissions and empty webhook settings.
2. Configure only the required permissions, the intended Test mode setting, callback URLs and statuses, then click **Save changes**.
3. Switch your server to the new key and secret, and add its new `x-armada-key-id` to your receiver's mapping.
4. Keep the old key until its outstanding deliveries and callbacks are finished, then delete it.

Keep credentials on your server. Webhook settings do not transfer automatically to a replacement key.
