API Keys and Bearer Tokens allow programmatic access to your application. Perfect for integrations, mobile apps, and automated scripts.
Perfect for: Third-party integrations, mobile apps, CI/CD scripts, and automated workflows.
# Enable API key management in user settings
NEXT_PUBLIC_BETTER_AUTH_TOKEN_MANAGEMENT=trueAPI key management automatically appears in user settings at /[locale]/account/api-keys.

Long-lived tokens for applications
Characteristics:
Best for: Backend integrations, scheduled jobs, third-party services
User visits /[locale]/account/api-keys in their account settings.
Click "Generate New API Key" button.
API key is generated and displayed once only. User must copy and store it securely.

Important: API keys are shown only once during creation. If lost, the key must be deleted and recreated.
User can:
Security features:
API Key Generation Interface:
# Using API key in Authorization header
curl -H "Authorization: Bearer your-api-key-here" \
https://yourdomain.com/api/protected-endpoint
# Alternative: Using API key header
curl -H "X-API-Key: your-api-key-here" \
https://yourdomain.com/api/protected-endpointBearer tokens are typically obtained through login and used for subsequent API calls:
// 1. Login to get bearer token
const loginResponse = await fetch('/api/auth/login', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ email, password })
});
const { token } = await loginResponse.json();
// 2. Use bearer token for API calls
const apiResponse = await fetch('/api/user/profile', {
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json'
}
});The token system uses Better Auth's apiKey and bearer plugins:
// In src/lib/better-auth/auth.ts
apiKey() // Enables API key generation and management// In src/lib/better-auth/auth.ts
bearer() // Enables JWT bearer token support// Example API route protection
import { auth } from '@/lib/better-auth/auth'
export async function GET(request: Request) {
const session = await auth.api.getSession({ headers: request.headers })
if (!session) {
return new Response('Unauthorized', { status: 401 })
}
// API logic here
return Response.json({ data: 'protected data' })
}Built-in security features:
API Key Security:
Bearer Token Security:
Key Management:
The boilerplate includes standard API endpoints for token management:
// Token management endpoints
POST /api/auth/api-key/create // Create new API key
GET /api/auth/api-key/list // List user's API keys
DELETE /api/auth/api-key/revoke // Delete specific key
// Bearer token endpoints
POST /api/auth/login // Get bearer token via login
POST /api/auth/refresh // Refresh bearer token
POST /api/auth/logout // Invalidate bearer token"Invalid API key" error?
"Token expired" error?
API key management not visible?
NEXT_PUBLIC_BETTER_AUTH_TOKEN_MANAGEMENT=true/[locale]/account/api-keys// Store API key securely in mobile app
const API_KEY = await SecureStore.getItemAsync('api_key');
const fetchUserData = async () => {
const response = await fetch('/api/user/profile', {
headers: {
'Authorization': `Bearer ${API_KEY}`,
'Content-Type': 'application/json'
}
});
return response.json();
};# GitHub Actions example
- name: Deploy with API
env:
API_KEY: ${{ secrets.API_KEY }}
run: |
curl -H "Authorization: Bearer $API_KEY" \
-X POST https://yourdomain.com/api/deploy
All working? Your application now supports secure programmatic access via API keys and bearer tokens!