Overview
Kortix API supports two authentication methods:- API Keys: For server-to-server communication and long-lived integrations
- JWT Tokens: For user sessions and web applications
API Key Authentication
API keys consist of a public key (pk_) and secret key (sk_) pair. They are ideal for:
- Server-to-server integrations
- CI/CD pipelines
- Long-running automation
- Microservices
API Key Format
Public key starting with
pk_ (32 random characters)Secret key starting with
sk_ (32 random characters)core/services/api_keys.py:115-133
Using API Keys
Include the API key in theX-API-Key header:
API Key Validation
The validation process includes:- Format Check: Validates
pk_:sk_format - Database Lookup: Finds key by public_key
- Hash Verification: HMAC-SHA256 comparison (constant-time)
- Status Check: Ensures key is
active - Expiration Check: Validates
expires_attimestamp
core/services/api_keys.py:335-456
Security Features
HMAC-SHA256 Hashing
Secret keys are hashed using HMAC-SHA256 for fast, secure verification:core/services/api_keys.py:139-150
Constant-Time Comparison
Prevents timing attacks:core/services/api_keys.py:152-167
Redis Caching
Validation results are cached for 2 minutes:core/services/api_keys.py:356-379, 458-473
API Key Lifecycle
Storage
The secret key is hashed and stored securely. You will only see the plain secret key once during creation.
API Key Status
Key is valid and can be used
Key has been manually revoked
Key has passed its expiration date
core/services/api_keys.py:29-32
JWT Token Authentication
JWT (JSON Web Token) authentication is used for:- Web application user sessions
- Mobile app authentication
- Temporary access grants
- Server-Sent Events (SSE) streaming
JWT Algorithms
Kortix supports two JWT signing algorithms:ES256 (Recommended)
Elliptic Curve Digital Signature Algorithm with P-256 curve:- Uses Supabase JWKS (JSON Web Key Set) endpoint
- Fetches public keys dynamically
- Caches JWKS for 1 hour
- More secure than HS256
core/utils/auth_utils.py:25-126
core/utils/auth_utils.py:53
HS256 (Legacy)
HMAC-SHA256 symmetric signing:- Uses shared secret (
SUPABASE_JWT_SECRET) - Faster verification
- Supported for backwards compatibility
core/utils/auth_utils.py:217-258
Using JWT Tokens
Include the JWT token in theAuthorization header:
JWT Verification Process
Decode Header
Extract algorithm (Source:
alg) and key ID (kid) without verificationcore/utils/auth_utils.py:159-162Fetch Public Key
For ES256 tokens, fetch JWKS and extract public keySource:
core/utils/auth_utils.py:174-175Verify Signature
Validate token signature using public key (ES256) or secret (HS256)Source:
core/utils/auth_utils.py:177-187Streaming Authentication
For Server-Sent Events (SSE), which cannot set custom headers:JWT token passed as query parameter for EventSource compatibility
core/utils/auth_utils.py:502-549
Authentication Flow
The API checks both authentication methods in order: Source:core/utils/auth_utils.py:396-492
Account-Based Access Control
All resources in Kortix are scoped to accounts:- API Keys → Linked to an account_id
- JWT Tokens → User is primary owner of account
- Resources → Owned by account (threads, agents, etc.)
Account Lookup
API keys resolve to users via account ownership:core/utils/auth_utils.py:361-394
Admin Authentication
Admin endpoints require a special admin API key:core/utils/auth_utils.py:128-148
Best Practices
API Keys
API Keys
- Store API keys in environment variables, never in code
- Use different keys for development, staging, and production
- Set expiration dates for keys (max 365 days)
- Rotate keys regularly
- Revoke compromised keys immediately
- Monitor
last_used_atfor suspicious activity
JWT Tokens
JWT Tokens
- Always verify signatures (never use unverified tokens)
- Check expiration timestamps
- Use short-lived tokens for sensitive operations
- Refresh tokens before expiration
- Store tokens securely (httpOnly cookies for web apps)
Security
Security
- Use HTTPS in production
- Implement rate limiting on your side
- Log authentication failures
- Monitor for brute force attempts
- Never expose secret keys in client-side code
Error Handling
See the Errors page for authentication error codes:401 Unauthorized: Invalid or missing credentials403 Forbidden: Valid credentials but insufficient permissions404 Not Found: API key or user not found
Next Steps
Error Reference
Learn about error codes and handling
API Introduction
Return to API overview