# Errors

Every error response, on every `/v1` endpoint, has the same shape:

```json
{
  "error": { "code": "missing_parameter", "message": "text is required.", "type": "invalid_request_error", "param": "text" },
  "request_id": "req_5f0c9a1e2b3d4c5e6f708192"
}
```

- `code` is stable and machine-readable: branch on it, not on `message`.
- `type` groups codes: `invalid_request_error`, `authentication_error`, `billing_error`,
  `permission_error`, `not_found_error`, `rate_limit_error`, `api_error`.
- `param` names the request field at fault (`text`, `audio`, `model`, `consent`, …), or is
  `null` when no single field is at fault.
- `request_id` equals the `X-Request-Id` header. Quote it to support.
- Legacy fields `success: false` and `message` are also present; do not rely on them.
- Failed calls are never charged.

## Codes

The default code for each HTTP status is generated from the API's error middleware
and listed in `x-talksign-error-codes` of `GET /v1/openapi.json`.

| Status | Code | Type | Meaning | Retry? |
|---|---|---|---|---|
| 400 | `invalid_request` | invalid_request_error | Missing/invalid parameter, malformed JSON, unknown model. | No – fix the request. |
| 400 | `missing_parameter` | invalid_request_error | A required field is missing (`param` names it). | No – fix the request. |
| 400 | `invalid_parameter` | invalid_request_error | A field has an invalid value (`param` names it). | No. |
| 400 | `unknown_model` | invalid_request_error | `model` is not offered for this service. | No. |
| 400 | `invalid_file` | invalid_request_error | Uploaded file missing, unreadable or the wrong type (checked from its content). | No. |
| 400 | `consent_required` | invalid_request_error | `consent` / `disclosure_accepted` is not `true`. | No. |
| 400 | `invalid_version` | invalid_request_error | Unknown `Talksign-Version`. | No. |
| 400 | `invalid_idempotency_key` | invalid_request_error | `Idempotency-Key` not 1–255 printable ASCII characters. | No. |
| 401 | `invalid_api_key` | authentication_error | Missing, malformed, unknown, revoked or expired key. | No – check the key. |
| 402 | `insufficient_credits` | billing_error | Wallet cannot cover the call, or the hard spend limit paused requests. | After a top-up / limit change. |
| 402 | `payment_method_required` | billing_error | Creating a custom avatar needs a saved card. | After adding a card. |
| 402 | `avatar_slot_inactive` | billing_error | The custom avatar's yearly slot is unpaid. | After fixing payment. |
| 403 | `permission_denied` | permission_error | The key may not do this. | No. |
| 403 | `insufficient_scope` | permission_error | The key lacks the scope for this endpoint. | Use a key with that scope. |
| 403 | `ip_not_allowed` | permission_error | The key's IP allow-list does not include your IP. | No. |
| 403 | `cors_origin_not_allowed` | permission_error | Browser call from an origin other than the portal. | Call from your server. |
| 403 | `avatar_pending_review` | permission_error | The custom avatar is still being reviewed. | Later. |
| 403 | `avatar_limit` | permission_error | 50 custom avatars per account. | No. |
| 404 | `not_found` | not_found_error | Unknown endpoint or resource (e.g. a video you did not render). | No. |
| 404 | `avatar_not_found` | not_found_error | Unknown avatar id, or another account's avatar. | No. |
| 404 | `job_not_found` / `meeting_not_found` / `upload_not_found` | not_found_error | Unknown id for your account. | No. |
| 405 | `method_not_allowed` | invalid_request_error | Wrong HTTP method. | No. |
| 409 | `conflict` | invalid_request_error | Conflicting resource state. | Depends. |
| 409 | `avatar_not_ready` | invalid_request_error | Custom avatar still preparing. | After `estimated_ready_at`. |
| 409 | `avatar_coming_soon` | invalid_request_error | Preset avatar not available yet. | No – pick another. |
| 409 | `avatar_unusable` | invalid_request_error | Custom avatar failed preparation. | No. |
| 409 | `idempotency_in_progress` | invalid_request_error | Same `Idempotency-Key` still running. | Yes, after 1–2 s. |
| 413 | `payload_too_large` | invalid_request_error | Upload too large (50 MB for avatars). | No. |
| 413 | `text_too_long` | invalid_request_error | `text` over 5,000 characters. | Split the text. |
| 413 | `file_too_large` | invalid_request_error | File over its limit (audio 25 MB, video 500 MB, Sign Morph video 200 MB, photo 10 MB). | No. |
| 413 | `audio_too_long` / `video_too_long` | invalid_request_error | Audio over 10 min; video over 10 min (captions) or 30 s (Sign Morph). | Shorten it. |
| 409 | `meeting_already_interpreted` | invalid_request_error | An interpreter is already in that meeting. | No. |
| 422 | `invalid_request` | invalid_request_error | Semantically invalid request. | No. |
| 422 | `avatar_not_available` | invalid_request_error | Avatar has no Echo 2.0 signing yet. | No – pick another. |
| 422 | `idempotency_key_reused` | invalid_request_error | Key reused with another endpoint or body. | Use a new key. |
| 422 | `no_speech` | invalid_request_error | No speech found in the audio or video. Nothing charged. | No. |
| 422 | `avatar_quality_failed` | invalid_request_error | Photo/video failed a quality check (`checks[]`). | With a better photo. |
| 429 | `rate_limit_exceeded` | rate_limit_error | Too many requests. | Yes, after `Retry-After`. |
| 429 | `avatar_quality_retry_limit` | rate_limit_error | 5 failed avatar creations in 24 h. | After 24 h. |
| 429 | `meeting_user_limit` / `meeting_daily_limit` | rate_limit_error | Too many meetings at once / today. | Later. |
| 500 | `internal_error` | api_error | Unexpected error. | Yes, with backoff. |
| 501 | `not_available` | api_error | Not offered through the API yet (sign to speech). | No. |
| 502 | `avatar_upload_failed` | api_error | Avatar upload failed; slot removed, nothing charged. | Yes. |
| 502 | `transcription_failed` | api_error | Speech-to-text failed; nothing charged. | Yes. |
| 503 | `meetings_busy` | api_error | All interpreters are busy. | Yes, in a minute. |
| 502–504 | `service_unavailable` | api_error | The engine is temporarily unavailable. | Yes, with backoff. |

