# Authentication

> OAuth2 client_credentials for a one-hour token, and the token plus your X-API-Key on every call.

[Production]

The API uses two credentials together, the same way in the sandbox and in production:

1. An OAuth2 **access token** you get with your `client_id` and your `client_secret`. It travels in `Authorization: Bearer <access_token>`.
2. Your **API key**, which travels in the `X-API-Key` header.

## Get the token

**POST** `/v1/auth/token` — Get an access token · [Production] · [See the reference](https://developers.zulu.com.co/en/reference/get-token.md)

The body is snake_case, as in OAuth2 (`client_id`, `client_secret`). It is the only place in the API that is not camelCase.

**POST /v1/auth/token**

```bash
curl https://sandbox.zulu.com.co/v1/auth/token \
  -H "Content-Type: application/json" \
  -d "{\"client_id\": \"$ZULU_CLIENT_ID\", \"client_secret\": \"$ZULU_CLIENT_SECRET\"}"
```

```python
import os

import requests

response = requests.post(
    "https://sandbox.zulu.com.co/v1/auth/token",
    json={"client_id": os.environ["ZULU_CLIENT_ID"], "client_secret": os.environ["ZULU_CLIENT_SECRET"]},
    timeout=30,
)
token = response.json()["access_token"]
```

The token is a JWT, but treat it as an opaque value: do not depend on its contents.

## Reuse the token

The token expires in `expires_in` seconds (3600, one hour). Request a new one only when it is about to expire: requesting one per call can end in `429 too_many_requests`. A minimal cache in Node, with `ZULU_API_URL` pointing at your environment's base URL:

```js title="token.js"
let cached = null;

export async function accessToken() {
  if (cached && cached.expiresAt - 60_000 > Date.now()) return cached.value;

  const response = await fetch(`${process.env.ZULU_API_URL}/auth/token`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ client_id: process.env.ZULU_CLIENT_ID, client_secret: process.env.ZULU_CLIENT_SECRET }),
  });
  if (!response.ok) throw new Error(`zulu token: ${response.status}`);

  const body = await response.json();
  cached = { value: body.access_token, expiresAt: Date.now() + body.expires_in * 1000 };

  return cached.value;
}
```

## Call every other endpoint

Every call carries both headers:

```bash
curl https://sandbox.zulu.com.co/v1/balances \
  -H "Authorization: Bearer $ZULU_ACCESS_TOKEN" \
  -H "X-API-Key: $ZULU_API_KEY"
```

The checks run in production's gateway order: first the token (missing or expired, `401 {"message":"Unauthorized"}`), then the API key (missing, or another credential's, `403 {"message":"Forbidden"}`).

## Authentication errors

| HTTP | error | cause | Description |
| --- | --- | --- | --- |
| 401 | `access_denied` | `Unauthorized` | The `client_id` or the `client_secret` is not valid. |
| 401 | — | `Unauthorized` | The `Authorization: Bearer <access_token>` header is missing, or the token is invalid or expired. |
| 403 | `access_denied` | `Client is not authorized to access this API` | The credentials are valid but the client is not enabled to use the API (revoked credential or suspended workspace). |
| 403 | — | `Forbidden` | The `X-API-Key` is missing, unknown, revoked or does not belong to the credentials of the token. |
| 429 | `too_many_requests` | `Too many requests` | Too many token requests. Reuse the current token until it expires (`expires_in`). |
| 429 | — | `Too Many Requests` | You exceeded the request limit of your credentials. Wait the seconds in `Retry-After`. |

An unknown `client_id` and a wrong secret answer the same (`401 access_denied`), so nobody can find out which clients exist.

## Good practices

- Keep `client_secret` and `X-API-Key` in a secrets manager or in environment variables; never in the repository or the frontend.
- Use separate credentials per environment and per service, so you can revoke them one by one from the dashboard.
- If a credential leaks, revoke it: its tokens stop working even before they expire.
