Setting an API Key
Add an API key to your configuration file:~/.openfang/config.toml
API keys are stored in plaintext in
config.toml. Use strong, randomly-generated keys and protect the config file with restrictive permissions (chmod 600 ~/.openfang/config.toml).Making Authenticated Requests
Include the API key as a Bearer token in theAuthorization header:
No Authentication Mode
Ifapi_key is empty or not set in the config, the API is publicly accessible without authentication:
~/.openfang/config.toml
CORS Behavior
With Authentication
CORS is restricted to localhost origins:http://127.0.0.1:4200http://localhost:4200http://127.0.0.1:8080http://localhost:8080http://localhost:3000
Without Authentication
CORS is restricted to the same localhost origins as above. This prevents malicious websites from making cross-origin requests to your local OpenFang instance.Public Endpoints
The following endpoints do not require authentication even when an API key is set:GET /api/health— Basic health check (redacted status)GET /— WebChat UIGET /logo.png— Logo imageGET /favicon.ico— Favicon
Channel Identity Resolution
When messages arrive from channel adapters (Telegram, Discord, etc.), OpenFang maps the sender’s platform identity to an agent:- User ID mapping — Each channel message includes a
user_id(Telegram user ID, Discord user ID, etc.) - Agent binding — You can configure which agent should respond to a specific user:
~/.openfang/config.toml
- Dynamic binding — Use the bindings API to map users to agents:
WebSocket Authentication
WebSocket connections inherit HTTP authentication. When connecting to:Sec-WebSocket-Protocol header:
The current implementation accepts API keys via query parameters for WebSocket convenience, but this may change in future versions. Prefer header-based auth when possible.
RBAC (Role-Based Access Control)
OpenFang currently implements a single-role system: either you have the API key (full access) or you don’t (no access). Future versions will support:- Read-only keys — List agents, view sessions, but no mutations
- Agent-scoped keys — Access specific agents only
- Channel-specific keys — For webhook integrations
- Temporary tokens — Time-limited access for external services
Security Best Practices
Use strong, random API keys
Use strong, random API keys
Generate API keys with at least 32 characters of entropy:Or use a password manager to generate a cryptographically secure key.
Rotate keys regularly
Rotate keys regularly
Change your API key periodically (every 90 days recommended):
- Update
api_keyinconfig.toml - Restart the daemon:
openfang restart - Update all clients with the new key
Restrict config file permissions
Restrict config file permissions
On Unix systems:This prevents other users from reading your API key.
Use environment variables for keys
Use environment variables for keys
Instead of hardcoding keys in Then set the env var:
config.toml, reference environment variables:Never expose to public internet without auth
Never expose to public internet without auth
If you must expose OpenFang publicly:
- Always set an API key
- Use a reverse proxy with TLS (nginx, Caddy)
- Add firewall rules to restrict access
- Consider adding HTTP basic auth at the proxy layer for defense-in-depth
Example: Authenticated Session
Next Steps
Agents API
Manage agent lifecycles
Workflows API
Orchestrate multi-agent tasks
