> ## Documentation Index
> Fetch the complete documentation index at: https://docs.fish.audio/llms.txt
> Use this file to discover all available pages before exploring further.

# Mint One-Time Ephemeral Token

> Mint a short-lived single-use ek-fish- token from your backend for direct client connections

Mint a short-lived, **one-time (single-use) `ek-fish-` ephemeral token** from your backend so an untrusted client (browser, mobile app, or desktop app) can connect directly to Fish Audio inference endpoints without exposing your `sk-fish-` API key.

For end-to-end browser WebSocket and `fetch` examples, see [One-Time Ephemeral Tokens](https://docs.fish.audio/developer-guide/getting-started/ephemeral-tokens.md).

## Authentication

Send a **Scoped** API key in `Authorization: Bearer <sk-fish-...>` that carries:

1. `tokens:mint`, and
2. Every scope listed in the request's `scopes` array.

* **Classic** API keys are rejected with `403` (`code: "classic_key_cannot_mint"`).
* **One-time** `ek-fish-` tokens cannot mint new tokens (`403`, `code: "ephemeral_token_cannot_mint"`).
* Responses include `Cache-Control: no-store` and `Pragma: no-cache`.

## Constraints and rate limit

* **Rate limit:** Each team can mint up to **600 ephemeral tokens per minute**. Exceeding this limit returns `429` (`code: "rate_limit_exceeded"`) with `Retry-After: 60`.
* **Single-use only:** `single_use` defaults to `true`; passing `false` returns `400` (`multi_use_not_supported`).
* **Allowed scopes:** `tts:write`, `stt:write`, `voice_design:write`, `realtime:write`, and `agents:session` (reserved for gateway-routed agent sessions; REST `POST /v1/agent/sessions` uses a `sk-fish-` key).
* **`allowed_origins`:** Up to 20 exact `http://host[:port]` or `https://host[:port]` origins (no wildcards, credentials, paths, queries, fragments, or custom app schemes such as `capacitor://` or `tauri://`; max 256 characters each). Omit for native mobile or desktop clients that do not send an `Origin` header.
* **`max_session_duration_seconds`:** `60`–`14400` (default `3600`). Cannot extend an endpoint's own maximum session cap (for example, 3,600 seconds on `/v1/asr/realtime`); whichever limit is lower fires first.

## Errors

| Status | `code` | Cause |
| - | - | - |
| `400` | `multi_use_not_supported` | `single_use` was set to `false`. |
| `400` | `invalid_ephemeral_scope` | `scopes` contains a whitespace-only value or a scope outside the 5 allowed ephemeral scopes. |
| `400` | `invalid_allowed_origin` | An `allowed_origins` entry is not an exact `http://` or `https://` origin or contains wildcards, credentials, paths, queries, or fragments. |
| `401` | — | Missing, invalid, expired, or revoked `sk-fish-` API key. |
| `402` | `api_key_spend_limit_exceeded` | Parent API key reached its USD spend limit (`limit_usd`, `spent_usd`, `resets_at` included). |
| `402` | `insufficient_credit` | Team has no available API credit, has a payment hold, or billing scope is closed. |
| `403` | `classic_key_cannot_mint` | Caller authenticated with a Classic API key instead of a Scoped key. |
| `403` | `ephemeral_token_cannot_mint` | Caller authenticated with an `ek-fish-` ephemeral token. |
| `403` | `api_key_scope_forbidden` | Calling scoped key lacks `tokens:mint` or one or more requested `scopes` (`required_scope`, `required_scopes`, and `missing_scopes` when applicable). |
| `422` | — | Request body schema validation failed (for example, empty string in `scopes`, `ttl_seconds` out of range, >20 `allowed_origins` entries, origin >256 chars, or unknown field). |
| `429` | `rate_limit_exceeded` | Team exceeded 600 mint requests per minute; retry after `Retry-After` seconds. |

```bash cURL theme={null}
curl -X POST https://api.fish.audio/v1/auth/tokens \
  -H "Authorization: Bearer $FISH_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "scopes": ["stt:write"],
    "ttl_seconds": 120,
    "max_session_duration_seconds": 1800,
    "allowed_origins": ["https://app.example.com"],
    "end_user_id": "user_42"
  }'
```

```python Python theme={null}
import os
import requests

response = requests.post(
    "https://api.fish.audio/v1/auth/tokens",
    headers={
        "Authorization": f"Bearer {os.environ['FISH_API_KEY']}",
        "Content-Type": "application/json",
    },
    json={
        "scopes": ["stt:write"],
        "ttl_seconds": 120,
        "max_session_duration_seconds": 1800,
        "allowed_origins": ["https://app.example.com"],
        "end_user_id": "user_42",
    },
    timeout=10,
)
response.raise_for_status()
print(response.json())
```

```javascript JavaScript theme={null}
const response = await fetch("https://api.fish.audio/v1/auth/tokens", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.FISH_API_KEY}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    scopes: ["stt:write"],
    ttl_seconds: 120,
    max_session_duration_seconds: 1800,
    allowed_origins: ["https://app.example.com"],
    end_user_id: "user_42",
  }),
});
const data = await response.json();
console.log(data);
```

```json 201 theme={null}
{
  "token": "ek-fish-exampleTokenValueReturnedOnce",
  "token_id": "etk_4c9f8b2e7a1d4f0a9b3c6d5e8f1a2b3c",
  "expires_at": "2026-12-04T04:02:00Z",
  "expires_in": 120,
  "scopes": ["stt:write"],
  "single_use": true,
  "max_session_duration_seconds": 1800
}
```

```json 402 Spend limit exceeded theme={null}
{
  "code": "api_key_spend_limit_exceeded",
  "message": "API key spend limit exceeded for the current reset window.",
  "limit_usd": "25.00",
  "spent_usd": "25.04",
  "resets_at": "2026-12-05T00:00:00+00:00"
}
```

```json 403 Classic key theme={null}
{
  "code": "classic_key_cannot_mint",
  "message": "Classic API keys cannot mint ephemeral tokens. Create a scoped API key with the 'tokens:mint' scope instead."
}
```

```json 403 Missing scope theme={null}
{
  "code": "api_key_scope_forbidden",
  "required_scope": "stt:write",
  "required_scopes": ["stt:write"],
  "missing_scopes": ["stt:write"],
  "message": "This API key is missing required scope: stt:write."
}
```


## OpenAPI

````yaml post /v1/auth/tokens
openapi: 3.1.0
info:
  title: FishAudio OpenAPI
  version: '1'
servers:
  - description: Fish Audio API
    url: https://api.fish.audio
security: []
tags: []
paths:
  /v1/auth/tokens:
    post:
      tags:
        - Auth
      summary: Mint a short-lived single-use ephemeral token (`ek-fish-...`).
      description: |-
        Call this endpoint from your trusted backend using a **scoped** API key
        with `tokens:mint` and the inference/session scopes you want to delegate
        (classic API keys cannot mint ephemeral tokens). Hand the returned
        `ek-fish-...` token to an untrusted client (browser, mobile, desktop) to
        open a direct connection to Fish Audio inference endpoints such as
        `wss://api.fish.audio/v1/asr/realtime`.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/CreateEphemeralTokenRequest'
      responses:
        '201':
          description: Document created, URL follows
          headers: {}
          content:
            application/json:
              schema:
                properties:
                  token:
                    description: >-
                      Single-use ephemeral token plaintext (`ek-fish-...`).
                      Returned once.
                    title: Token
                    type: string
                  token_id:
                    description: Stable non-secret identifier (`etk_...`).
                    title: Token Id
                    type: string
                  expires_at:
                    description: >-
                      UTC timestamp after which this token can no longer be
                      redeemed.
                    format: date-time
                    title: Expires At
                    type: string
                  expires_in:
                    description: Remaining lifetime in seconds at issuance.
                    title: Expires In
                    type: integer
                  scopes:
                    description: Granted scopes for this ephemeral token.
                    items:
                      type: string
                    title: Scopes
                    type: array
                  single_use:
                    description: Whether this token is single-use.
                    title: Single Use
                    type: boolean
                  max_session_duration_seconds:
                    description: Maximum session duration in seconds once connected.
                    title: Max Session Duration Seconds
                    type: integer
                required:
                  - token
                  - token_id
                  - expires_at
                  - expires_in
                  - scopes
                  - single_use
                  - max_session_duration_seconds
                type: object
        '400':
          description: Bad request syntax or unsupported method
          headers: {}
          content:
            application/json:
              schema:
                properties:
                  status:
                    title: Status
                    type: integer
                  message:
                    title: Message
                    type: string
                  reason:
                    anyOf:
                      - type: string
                      - type: 'null'
                    default: null
                    title: Reason
                required:
                  - status
                  - message
                type: object
        '401':
          description: No permission -- see authorization schemes
          headers: {}
          content:
            application/json:
              schema:
                properties:
                  status:
                    title: Status
                    type: integer
                  message:
                    title: Message
                    type: string
                  reason:
                    anyOf:
                      - type: string
                      - type: 'null'
                    default: null
                    title: Reason
                required:
                  - status
                  - message
                type: object
        '402':
          description: No payment -- see charging schemes
          headers: {}
          content:
            application/json:
              schema:
                properties:
                  status:
                    title: Status
                    type: integer
                  message:
                    title: Message
                    type: string
                  reason:
                    anyOf:
                      - type: string
                      - type: 'null'
                    default: null
                    title: Reason
                required:
                  - status
                  - message
                type: object
        '403':
          description: Request forbidden -- authorization will not help
          headers: {}
          content:
            application/json:
              schema:
                properties:
                  status:
                    title: Status
                    type: integer
                  message:
                    title: Message
                    type: string
                  reason:
                    anyOf:
                      - type: string
                      - type: 'null'
                    default: null
                    title: Reason
                required:
                  - status
                  - message
                type: object
        '422':
          description: ''
          headers: {}
          content:
            application/json:
              schema:
                properties:
                  status:
                    title: Status
                    type: integer
                  message:
                    title: Message
                    type: string
                  reason:
                    anyOf:
                      - type: string
                      - type: 'null'
                    default: null
                    title: Reason
                required:
                  - status
                  - message
                type: object
        '429':
          description: >-
            The user has sent too many requests in a given amount of time ("rate
            limiting")
          headers: {}
          content:
            application/json:
              schema:
                properties:
                  status:
                    title: Status
                    type: integer
                  message:
                    title: Message
                    type: string
                  reason:
                    anyOf:
                      - type: string
                      - type: 'null'
                    default: null
                    title: Reason
                required:
                  - status
                  - message
                type: object
        '503':
          description: The server cannot process the request due to a high load
          headers: {}
          content:
            application/json:
              schema:
                properties:
                  status:
                    title: Status
                    type: integer
                  message:
                    title: Message
                    type: string
                  reason:
                    anyOf:
                      - type: string
                      - type: 'null'
                    default: null
                    title: Reason
                required:
                  - status
                  - message
                type: object
      security:
        - BearerAuth: []
components:
  schemas:
    CreateEphemeralTokenRequest:
      additionalProperties: false
      properties:
        scopes:
          description: >-
            Requested ephemeral inference/session scopes. Must be a non-empty
            subset of both the parent API key's scopes and allowed ephemeral
            scopes (`tts:write`, `stt:write`, `voice_design:write`,
            `realtime:write`, `agents:session`).
          items:
            maxLength: 64
            minLength: 1
            type: string
          maxItems: 16
          minItems: 1
          title: Scopes
          type: array
        single_use:
          default: true
          description: >-
            Whether the token is consumed atomically on its first connection.
            Phase 1 requires `true`.
          title: Single Use
          type: boolean
        ttl_seconds:
          default: 900
          description: >-
            Time-to-live before the token must be redeemed, in seconds (30-900,
            default 900).
          maximum: 900
          minimum: 30
          title: Ttl Seconds
          type: integer
        max_session_duration_seconds:
          default: 3600
          description: >-
            Maximum duration of a WebSocket session established with this token,
            in seconds (60-14400, default 3600).
          maximum: 14400
          minimum: 60
          title: Max Session Duration Seconds
          type: integer
        allowed_origins:
          anyOf:
            - items:
                maxLength: 256
                minLength: 1
                type: string
              maxItems: 20
              type: array
            - type: 'null'
          default: null
          description: >-
            Optional list of allowed browser `Origin` values (e.g.
            `https://app.example.com`). When set, redemption requires a matching
            `Origin`.
          title: Allowed Origins
        end_user_id:
          anyOf:
            - maxLength: 128
              type: string
            - type: 'null'
          default: null
          description: >-
            Optional customer-supplied end-user identifier for audit and
            attribution.
          title: End User Id
      required:
        - scopes
      title: CreateEphemeralTokenRequest
      type: object
  securitySchemes:
    BearerAuth:
      type: http
      scheme: bearer

````

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