## Retry guidance

- Retry only `409 idempotency_in_progress`, `429`, `500`, `502`, `503`, `504` and network errors.
- Always send the **same `Idempotency-Key`** on a retry of a paid POST: a call that
  actually succeeded is then replayed, not charged twice.
- `429`: wait the `Retry-After` seconds.
- `5xx` / network: exponential backoff with jitter, e.g. 0.5 s, 1 s, 2 s, 4 s, 8 s (max 5 tries).
- Never retry `400`, `401`, `402`, `403`, `404`, `413`, `422` unchanged.

```js
async function callTalksign(path, body, key, tries = 5) {
  const idem = crypto.randomUUID();
  for (let i = 0; i < tries; i++) {
    const res = await fetch(`https://api-staging.talksign.co/v1${path}`, {
      method: 'POST',
      headers: { Authorization: `Bearer ${key}`, 'Content-Type': 'application/json', 'Idempotency-Key': idem },
      body: JSON.stringify(body),
    }).catch(() => null);
    if (res && res.ok) return res.json();
    const status = res ? res.status : 0;
    const retryable = !res || status === 409 || status === 429 || status >= 500;
    if (!retryable || i === tries - 1) throw new Error(res ? JSON.stringify(await res.json()) : 'network error');
    const wait = status === 429 ? Number(res.headers.get('retry-after') || 1) * 1000 : 500 * 2 ** i * (0.5 + Math.random());
    await new Promise((r) => setTimeout(r, wait));
  }
}
```
