Express
Integrate Better Agent with Express.
Integrate Better Agent with Express by creating your app and mounting its HTTP handler.
Create the app
Start with a normal Better Agent server module.
import { betterAgent, defineAgent } from "@better-agent/core";
import { createOpenAI } from "@better-agent/providers/openai";
const openai = createOpenAI({
apiKey: process.env.OPENAI_API_KEY,
});
const assistant = defineAgent({
name: "assistant",
model: openai.text("gpt-5-mini"),
instruction: "You are a concise assistant. Keep replies short and natural.",
});
const agentApp = betterAgent({
agents: [assistant],
baseURL: "/api",
secret: "dev-secret",
});
export default agentApp;Mount the handler
Use the Express adapter to bridge Express request and response objects into Better Agent's standard handler.
import express from "express";
import { toExpressHandler } from "@better-agent/adapters/express";
import agentApp from "./server";
const app = express();
app.use("/api", toExpressHandler(agentApp));
app.listen(3000);Keep baseURL and the mounted route aligned. If the Better Agent app uses baseURL: "/api", mount it under /api.