Authentication
Learn how to authenticate your requests to the AletheionGuard API using API keys.
Quick Start
AletheionGuard API uses simple API key authentication via the X-API-Key header. All API requests (except health checks) require a valid API key.
API Key Authentication
Simple and secure authentication for all types of applications. API keys are configured via environment variables.
How Authentication Works
Configure API Key
The API is configured with an AG_API_KEY_SECRET environment variable. This is the master API key that clients must provide.
Include in Requests
Clients include the API key in the X-API-Key header with every request.
Constant-Time Verification
The API uses secrets.compare_digest() for constant-time comparison to prevent timing attacks (SEC-002).
Note: If AG_API_KEY_SECRET is not configured, the API runs in open mode (no authentication required). This is useful for development but should never be used in production.
Using API Keys
cURL
Python (requests)
JavaScript/Node.js
Python (SDK)
Public Endpoints (No Auth Required)
The following endpoints do not require authentication:
| Endpoint | Description |
|---|---|
| GET / | Root endpoint - API information |
| GET /health | Health check - service status |
| GET /docs | OpenAPI/Swagger documentation |
| GET /openapi.json | OpenAPI schema |
| GET /metrics | Prometheus metrics |
Security Best Practices
✓ DO
- Store API keys in environment variables, never in source code
- Use different keys for development and production
- Configure
AG_API_KEY_SECRETin production - Use HTTPS for all API requests
- Add
.envto.gitignore - Rotate API keys periodically
- Monitor API usage for suspicious activity
✗ DON'T
- Hard-code API keys in your source code
- Commit API keys to version control (use .env and .gitignore)
- Share API keys via email or messaging apps
- Expose API keys in client-side JavaScript
- Run production without authentication enabled
- Use the same key across multiple independent deployments
Authentication Errors
Common authentication errors and how to resolve them:
401Unauthorized - Missing API Key
The X-API-Key header was not provided.
Solution: Include the X-API-Key header in your request with a valid API key.
403Forbidden - Invalid API Key
The provided API key does not match the configured secret.
Solution: Verify that your API key matches the AG_API_KEY_SECRET configured on the server. Check for typos or extra whitespace.
Environment Variables
Example configuration for different environments:
Client Configuration (.env)
Server Configuration (.env)
Important: Add .env to your .gitignore file to prevent accidentally committing secrets to version control.
Technical Implementation
Security Features
- •Constant-time comparison: Uses
secrets.compare_digest()to prevent timing attacks (SEC-002) - •Middleware-based: Authentication is enforced at the middleware level before reaching endpoints
- •Public endpoints: Health checks and docs are exempt from authentication
- •Optional mode: Can run without authentication for development (not recommended for production)
- •CORS protection: Configurable CORS origins via
CORS_ORIGINSenvironment variable (SEC-008)
Implementation: See src/aletheion_guard/security.py for the verify_api_key() function and src/aletheion_guard/api.py for the authentication middleware (lines 138-172).