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 theX-API-Key header:
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)
A JWT token encoded using HS256 algorithm. Valid for 5 minutes from issuance.
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:Expiration timestamp (Unix timestamp). Token expires 5 minutes after issuance.
Issued-at timestamp (Unix timestamp). When the token was created.
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.
Handling Expiration
Security Considerations
Why Use JWT Tokens?
- Time-limited access: Tokens expire after 5 minutes, reducing the window of exposure if compromised
- Separation of concerns: API keys are only sent to the auth endpoint, not the WebSocket
- Audit trail: Each token has an issuance timestamp for tracking
- Revocation: Changing the API key invalidates all existing tokens
Token Security Best Practices
- Never expose tokens in URLs that might be logged (use WSS protocol, query params are encrypted)
- Store API keys securely using environment variables or secret managers
- Use HTTPS/WSS for all connections to prevent token interception
- Rotate API keys periodically to invalidate old tokens
- Request new tokens for each WebSocket session rather than reusing
Configuration
The authentication system requires the following environment variable:Error Codes
| Status Code | Description | Reason |
|---|---|---|
| 200 | Success | Valid API key, token generated |
| 401 | Unauthorized | Missing or invalid X-API-Key header |
| 500 | Internal Server Error | Token generation failed (rare) |
Alternative Authentication
If you don’t want to use JWT tokens, you can authenticate directly with the API key on the WebSocket:Implementation Details
The token generation is implemented in/backend/jwt_auth.py:8-14:
- Algorithm: HS256 (HMAC with SHA-256)
- Secret: Your
API_KEYenvironment variable - Expiry: 5 minutes from issuance
- Claims:
exp,iat,type
/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
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:
Related Endpoints
- WebSocket Crawl - Use tokens to authenticate WebSocket connections