@agentdoor/next adapter provides both middleware-based and route-based approaches.
Installation
Quick Start (Edge Middleware)
Alternative: App Router Route Handlers
Instead of Edge middleware, you can use explicit route handlers:Complete Example
Here’s a full Next.js SaaS example with agent authentication:middleware.ts
app/api/data/route.ts
How It Works
The Next.js Edge middleware:- Serves discovery:
GET /.well-known/agentdoor.json - Handles registration:
POST /agentdoor/register(step 1)POST /agentdoor/register/verify(step 2)
- Handles auth:
POST /agentdoor/auth - Validates requests: Checks
Authorizationheaders on protected routes - Injects headers: Sets
x-agentdoor-*headers for downstream handlers
Advanced Configuration
Protected Paths
Customize which paths require agent authentication:Passthrough Mode
Allow unauthenticated requests to pass through:Custom Storage
Use persistent storage instead of in-memory:Webhooks & P1 Features
Request Headers
AgentDoor sets these headers on authenticated requests:x-agentdoor-authenticated:"true"or"false"x-agentdoor-agent-id: Agent IDx-agentdoor-agent: JSON-encoded agent contextx-agentdoor-reputation-warning: Present if reputation is below threshold
Helper Functions
getAgentContext(headers)
Extract agent context from request headers:
buildDiscoveryDocument(config)
Build a discovery document programmatically:
API Reference
createAgentDoorMiddleware(config)
Options:
scopes(required): Scope definitionsprotectedPaths: Path prefixes requiring auth (default:["/api/"])passthrough: Allow unauthenticated requests (default:false)store: Custom agent storejwt: JWT config ({ secret, expiresIn })webhooks,reputation,spendingCaps: P1 features
Route Handler Factories
createDiscoveryHandler(config): Discovery endpointcreateRegisterHandler(config): Registration step 1createVerifyHandler(config): Registration step 2createAuthHandler(): Auth endpointcreateRouteHandlers(config): All handlers combined