The implementation guide on this page shows you how to use access tokens in browsers and use API keys on trusted servers.Documentation Index
Fetch the complete documentation index at: https://docs.cartesia.ai/llms.txt
Use this file to discover all available pages before exploring further.
| Environment | Recommended auth | Why |
|---|---|---|
| Browser app | Access token | Keeps your API key out of client code |
| Trusted server, script, or notebook | API key | Simpler and does not require token exchange |
Server-side apps: use API keys
Use your API key directly from trusted environments such as backend services, jobs, local scripts, and notebooks. Cartesia accepts bothX-Api-Key and Authorization headers for server-side authentication. Header names are case-insensitive, so X-API-KEY and authorization work too.
Browser apps: use access tokens
Never ship a Cartesia API key in browser code. A user can extract it and make requests on your account. Instead, generate a short-lived access token on your server and return that token to the browser.Prerequisites
Before implementing Access Tokens:- Configure your server with a Cartesia API key
- Implement user authentication in your application
- Establish secure client-server communication
Available Grants
Access Tokens support granular permissions through grants. Both TTS and STT grants are optional: TTS Grant: Withgrants: { tts: true }, clients have access to:
/tts/bytes- Synchronous TTS generation streamed with chunked encoding/tts/sse- Server-sent events for streaming/tts/websocket- WebSocket-based streaming
grants: { stt: true }, clients have access to:
/stt/websocket- WebSocket-based speech-to-text streaming/stt- Batch speech-to-text processing/audio/transcriptions- OpenAI-compatible transcription endpoint
grants: { agent: true }, clients have access to:
- the Agents websocket calling endpoint
Implementation Guide
1. Token Generation (Server-side)
Make a request to generate a new access token using cURL or the official client libraries:2. Token Storage (Client-side)
Store the token securely, such as setting HTTP-only cookie with matching token expiration. The cookie should behttpOnly, secure, and sameSite: "strict".
3. Making Authenticated Requests Using the API
4. Token Refresh Strategy
Before each API call, check whether the current token is still valid. If it has expired or is about to expire, request a new one from your server before proceeding.Security Best Practices
Server-side
- Generate tokens server-side only — never in browser code
- Use short token lifetimes (minutes)
- Serve tokens over HTTPS exclusively
Client-side
- Store tokens in HTTP-only cookies (cookies the browser sends automatically but JavaScript cannot read)
- Enable
secureandsameSite: "strict"cookie flags - Never store tokens in
localStorageorsessionStorage - Never log tokens or display them in the UI
Additional Resources
- API Reference - Access Token generation endpoint documentation