Authentication
Authentication
Section titled “Authentication”Evolution API Lite uses a dual-layer API key authentication system to secure access to the API endpoints. This system provides both global administrative access and instance-specific access control.
Authentication Overview
Section titled “Authentication Overview”The API implements two types of authentication keys:
- Global API Key: Administrative access for creating instances and managing the system
- Instance Token: Instance-specific access for operations on individual WhatsApp connections
Global API Key
Section titled “Global API Key”Configuration
Section titled “Configuration”The global API key is configured via the AUTHENTICATION_API_KEY environment variable:
# Define a global apikey to access all instancesAUTHENTICATION_API_KEY=429683C4C977415CAAFCCE10F7D57E11The global API key must be included in the request headers for:
- Creating new instances (
/instance/create) - Fetching all instances (
/instance/fetchInstances) - Administrative operations
Header Format:
apikey: 429683C4C977415CAAFCCE10F7D57E11Example Request
Section titled “Example Request”curl -X POST http://localhost:8080/instance/create \ -H "Content-Type: application/json" \ -H "apikey: 429683C4C977415CAAFCCE10F7D57E11" \ -d '{ "instanceName": "my-instance", "integration": "WHATSAPP-BAILEYS" }'Instance Token
Section titled “Instance Token”How Instance Tokens Work
Section titled “How Instance Tokens Work”When you create a new instance, the system automatically generates a unique token for that instance. This token provides access to all operations specific to that instance.
Token Generation
Section titled “Token Generation”Instance tokens are automatically generated during instance creation and stored in the database. Each instance has its own unique token that cannot be changed after creation.
Instance tokens are used for all instance-specific operations:
- Sending messages
- Managing contacts and chats
- Group operations
- Instance settings and configuration
Header Format:
apikey: <instance-token>Finding Your Instance Token
Section titled “Finding Your Instance Token”You can retrieve your instance token by:
- During Creation: The token is returned in the instance creation response
- Fetch Instances: Use the global API key to fetch all instances and their tokens
# Fetch all instances with their tokenscurl -X GET http://localhost:8080/instance/fetchInstances \ -H "apikey: 429683C4C977415CAAFCCE10F7D57E11"Example Instance Operation
Section titled “Example Instance Operation”curl -X POST http://localhost:8080/message/sendText/my-instance \ -H "Content-Type: application/json" \ -H "apikey: <instance-token>" \ -d '{ "number": "5511999999999", "text": "Hello from Evolution API!" }'Authentication Flow
Section titled “Authentication Flow”1. Instance Creation Flow
Section titled “1. Instance Creation Flow”2. Instance Operation Flow
Section titled “2. Instance Operation Flow”Authentication Guard Implementation
Section titled “Authentication Guard Implementation”The authentication system is implemented in the auth.guard.ts file with the following logic:
Validation Priority
Section titled “Validation Priority”- Global API Key Check: First checks if the provided key matches the global API key
- Instance Token Check: If not global, validates against instance-specific tokens
- Context-Aware Validation: Different validation rules for different endpoints
Special Cases
Section titled “Special Cases”- Instance Creation: Requires global API key
- Fetch Instances: Accepts either global API key or valid instance token
- Instance Operations: Requires matching instance token
Configuration Options
Section titled “Configuration Options”Expose Instances
Section titled “Expose Instances”Control whether instance tokens are exposed in the fetch instances endpoint:
# If true, instance tokens are included in fetch instances responseAUTHENTICATION_EXPOSE_IN_FETCH_INSTANCES=trueWhen set to false, instance tokens are hidden from the fetch instances response for security.
Security Best Practices
Section titled “Security Best Practices”Global API Key Security
Section titled “Global API Key Security”- Store the global API key securely (environment variables, secrets management)
- Use different keys for different environments (development, staging, production)
- Rotate keys periodically
- Monitor access logs for unauthorized usage
Instance Token Security
Section titled “Instance Token Security”- Instance tokens are automatically generated and cannot be changed
- Each instance has a unique token
- Tokens are stored securely in the database
- Use HTTPS in production to protect tokens in transit
Network Security
Section titled “Network Security”- Always use HTTPS in production environments
- Consider implementing rate limiting
- Use proxy configurations when needed for additional security layers
- Monitor authentication failures and implement alerting
Error Responses
Section titled “Error Responses”Unauthorized (401)
Section titled “Unauthorized (401)”Returned when no API key is provided or the key is invalid:
{ "status": 401, "error": "Unauthorized", "response": { "message": "Unauthorized access" }}Forbidden (403)
Section titled “Forbidden (403)”Returned when the API key doesn’t have permission for the requested operation:
{ "status": 403, "error": "Forbidden", "response": { "message": "Missing global api key" }}Troubleshooting
Section titled “Troubleshooting”Common Issues
Section titled “Common Issues”- “Unauthorized” Error: Check that you’re including the
apikeyheader - “Forbidden” Error: Ensure you’re using the correct key type (global vs instance)
- Instance Not Found: Verify the instance name and token combination
- Token Mismatch: Ensure you’re using the correct instance token for the specific instance
Debugging Tips
Section titled “Debugging Tips”- Enable debug logging to see authentication attempts
- Verify your environment variables are loaded correctly
- Check database connectivity if instance token validation fails
- Ensure your HTTP client is sending headers correctly
This dual-layer authentication system provides both security and flexibility, allowing you to manage multiple WhatsApp instances while maintaining proper access control.