Overview
Agent-Key authentication is designed for AI agents making API calls to protected services. Each agent has a unique API key that can be scoped to specific services.How It Works
Agent keys are validated by therequireAgentAuth middleware in /home/daytona/workspace/source/backend/src/middleware/auth.ts:65:
- Extract
Agent-Keyheader from request - Validate key format (must start with
agt_) - Hash the key and lookup in database
- Check if agent is active
- Return agent ID and user ID
Authentication Flow
Using Agent-Key in Requests
Header Format
Include theAgent-Key header in every request to protected endpoints:
string
required
Your agent’s API key. Must start with
agt_ prefix.Service-Based Access Control
Agent keys are scoped to specific services via theagent_services join table. The requireServiceAccess middleware checks authorization:
Access Check Flow
- Agent authenticates with Agent-Key
- Request targets a specific service
- System checks
agent_servicestable for relationship - Returns
403 Forbiddenif agent lacks access
Creating Agent Keys
Agent keys are created via the dashboard API (requires JWT authentication):Create Agent
Request Body
string
required
Human-readable name for the agent (e.g., “Production Bot”, “Staging Agent”)
number[]
required
Array of service IDs this agent can access. Pass an empty array
[] for no service access.Response Fields
object
string
Full API key - only returned during creation. Store this securely.
array
List of services this agent can access
Managing Agent Keys
List All Agents
Update Agent Services
Change which services an agent can access:Revoke Agent Key
SetisActive to false to revoke access:
Delete Agent
Permanently delete an agent and all service associations:Error Responses
Missing Agent-Key Header
Invalid Key Format
agt_ prefix.
Invalid or Revoked Key
- Key doesn’t exist in database
- Agent’s
isActiveisfalse
Insufficient Service Access
Security Best Practices
Store Keys Securely
Store Keys Securely
- Use environment variables or secret managers
- Never commit keys to version control
- Rotate keys if exposed
Scope Agent Access
Scope Agent Access
- Only grant access to required services
- Create separate agents for different environments
- Use descriptive names for easy identification
Monitor Usage
Monitor Usage
- Check
lastUsedAttimestamps regularly - Revoke unused agents
- Review service access periodically
Implementation Reference
Source code locations:- Agent authentication middleware:
backend/src/middleware/auth.ts:65 - Service access check:
backend/src/middleware/auth.ts:112 - Agent management routes:
backend/src/routes/agents.ts - Key hashing utility:
backend/src/utils/apikey.ts