# Keys and errors

Every call authenticates with a bearer key and returns structured problem details when anything fails.

## API keys

Send your key as `Authorization: Bearer <key>` on every request. The API stores only the SHA-256 hash of a key and checks what it may see and do inside each request's database transaction.

Calls without a valid key return `401`. Calling a customer-only route with a non-customer key returns `403`.

## Credits and key limits

Each customer has a credit balance, and an individual key may also carry a `credit_limit` capping what that key's jobs can hold or spend. Check both with `GET /v1/credits`.

```bash
$ curl https://api.sequencylabs.com/v1/credits \
  -H "Authorization: Bearer $SEQUENCY_API_KEY"
```

```python
import os
import httpx

response = httpx.get(
    "https://api.sequencylabs.com/v1/credits",
    headers={"Authorization": f"Bearer {os.environ['SEQUENCY_API_KEY']}"},
)
response.raise_for_status()
data = response.json()
```

```javascript
const response = await fetch("https://api.sequencylabs.com/v1/credits", {
  headers: {
    Authorization: `Bearer ${process.env.SEQUENCY_API_KEY}`,
  },
});
const data = await response.json();
```

```json
{
  "balance": 5000.0,
  "key_limit": null,
  "key_spent": 10.0
}
```

When you ask for a job, the API holds the most it may cost: each reaction at its kind's `reaction` rate from `GET /v1/prices`, or `max_credits` for a synthesis. Asking fails with `402` when the balance or remaining key limit does not cover that hold, and with `503` when that kind of job has no price yet.

A canceled job charges nothing. When a job completes, its first package sets the final cost as `fresh * reaction + reused * reused_reaction`, never more than it held, and releases the rest back to the balance. A synthesis served whole from earlier results counts as one reused reaction.

## Idempotency

Send `Idempotency-Key` on `POST /v1/jobs` with 1 to 255 visible ASCII characters. Retrying with the same key and the same request returns `201` with the job the first call created. Reusing the same key for a different request returns `409`.

## Error format

Every `4xx` and `5xx` response carries a `Problem` object as `application/problem+json` (RFC 9457). The `type` field is always `about:blank`, `status` matches the HTTP status code and `detail` explains what went wrong.

```json
{
  "detail": "job not found",
  "status": 404,
  "title": "Not Found",
  "type": "about:blank"
}
```

The API rejects unknown fields with `422`, so a misspelled field fails instead of being ignored. JSON request bodies are limited to 1 MiB (`413`) and must use `application/json` (`415`).
