Overview
Khoj supports multiple authentication methods:- API Tokens - For programmatic access (recommended for API usage)
- OAuth with Google - For user login via web interface
- Magic Links - Passwordless email authentication
authenticated scope and will redirect unauthenticated requests to the login page.
API Token Authentication
API tokens provide secure programmatic access to your Khoj account. This is the recommended method for API integrations.Generate an API Token
Generate a new API token for your account.Optional name for the token to help you identify it later
The generated API token (store this securely)
The name you assigned to the token
Example Response
Using API Tokens
Include your API token in theAuthorization header of your requests:
List Your Tokens
View all active API tokens for your account.cURL
Delete a Token
Revoke an API token when it’s no longer needed.cURL
The token string to delete
OAuth Authentication
Khoj supports OAuth 2.0 authentication with Google.OAuth Login Flow
- Initiate Login - Redirect user to
/api/auth/login - OAuth Redirect - User authenticates with Google
- Callback - Google redirects to
/api/auth/redirect - Session Created - User session established
Endpoints
GET /api/auth/login
Initiates the OAuth flow by redirecting to Google’s authorization page.
GET /api/auth/redirect
Callback endpoint for OAuth. Google redirects here after authentication.
Authorization code from Google
URL to redirect to after successful authentication
GET /api/auth/logout
Ends the user session.
OAuth Metadata
Retrieve OAuth configuration details.Magic Link Authentication
Magic links provide passwordless authentication via email.Send Magic Link
Request a magic link to be sent to your email.cURL
Email address to send the magic link to
200 OK if email was sent successfully
Rate limited to 10 requests per 24 hours per user and 20 requests per 24 hours per email.
Verify Magic Link
This endpoint is called automatically when the user clicks the magic link.Verification code from the magic link
Email address associated with the magic link
/) on success
Session Management
Khoj uses session cookies for web-based authentication. The session is stored in theuser field of the request session.
Check Authentication Status
Verify your authentication and get user info.Get User Information
Retrieve detailed information about the authenticated user.User’s email address
Username
URL to user’s profile photo
Whether the user has an active premium subscription
Whether the user has indexed any documents
Version of Khoj server
Example
Security Best Practices
Store Tokens Securely
Never commit tokens to version control. Use environment variables or secure secret management.
Anonymous Mode
Khoj can be run in anonymous mode with--anonymous-mode flag, which disables authentication entirely. This is only recommended for local development or single-user deployments.
