Skip to main content

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 the requireAgentAuth middleware in /home/daytona/workspace/source/backend/src/middleware/auth.ts:65:
  1. Extract Agent-Key header from request
  2. Validate key format (must start with agt_)
  3. Hash the key and lookup in database
  4. Check if agent is active
  5. Return agent ID and user ID

Authentication Flow

Using Agent-Key in Requests

Header Format

Include the Agent-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 the agent_services join table. The requireServiceAccess middleware checks authorization:

Access Check Flow

  1. Agent authenticates with Agent-Key
  2. Request targets a specific service
  3. System checks agent_services table for relationship
  4. Returns 403 Forbidden if agent lacks access

Creating Agent Keys

Agent keys are created via the dashboard API (requires JWT authentication):

Create Agent

The full apiKey is only returned once during creation. Store it securely - it cannot be retrieved again.

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
The created agent record
number
Unique agent identifier
string
Agent name
string
Truncated key for identification (e.g., agt_...cdef)
boolean
Whether the agent key is active. Defaults to true.
string | null
ISO timestamp of last API usage. null if never used.
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

Set isActive to false to revoke access:

Delete Agent

Permanently delete an agent and all service associations:

Error Responses

Missing Agent-Key Header

Invalid Key Format

Agent keys must start with agt_ prefix.

Invalid or Revoked Key

Returned when:
  • Key doesn’t exist in database
  • Agent’s isActive is false

Insufficient Service Access

The agent key is valid but not authorized for the requested service.

Security Best Practices

  • Use environment variables or secret managers
  • Never commit keys to version control
  • Rotate keys if exposed
  • Only grant access to required services
  • Create separate agents for different environments
  • Use descriptive names for easy identification
  • Check lastUsedAt timestamps 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