NestJS

Mount Better Agent through Nest's underlying HTTP adapter. This example uses Nest with Express.

Install

npm install @better-agent/adapters

Service

// server.service.ts
import { Injectable } from "@nestjs/common";
import { betterAgent, defineAgent } from "@better-agent/core";
import { openai } from "@better-agent/openai";

@Injectable()
export class ServerService {
  readonly app = betterAgent({
    agents: [
      defineAgent({
        name: "support",
        model: openai("gpt-5.5"),
        instruction: "You help customers.",
      }),
    ],
    basePath: "/api/agents",
  });
}

Bootstrap

// main.ts
import { NestFactory } from "@nestjs/core";
import { toExpressHandler } from "@better-agent/adapters/express";
import type { Express } from "express";
import { AppModule } from "./app.module";
import { ServerService } from "./server.service";

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  const expressApp = app.getHttpAdapter().getInstance() as Express;
  const serverService = app.get(ServerService);

  expressApp.use("/api/agents", toExpressHandler(serverService.app));

  await app.listen(3000);
}

bootstrap();

Module

import { Module } from "@nestjs/common";
import { ServerService } from "./server.service";

@Module({
  providers: [ServerService],
})
export class AppModule {}

Client

import { createClient } from "@better-agent/client";
import type { ServerService } from "./server.service";

type App = ServerService["app"];

export const client = createClient<App>({
  baseURL: "http://localhost:3000/api/agents",
});

Auth and headers

Headers from the underlying Express request are forwarded to Better Agent. Use Auth to resolve identity from cookies, bearer tokens, or API keys.

Features

The mounted route supports runs, streams, threads, interrupts, client tools, and approvals. Use Client from your frontend to call it.

Fastify

If your Nest app uses Fastify, mount Better Agent with toFastifyHandler instead.

Next

See Client, Tools, Human in the Loop, Memory, Events, and Storage.