Skip to main content

API Key Authentication

switchAILocal requires clients to authenticate using API keys. Requests must include the Authorization: Bearer <key> header.

Configuring API Keys

config.yaml
array
required
List of valid API keys for client authentication. All requests to /v1/* endpoints require a key from this list.
Example client request:
Store API keys securely. Do not commit them to version control. Use environment-based configuration for production deployments.

Key Rotation

API keys support hot reload. To rotate keys:
  1. Add new key to api-keys list
  2. Save config.yaml
  3. switchAILocal automatically reloads (no restart needed)
  4. Update clients to use new key
  5. Remove old key from config

Management Access Control

The Management API provides dashboard access and control endpoints. It has separate authentication and access controls.
config.yaml

Management Secret Key

string
Management authentication key. All management requests require this key. Leave empty to disable Management API entirely (404 for all management routes).
Automatic hashing: When you set a plaintext key:
switchAILocal automatically hashes it with bcrypt on first startup:
The hashed value is persisted back to config.yaml automatically. Future startups skip re-hashing.

Remote Access Control

boolean
default:"false"
Allow remote (non-localhost) access to management endpoints. When false, only localhost (127.0.0.1, ::1) can access management routes, even with valid key.
Localhost-only (default):
  • ✅ Requests from 127.0.0.1 with valid key → Allowed
  • ❌ Requests from 192.168.1.100 with valid key → Denied (403)
Remote access enabled:
  • ✅ Requests from any IP with valid key → Allowed
Enabling allow-remote: true exposes management endpoints to the network. Ensure you use a strong secret key and TLS.

Management Control Panel

boolean
default:"false"
Disable the bundled management control panel asset download and HTTP route. When true, management UI is not served.
string
GitHub repository URL for management panel assets. Override to use custom dashboard.
Disable dashboard UI:

Disabling Management API

To completely disable all management endpoints:
With empty secret key:
  • Management dashboard returns 404
  • All /api/management/* routes return 404
  • Client API (/v1/*) continues working normally

WebSocket Authentication

Control authentication for WebSocket API:
config.yaml
boolean
default:"true"
Enable authentication for WebSocket API (/v1/ws). Requires same API keys as REST endpoints.
Disabling WebSocket authentication (ws-auth: false) allows unauthenticated access to streaming endpoints. Only disable in trusted environments.

TLS/HTTPS Configuration

Enable HTTPS for encrypted transport:
config.yaml
boolean
default:"false"
Enable HTTPS server mode with TLS certificates
string
Path to TLS certificate file (PEM format)
string
Path to TLS private key file (PEM format)

Production TLS Example

With Let’s Encrypt:
With custom CA:
TLS certificate changes require a server restart. Hot reload does not apply to TLS settings.

Authentication Directory

Configure storage location for authentication tokens:
config.yaml
string
default:"~/.switchailocal"
Directory for OAuth tokens, session data, and authentication cache. Supports ~ for home directory.
This directory contains:
  • tokens.json - OAuth refresh tokens (Gemini, etc.)
  • Session state for authenticated providers
  • Authentication cache
Ensure this directory has restricted permissions (chmod 700) to prevent token theft.

Security Best Practices

1. Use Strong API Keys

Generate strong keys:

2. Enable TLS in Production

3. Restrict Management Access

4. Secure Authentication Directory

5. Rotate Keys Regularly

Set up a key rotation schedule:
  • Client API keys: Rotate every 90 days
  • Management secret key: Rotate every 180 days
  • OAuth tokens: Automatically refreshed by switchAILocal

6. Use Environment-Specific Configs

Firewall Configuration

When deploying with allow-remote: true, configure firewall rules:

Complete Security Example

config.yaml

Troubleshooting

Problem: Requests return 401 UnauthorizedSolutions:
  • Verify Authorization: Bearer <key> header is included
  • Check key is in api-keys list
  • Ensure config.yaml has been reloaded
  • Check for typos in API key
Problem: Management endpoints return 404Solutions:
  • Ensure remote-management.secret-key is not empty
  • Check if disable-control-panel: true is set
  • Verify you’re accessing the correct endpoint
Problem: Management requests from network IPs return 403Solutions:
  • Set remote-management.allow-remote: true
  • Verify firewall allows traffic on the port
  • Ensure correct management secret key is used

Next Steps

Server Configuration

Configure host, port, logging, and core settings

Provider Setup

Add API keys for cloud and local AI providers