Skip to main content

Overview

The authentication endpoint generates short-lived JWT tokens that can be used to authenticate WebSocket connections. This is the recommended authentication method for production use, as it provides time-limited access tokens instead of exposing long-lived API keys.

Endpoint

Authentication

This endpoint requires authentication via the X-API-Key header:
string
required
Your API key configured in the backend’s API_KEY environment variable.

Request

No request body is required. Authentication is handled entirely through the header.

Example Request

Response

Success Response (200)

string
A JWT token encoded using HS256 algorithm. Valid for 5 minutes from issuance.
integer
Token expiration time in seconds (always 300 for 5 minutes).

Error Response (401)

Returned when the API key is missing, invalid, or doesn’t match the configured value.

JWT Token Structure

The generated JWT token contains the following payload:
integer
Expiration timestamp (Unix timestamp). Token expires 5 minutes after issuance.
integer
Issued-at timestamp (Unix timestamp). When the token was created.
string
Token type, always "websocket" for WebSocket authentication.

Example Decoded Token

Token Usage

Once you have a token, use it to authenticate WebSocket connections:

Complete Authentication Flow

Token Expiration

Tokens are valid for 5 minutes (300 seconds) from the time of issuance. This is configured in /backend/jwt_auth.py:6.
Tokens cannot be refreshed. When a token expires, you must request a new one from the /auth/token endpoint.

Handling Expiration

Security Considerations

Why Use JWT Tokens?

  1. Time-limited access: Tokens expire after 5 minutes, reducing the window of exposure if compromised
  2. Separation of concerns: API keys are only sent to the auth endpoint, not the WebSocket
  3. Audit trail: Each token has an issuance timestamp for tracking
  4. Revocation: Changing the API key invalidates all existing tokens

Token Security Best Practices

  1. Never expose tokens in URLs that might be logged (use WSS protocol, query params are encrypted)
  2. Store API keys securely using environment variables or secret managers
  3. Use HTTPS/WSS for all connections to prevent token interception
  4. Rotate API keys periodically to invalidate old tokens
  5. Request new tokens for each WebSocket session rather than reusing

Configuration

The authentication system requires the following environment variable:
Generate a secure API key:

Error Codes

Alternative Authentication

If you don’t want to use JWT tokens, you can authenticate directly with the API key on the WebSocket:
Direct API key authentication is not recommended for production as it exposes your long-lived credentials on every connection.

Implementation Details

The token generation is implemented in /backend/jwt_auth.py:8-14:
  • Algorithm: HS256 (HMAC with SHA-256)
  • Secret: Your API_KEY environment variable
  • Expiry: 5 minutes from issuance
  • Claims: exp, iat, type
Token validation happens in /backend/jwt_auth.py:16-23 and is called by the WebSocket endpoint at /backend/main.py:94.

Health Check Endpoint

A simple health check endpoint for monitoring the API status.

Endpoint

Authentication

No authentication required.

Response

string
Always returns "ok" when the API is operational.

Example Request

Response Example

Use Cases

  • Kubernetes/ECS health checks: Use as a liveness probe
  • Load balancer health checks: Configure ALB to ping this endpoint
  • Monitoring: Set up uptime monitoring services
  • CI/CD validation: Verify deployment succeeded

Implementation

Implemented in /backend/main.py:25-27: