01Overview
This page covers the REST, Server-Sent Events and WebSocket routes served by the NestJS integration.
AgenticModule.forRoot registers the REST controller and the WebSocket gateway. Set enableRestApi: false or enableWebSocket: false to leave out either one.
02REST endpoints
The REST routes are enabled with enableRestApi. Successful responses use the envelope { success, data }. The execute route also returns conversationId beside data, and the delete route returns message in place of data.
GET /agentsGET /agents/:namePOST /agents/:name/executeDELETE /agents/:name/conversations/:conversationIdGET /agents/:name/stream03List agents
- Each item in data has name, description, model, provider and tools (the tool names).
GET /agents HTTP/1.1
Host: localhost:300004Get an agent
- data has name, description, model, provider, systemPrompt, tools (name and description for each), temperature, maxTokens and maxIterations.
- An unknown name throws an error. See Error handling.
GET /agents/customer-support HTTP/1.1
Host: localhost:300005Execute an agent
- Body fields (ExecuteAgentDto): input (required), conversationId, userId, sessionData, history, metadata, outputFormat and formatOptions.
- data is the agent response: content, toolCalls, metadata (tokensUsed, latencyMs, iterations, cost) and finishReason.
POST /agents/customer-support/execute HTTP/1.1
Host: localhost:3000
Content-Type: application/json
{
"input": "I need help with my order #12345",
"conversationId": "conv-user-123",
"userId": "user-123",
"sessionData": { "orderNumber": "12345" },
"outputFormat": "markdown"
}Note
06Delete a conversation
The response is { success: true, message: 'Conversation conv-user-123 deleted' }.
DELETE /agents/customer-support/conversations/conv-user-123 HTTP/1.1
Host: localhost:3000Warning
07Server-Sent Events
The stream route sends each agent event as a message whose data is the event object as JSON. Events arrive in the order the agent produces them.
data: {"type":"iteration","iteration":1}
data: {"type":"content","content":"I'd be happy to","delta":true}
data: {"type":"done","metadata":{"tokensUsed":450,"iterations":1}}08SSE event types
iterationcontenttool_callstool_resultdoneerror09Stream route method
The stream route is declared with @Sse, which NestJS registers as GET. The handler reads input from the request body, but a browser EventSource cannot send a body. Check the method and body your client uses before you build on it.
GET /agents/customer-support/stream HTTP/1.1
Host: localhost:3000
Accept: text/event-stream
Content-Type: application/json
{"input": "Hello!"}Note
10WebSocket
The gateway serves the /agents namespace. Connect with Socket.IO. The gateway allows any origin ('*'). Restrict it before deploying.
import { io } from 'socket.io-client';
const socket = io('http://localhost:3000/agents');
let text = '';
socket.on('stream', (event) => {
if (event.type === 'content') text += event.content;
});
socket.on('complete', ({ conversationId }) => console.log(conversationId, text));
socket.on('error', ({ message }) => console.error(message));
socket.emit('execute', { agentName: 'customer-support', input: 'I need help with my order' });11Client events
executegetAgentlistAgents12Server events
streamcompleteagentInfoagentListerror13Authentication
Neither the REST routes nor the WebSocket gateway apply a guard by default. Requests reach them without authentication unless your app adds guards.
The package includes TenantGuard, which reads the API key from x-api-key and the tenant from x-tenant-id by default. It needs a tenant setup, described in the multi-tenancy guide.
import { Controller, UseGuards } from '@nestjs/common';
import { TenantGuard } from '@lov3kaizen/agentsea-nestjs';
@Controller('agents')
@UseGuards(TenantGuard)
export class ProtectedAgentsController {}Warning
14Rate limiting
Use RateLimit on a route and RateLimitGuard on the class or method. RateLimit takes maxRequests, windowMs and an optional keyExtractor. The default key is the request IP. A request over the limit gets HTTP 429 with the message 'Rate limit exceeded'. The built-in routes do not apply the guard.
import { Controller, Param, Post, UseGuards } from '@nestjs/common';
import { RateLimit, RateLimitGuard } from '@lov3kaizen/agentsea-nestjs';
@Controller('custom-agents')
export class CustomAgentsController {
@Post(':name/execute')
@UseGuards(RateLimitGuard)
@RateLimit({ maxRequests: 10, windowMs: 60000 })
run(@Param('name') name: string) {
return { name };
}
}Note
15Error handling
Unknown agentRate limit exceededRun fails, SSEUnknown agent, WebSocketRun fails, WebSocketWarning
16Request validation
The DTOs use class-validator decorators. Whether invalid bodies are rejected depends on a validation pipe in your app. The package does not set up a validation pipe. Confirm how your app handles invalid bodies.