apikey header. The system supports two levels of authentication: global API keys and per-instance tokens.
Authentication Architecture
Authentication is handled by the auth guard middleware (src/api/guards/auth.guard.ts:10-51) which validates every protected request.
Two-Level Authentication
Evolution API implements a hierarchical authentication model:- Global API Key: Administrative access for creating and managing all instances
- Instance Tokens: Scoped access for operations on specific instances
This dual-key system provides security through least-privilege access. Use instance tokens for client applications and reserve the global key for administrative operations.
Global API Key
The global API key provides unrestricted access to all instances and administrative operations.Configuration
Set the global API key using theAUTHENTICATION_API_KEY environment variable:
.env
Using the Global API Key
Include the global API key in theapikey header:
Global Key Permissions
The global API key grants access to:- Instance creation:
POST /instance/create - Fetch all instances:
GET /instance/fetchInstances - All instance operations: Full CRUD on any instance
- Administrative endpoints: Metrics, health checks, configuration
Instance Tokens
Each instance has a unique token that provides scoped access to only that instance’s operations.Token Generation
Instance tokens are set during instance creation:A unique identifier for this instance. This becomes the instance’s authentication token.Requirements:
- Must be unique across all instances
- Recommended: Use UUID or similar cryptographically random strings
- Avoid predictable patterns for security
Using Instance Tokens
Once an instance is created, use its token for all instance-specific operations:Token Scope and Isolation
Instance tokens are validated against the database (src/api/guards/auth.guard.ts:30-35):
Authentication Flow
The authentication guard implements the following flow (src/api/guards/auth.guard.ts:10-51):
Authentication Priority
- Global API Key: Checked first, grants immediate access if valid
- Instance Token: Checked if global key doesn’t match
- Database Validation: Instance tokens are verified against the database
Per-Instance Access Control
Evolution API enforces strict per-instance access control:Instance Name Validation
All instance-specific endpoints require theinstanceName parameter:
src/api/abstract/abstract.router.ts:34).
Database-Level Isolation
All database queries include instance-scoped filters:This architecture ensures complete data isolation between tenants, even in the event of application-level bugs.
Common Authentication Scenarios
Scenario 1: Administrative Dashboard
Use the global API key for administrative interfaces:Scenario 2: Client Application
Provide instance tokens to client applications:Scenario 3: Multi-Instance Management
Use the global key to manage multiple instances:Authentication Error Responses
Evolution API returns specific error responses for authentication failures:Missing API Key
Request:src/exceptions/401.exception.ts:3-11 for the exception implementation.
Invalid API Key
Request:Wrong Instance Token
Request:Missing Global Key for Administrative Operations
Request:src/exceptions/403.exception.ts:3-11 for the Forbidden exception.
Verifying Credentials
Test your authentication credentials using the verify endpoint:src/api/routes/index.router.ts:207-216.
Security Best Practices
1. Secure API Key Generation
Generate cryptographically secure API keys:2. Environment Variable Management
Never commit API keys to version control:3. Key Rotation
Implement regular key rotation:- Global keys: Rotate quarterly or after team changes
- Instance tokens: Rotate when clients request or after security events
- Process: Create new key → update clients → revoke old key
4. HTTPS Only
Always use HTTPS in production to prevent key interception:5. IP Allowlisting
Restrict API access to known IP addresses at the infrastructure level (reverse proxy, firewall, or API gateway).6. Monitor Authentication Failures
Log and alert on repeated authentication failures:7. Instance Token Management
Control instance token visibility:false, the /instance/fetchInstances endpoint will not return instance tokens in the response.
Next Steps
Error Handling
Learn about authentication error codes and troubleshooting
Create Instance
Create your first WhatsApp instance
API Overview
Return to API overview
Security
Read the full security documentation