Authentication Methods
1. API Key (Simplest)
UseX-API-Key header for REST API calls.
Generate API Key:
X-API-Key: ak-a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4s9t0u1v2w3x4y5z6API-Key: ak-a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4s9t0u1v2w3x4y5z6Authorization: ApiKey ak-a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4s9t0u1v2w3x4y5z6
- CLI tools
- Service-to-service authentication
- Simple scripts
- Development/testing
- Treat as a password
- Rotate periodically
- Revoke when compromised
- Scope to specific API keys (read-only vs full access)
2. Bearer Token (OAuth)
UseAuthorization: Bearer header with JWT token.
Get Token (OAuth Device Flow):
- User-initiated workflows
- Web applications
- Mobile apps
- Delegated access
3. Service Account Token
For server-to-server authentication with extended expiry. Create Service Account:- Automation and CI/CD
- Server-to-server communication
- Scheduled jobs
- Long-lived integrations
MCP Gateway Authentication
MCP Gateways require OAuth token or API key. Connect via Bearer Token:A2A Gateway Authentication
A2A Gateways require OAuth token. WebSocket Connection:Token Management
Expiration and Refresh
Tokens expire after set duration:Token Revocation
Revoke tokens when no longer needed:Checking Token Status
Validate token before use:Authentication Examples
Python
JavaScript/TypeScript
cURL
Security Best Practices
Protect API Keys
- ❌ Don’t commit to version control
- ✅ Use
.envor secrets management - ❌ Don’t hardcode in code
- ✅ Load from environment
- ❌ Don’t log or display
- ✅ Rotate regularly
Token Scope
Use minimal scopes:TLS/HTTPS
Always use HTTPS:Audit Access
Monitor authentication:Troubleshooting
“Invalid API Key”- Check key starts with
ak- - Verify key hasn’t been revoked
- Regenerate if unsure
- Refresh using refresh_token
- Re-authenticate if expired
- Check token/key scope
- Ensure account has access
- Contact admin if needed