Authentication Guide
Authentication is the foundation of the kagi CLI’s functionality. This comprehensive guide explains the three credential types, how they’re used, when each is required, and how to configure them securely.Recommended Setup Path
On a real terminal, the default setup path is:Session Link, API Key, or Legacy API Token, points you to the correct Kagi settings page, accepts a paste, saves into ./.kagi.toml, and validates the selected credential immediately.
Use kagi auth set ... only when you want a non-interactive or scripted flow.
Authentication Overview
The kagi CLI supports three 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) - Session-backed search options such as lenses, relative time windows, ordering, verbatim mode, and personalization
- Kagi Quick Answer (
kagi quick) - Kagi Assistant (
kagi assistant) - Custom assistant management (
kagi assistant custom) - Ask Page (
kagi ask-page) - Lens management (
kagi lens) - Custom bang management (
kagi bang custom) - Redirect rule management (
kagi redirect) - Kagi Translate (
kagi translate) - Subscriber Summarizer (
kagi summarize --subscriber) - Base search via the web product path
API Key (KAGI_API_KEY)
What it is: A key for Kagi’s current documented /api/v1 APIs.
Where it comes from: Generated in the Kagi API dashboard at https://kagi.com/api/keys.
What it unlocks:
- Search API (
kagi searchwhen[auth.preferred_auth] = "api") - Extract API (
kagi extract)
Authorization: Bearer <key>
Cost: Requires available API credit (separate from subscription)
Legacy API Token (KAGI_API_TOKEN)
What it is: A token for Kagi’s older /api/v0 APIs still used by some CLI commands.
Where it comes from: Generated in older Kagi API settings.
What it unlocks:
- FastGPT (
kagi fastgpt) - Public Summarizer (
kagi summarizewithout--subscriber) - Web/News Enrichment (
kagi enrich)
Authorization: Bot <token>
Cost: Requires available API credit (separate from subscription)
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
- Open Session Link settings
- Copy the full Session Link 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 Key
Your API Key is available separately in the Kagi API dashboard:- Log into Kagi at kagi.com
- Open API keys
- Generate a new key if you don’t have one
- Copy the key (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 Key | Long-lived (months) | Manual regeneration in the API dashboard |
| Legacy 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 credential 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 search --time ...,--order ...,--verbatim, personalization flags - session-backed search optionskagi search --region ...,--from-date ...,--to-date ...- V1 Search API filters when routed throughKAGI_API_KEY, or subscriber filters when routed throughKAGI_SESSION_TOKENkagi assistant- AI Assistantkagi assistant custom- saved assistant CRUDkagi quick- Quick Answerkagi ask-page- Ask Assistant about one pagekagi lens- lens CRUD and enable/disablekagi bang custom- custom bang CRUDkagi redirect- redirect rule CRUD and enable/disablekagi translate- Kagi Translate text modekagi summarize --subscriber- Subscriber Summarizer- Base search (if no API key is configured)
API Key Required
These commands requireKAGI_API_KEY:
kagi extract- Extract API- Base search when
[auth.preferred_auth] = "api"
Legacy 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
Session and API Key Search Support
Base search (kagi search without --lens or runtime filters) supports session tokens and API keys:
- Defaults to session unless
[auth.preferred_auth] = "api" - Falls back to session token only when API-first mode is enabled and the Search API rejects the request
- Requires an API key for API-first Search API mode
Search-Specific Authentication Behavior
Thekagi search command has special authentication logic:
Normal Operation
Base Search Behavior
The base search command follows the configured base-search preference. Scenario 1: Default session-first behaviorSecurity Best Practices
Token Storage
DO:- ✅ Use
kagi authfor local setup so the CLI validates the credential as you save it - ✅ 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 authorkagi 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 an API key or legacy API token. Solutions:- Get Session Token from Kagi settings
- Set session token:
kagi authorkagi auth set --session-token '...' - Verify:
kagi auth status
”this command requires KAGI_API_TOKEN”
Cause: Attempting to use legacy/api/v0 API features without a legacy API token.
Solutions:
- Get a legacy API Token from Kagi settings
- Check you have API credit available
- Set API token:
kagi authorkagi auth set --api-token '...' - Verify:
kagi auth status
”base search requires KAGI_API_KEY”
Cause: Attempting to use API-first base search with only a legacy API token. Solutions:- Get an API Key from the Kagi API dashboard
- Set API key:
kagi authorkagi auth set --api-key '...' - Or set a session token and use the default session-backed search path
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, prefer named profiles in one.kagi.toml:
Summary
Key takeaways:- Three credential types: Session (subscriber features), API Key (current
/api/v1features), and Legacy API Token (older/api/v0features) - Best local setup path:
kagi auth - Clear precedence: Environment variables override config files
- Configured search preference: Base search defaults to session unless
[auth.preferred_auth] = "api" - Security first: Never commit tokens, rotate regularly, use secret managers
- Easy verification: Use
kagi auth statusandkagi auth check
- Session Token = Personal subscription features
- API Key = Current
/api/v1endpoints - Legacy API Token = Older
/api/v0endpoints - All can coexist, each serves different commands
- Base search follows the configured search preference
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!