Express

easy5 minexpress-server.ts

You want your agent behind a REST API with streaming in an existing Express app.

What it shows

  • Mount CogitatorServer on a router under /cogitator

The code

express-server.ts
import { Agent, Cogitator, calculator } from '@cogitator-ai/core';
import { CogitatorServer } from '@cogitator-ai/express';
import express from 'express';

const apiKey = process.env.GOOGLE_API_KEY;
if (!apiKey) throw new Error('Set GOOGLE_API_KEY');

const cog = new Cogitator({ llm: { providers: { google: { apiKey } } } });

const assistant = new Agent({
  name: 'assistant',
  description: 'General assistant with a calculator tool',
  model: 'google/gemini-3.5-flash-lite',
  instructions: 'You are a helpful assistant. Use tools when appropriate. Be concise.',
  tools: [calculator],
  temperature: 0.3,
});

const app = express();
const router = express.Router();

const server = new CogitatorServer({
  app: router,
  cogitator: cog,
  agents: { assistant },
  config: { basePath: '/cogitator', enableSwagger: false },
});
await server.init();
app.use(router);

app.listen(3100, () => console.log('Listening on http://localhost:3100/cogitator'));

Run it

install · bash
pnpm add @cogitator-ai/core @cogitator-ai/express express
EnvironmentGOOGLE_API_KEY
run · bash
GOOGLE_API_KEY=your-key npx tsx express-server.ts

Or run the full example from a clone of the repository (keys in .env):

repository · bash
npx tsx examples/integrations/01-express-server.ts

Try it

Try it · bash
curl http://localhost:3100/cogitator/agents
curl -X POST http://localhost:3100/cogitator/agents/assistant/run \
  -H 'Content-Type: application/json' -d '{"input": "What is 123 * 456?"}'
curl -N -X POST http://localhost:3100/cogitator/agents/assistant/stream \
  -H 'Content-Type: application/json' -d '{"input": "Explain recursion in one sentence"}'

Learn more