Authentication Guide
Authentication is the foundation of the kagi CLI’s functionality. This comprehensive guide explains the two credential types, how they’re used, when each is required, and how to configure them securely.Authentication Overview
The kagi CLI supports two distinct credential types that unlock different capabilities:Session Token (KAGI_SESSION_TOKEN)
What it is: A token from your Kagi Session Link that authenticates you as a Kagi subscriber.
Where it comes from: Generated in your Kagi account settings as a Session Link URL.
What it unlocks:
- Subscriber-only web product features
- Lens-aware search (
kagi search --lens) - Kagi Assistant (
kagi assistant) - Subscriber Summarizer (
kagi summarize --subscriber) - Base search via the web product path
API Token (KAGI_API_TOKEN)
What it is: A token for Kagi’s documented public APIs.
Where it comes from: Generated in your Kagi account settings under API.
What it unlocks:
- Public API endpoints
- FastGPT (
kagi fastgpt) - Public Summarizer (
kagi summarizewithout--subscriber) - Web/News Enrichment (
kagi enrich) - Base search via the Search API (if you have API access)
Visual Authentication Flow
Getting Your Tokens
Obtaining a Session Token
Your Session Token is available in your Kagi account settings:- Log into Kagi at kagi.com
- Navigate to Settings by clicking your profile picture
- Select “Account Settings” from the dropdown
- Scroll to “Session Link” section
- Copy the full URL that looks like:
- This token authenticates as you - protect it like a password
- Anyone with this token can access your Kagi account features
- Don’t commit it to version control
- Don’t share it in public forums or chat
- Rotate it periodically if you suspect compromise
Obtaining an API Token
Your API Token is available separately in API settings:- Log into Kagi at kagi.com
- Navigate to Settings
- Select “API Settings” or “API”
- Generate a new token if you don’t have one
- Copy the token (long alphanumeric string)
- API access requires available credit
- Monitor your usage in Kagi settings
- API calls consume credit per request
- Different endpoints may have different costs
Token Lifecycle and Management
Token Validity
| Token Type | Lifetime | Renewal |
|---|---|---|
| Session Token | Long-lived (months) | Automatic via Kagi web session |
| API Token | Long-lived (months) | Manual regeneration in settings |
Token Rotation
When to rotate:- Suspected compromise
- Regular security maintenance (every 3-6 months)
- After team member departure
- After changing Kagi account password
- Generate new token in Kagi settings
- Update your configuration with the new token
- Test that new token works
- Revoke old token in Kagi settings
- Remove old token from any saved locations
Configuration Methods
The kagi CLI supports two configuration methods with clear precedence:Method 1: Environment Variables (Highest Priority)
Environment variables take precedence over the config file, making them ideal for:- CI/CD pipelines
- Docker containers
- Temporary testing
- Overriding file configuration
Method 2: Configuration File
The.kagi.toml file provides persistent configuration:
Location:
./.kagi.tomlin the current working directory
Precedence Rules
Credentials are resolved in this order (first match wins):- Environment variables override config file values
- You can temporarily test different tokens via env vars
- CI/CD can inject tokens without modifying files
- File configuration provides defaults, env vars provide overrides
Authentication Verification
Checking Status
See which credentials are configured:Validating Credentials
Test that configured credentials actually work:- Loads credentials using precedence rules
- Attempts a test search
- Reports which token was used
- Confirms authentication succeeds
auth check is intentionally strict and does not use the base-search fallback behavior. It tests the primary credential only.
Command Authentication Requirements
No Authentication Required
These commands work without any tokens:kagi news- Public news feedkagi news --list-categories- News categorieskagi news --chaos- Chaos indexkagi smallweb- Small Web feedkagi auth status- Check configuration
Session Token Required
These commands requireKAGI_SESSION_TOKEN:
kagi search --lens <INDEX>- Lens-aware searchkagi assistant- AI Assistantkagi summarize --subscriber- Subscriber Summarizer- Base search (if no API token configured)
API Token Required
These commands requireKAGI_API_TOKEN:
kagi fastgpt- FastGPT querieskagi enrich web- Web enrichmentkagi enrich news- News enrichmentkagi summarize(without--subscriber) - Public Summarizer- Base search (preferred path when API token available)
Dual Token Support
Base search (kagi search without --lens) supports both tokens:
- Prefers API token when available
- Falls back to session token if API search is rejected
- Enables seamless operation regardless of which token you have
Search-Specific Authentication Behavior
Thekagi search command has special authentication logic:
Normal Operation
Fallback Behavior
The base search command implements intelligent fallback: Scenario 1: API token worksSecurity Best Practices
Token Storage
DO:- ✅ Store tokens in
./.kagi.tomlwith 600 permissions - ✅ Use environment variables in CI/CD secrets
- ✅ Use secret managers (1Password, Vault, etc.)
- ✅ Rotate tokens periodically
- ❌ Commit tokens to version control
- ❌ Hardcode tokens in scripts
- ❌ Share tokens in public channels
- ❌ Store tokens in plain text notes
- ❌ Include tokens in bug reports or logs
Shell History
Be aware thatexport KAGI_TOKEN=... commands may be saved to shell history:
Prevent history logging:
CI/CD Security
GitHub Actions:Docker Security
Avoid in Dockerfile:Team Sharing
For shared development environments:- Each developer has their own tokens
- Use environment-specific config (not committed)
- Document setup in README (without actual tokens)
- Use secret managers for shared CI/CD
.env.example (safe to commit):
.env (NEVER commit):
Troubleshooting Authentication
”missing credentials” Error
Cause: Command requires authentication but no valid token found. Solutions:- Check current status:
kagi auth status - Verify token is set:
echo $KAGI_SESSION_TOKEN - Set token if missing:
kagi auth set --session-token '...' - Check correct variable name (SESSION not SESSSION)
“auth check failed” Error
Cause: Token is set but invalid, expired, or revoked. Solutions:- Verify token in Kagi settings
- Check token hasn’t expired
- Regenerate if necessary
- Update configuration with new token
”this command requires KAGI_SESSION_TOKEN”
Cause: Attempting to use subscriber features with only API token. Solutions:- Get Session Token from Kagi settings
- Set session token:
kagi auth set --session-token '...' - Verify:
kagi auth status
”this command requires KAGI_API_TOKEN”
Cause: Attempting to use API features without API token. Solutions:- Get API Token from Kagi settings
- Check you have API credit available
- Set API token:
kagi auth set --api-token '...' - Verify:
kagi auth status
Token Not Being Used from Config File
Cause: Environment variable is overriding file. Check:Session Link URL Not Working
Cause: CLI expects either full URL or just token. Solution:Advanced Configuration
Platform-Specific Configuration
macOS Keychain (alternative storage):Multiple Profiles
For users with multiple Kagi accounts:Summary
Key takeaways:- Two token types: Session (subscriber features) and API (paid API features)
- Clear precedence: Environment variables override config files
- Smart fallback: Base search tries API first, falls back to session
- Security first: Never commit tokens, rotate regularly, use secret managers
- Easy verification: Use
kagi auth statusandkagi auth check
- Session Token = Personal subscription features
- API Token = Paid API endpoints
- Both can coexist, each serves different commands
- Base search is smart about which to use
Next Steps
- Auth Matrix - Complete command-to-token mapping
- Workflows - Real-world authentication patterns
- Troubleshooting - More auth debugging help
- Support and Contribution - Safe handling and contribution notes
Keep your tokens secure and happy searching!