kagi search
Search Kagi and return structured results in JSON or human-readable format. This is the primary command for retrieving search results from your terminal.

Synopsis
Description
Thekagi search command performs a search query against Kagi’s search index and returns results in a structured format. By default, output is JSON suitable for programmatic processing. With the --pretty flag, results are formatted for human readability.
This command has sophisticated authentication behavior: it prefers the API token path when available but can automatically fall back to the session token path if the API path is rejected. This ensures maximum utility regardless of which token type you have configured.
Authentication
Base Search (No Lens)
Preferred:KAGI_API_TOKEN
Fallback: KAGI_SESSION_TOKEN (if API path is rejected)
The base search command is unique in that it attempts both authentication paths:
- First attempt: Uses
KAGI_API_TOKENif available, querying the Search API - On API rejection: Falls back to
KAGI_SESSION_TOKEN, querying via the web product - On success: Returns results from whichever path succeeded
Lens Search (--lens)
Required: KAGI_SESSION_TOKEN
Lens-aware search requires a subscriber session token. This feature is not available via the Search API and requires web-product authentication.
What is a lens? Kagi lenses are custom search scopes you create in your Kagi account. They allow you to search within specific domains, topics, or content types you’ve defined.
Options
<QUERY> (Required)
The search query string.
Constraints:
- Minimum length: 1 character
- Maximum length: 2000 characters
- Supports all standard search operators
--pretty
Render output in human-readable format instead of JSON.
Default: false (JSON output)
When to use: Interactive terminal sessions, quick lookups, reading results
JSON Output (default):
--pretty):
--pretty only changes output formatting. It does not affect:
- Which authentication is used
- Transport method (API vs web)
- Command semantics
- Error behavior
--lens <INDEX>
Search using a specific Kagi lens.
Requires: KAGI_SESSION_TOKEN
Type: String value representing your numeric lens index
Example: --lens 2
Finding your lens index:
- Log into Kagi at kagi.com
- Navigate to your lenses in settings
- The lens index is the number shown for each lens
Output Format
JSON Structure
Result Types
| Type | Description |
|---|---|
| 0 | Organic search result |
| 1 | News result |
| 2 | Image result |
| 3 | Video result |
| 4 | Map/local result |
Pretty Output Format
Results are numbered and formatted with title, URL, and optional snippet:Examples
Basic Search
Lens Search
Advanced Queries
Processing Results
Combining with Other Tools
Exit Codes
| Code | Meaning |
|---|---|
| 0 | Success - search completed, results returned |
| 1 | Error - see stderr for details |
- Missing credentials
- Invalid authentication
- Network failure
- Rate limiting
Authentication Flow Details
Base Search Flow
Lens Search Flow
Performance Considerations
Response Times
- Base search: 1-3 seconds typical
- Lens search: 2-5 seconds typical
- With fallback: May take 2x time if fallback occurs
Rate Limiting
- Respect rate limits between requests
- Add delays when processing multiple queries:
sleep 1 - Consider caching results for repeated queries
Best Practices
- Use appropriate token: If you have both, base search uses API first
- Pretty for humans: Use
--prettyfor interactive use - JSON for automation: Default JSON is perfect for piping
- Lens for focus: Use lenses to scope searches to relevant domains
- Limit results: Use
--limitto reduce payload size
Troubleshooting
”No results found”
- Try a broader query
- Check your internet connection
- Verify authentication is working:
kagi auth check
”missing credentials”
- Set your session token:
kagi auth set --session-token '...' - Or set API token:
kagi auth set --api-token '...' - Check status:
kagi auth status
”auth check failed”
- Token may be expired - regenerate in Kagi settings
- Check token hasn’t been revoked
- Verify subscription/API credit status
Slow responses
- May be experiencing fallback behavior
- Check network connectivity
- Consider rate limiting delays
Comparison with Other Commands
| Feature | kagi search | kagi news | kagi smallweb |
|---|---|---|---|
| Authentication | Optional (API or Session) | None | None |
| Output | JSON or pretty | JSON | JSON |
| Source | Search index | News feed | Small Web |
| Requires token | No (public) | No | No |
Related Commands
kagi news- Search news articles specificallykagi enrich web- Web enrichment with different APIkagi auth- Manage authenticationkagi assistant- AI-powered assistance
See Also
- Authentication Guide - Detailed token setup
- Workflows - Real-world usage patterns
- Troubleshooting - Common issues
- Auth Matrix - Command auth requirements