Access Tokens are used in contexts like web apps which should not be bundled with an API key. For
trusted contexts like server applications, local scripts, or iPython notebooks, you should simply
use API keys.
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: { tts: true, stt: true }
Implementation Guide
1. Token Generation (Server-side)
Make a request to generate a new access token:Example Implementation
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
4. Token Refresh Strategy
Proactively refresh the token in your app before they expire.Security Best Practices
Essential Guidelines
- ✅ Generate tokens server-side only
- ✅ Use short token lifetimes (minutes)
- ✅ Implement automatic token refresh
- ✅ Store tokens in HTTP-only cookies
- ✅ Enable secure and SameSite cookie flags
Security Don’ts
- ❌ Never store tokens in localStorage/sessionStorage
- ❌ Never log tokens or display them in the UI
- ❌ Never transmit tokens over non-HTTPS connections
Token Lifecycle Management
- Generate new token upon user authentication
- Implement automatic refresh before expiration
- Handle token expiration gracefully
Additional Resources
- API Reference - Access Token generation endpoint documentation