skills/runtime/references/setup-endpoint.md
createCopilotRuntimeHandler is the strongly-preferred primitive. It returns a
(Request) => Promise<Response> that works in every fetch-native runtime and can be
delegated to from Express/Hono/Node. Avoid createCopilotExpressHandler and
createCopilotHonoHandler in new code.
Minimal runtime on any fetch server (Bun, Deno, Cloudflare Workers, Vercel Edge):
import {
CopilotRuntime,
createCopilotRuntimeHandler,
BuiltInAgent,
convertInputToTanStackAI,
} from "@copilotkit/runtime/v2";
import { chat } from "@tanstack/ai";
import { openaiText } from "@tanstack/ai-openai";
const runtime = new CopilotRuntime({
agents: {
default: new BuiltInAgent({
type: "tanstack",
factory: ({ input, abortController }) => {
const { messages, systemPrompts } = convertInputToTanStackAI(input);
return chat({
adapter: openaiText("gpt-4o"),
messages,
systemPrompts,
abortController,
});
},
}),
},
});
export const handler = createCopilotRuntimeHandler({
runtime,
basePath: "/api/copilotkit",
cors: true,
});
// Bun / Deno / Vercel Edge:
// Bun.serve({ fetch: handler });
// Deno.serve(handler);
// Cloudflare Workers:
// export default { fetch: handler };
// app/routes/api.copilotkit.$.tsx
import type { Route } from "./+types/api.copilotkit.$";
import {
CopilotRuntime,
createCopilotRuntimeHandler,
BuiltInAgent,
convertInputToTanStackAI,
} from "@copilotkit/runtime/v2";
import { chat } from "@tanstack/ai";
import { openaiText } from "@tanstack/ai-openai";
const runtime = new CopilotRuntime({
agents: {
default: new BuiltInAgent({
type: "tanstack",
factory: ({ input, abortController }) => {
const { messages, systemPrompts } = convertInputToTanStackAI(input);
return chat({
adapter: openaiText("gpt-4o"),
messages,
systemPrompts,
abortController,
});
},
}),
},
});
const handler = createCopilotRuntimeHandler({
runtime,
basePath: "/api/copilotkit",
});
export async function loader({ request }: Route.LoaderArgs) {
return handler(request);
}
export async function action({ request }: Route.ActionArgs) {
return handler(request);
}
// app/api/copilotkit/[...slug]/route.ts
import {
CopilotRuntime,
createCopilotRuntimeHandler,
BuiltInAgent,
convertInputToTanStackAI,
} from "@copilotkit/runtime/v2";
import { chat } from "@tanstack/ai";
import { openaiText } from "@tanstack/ai-openai";
const runtime = new CopilotRuntime({
agents: {
default: new BuiltInAgent({
type: "tanstack",
factory: ({ input, abortController }) => {
const { messages, systemPrompts } = convertInputToTanStackAI(input);
return chat({
adapter: openaiText("gpt-4o"),
messages,
systemPrompts,
abortController,
});
},
}),
},
});
const handler = createCopilotRuntimeHandler({
runtime,
basePath: "/api/copilotkit",
});
export const GET = handler;
export const POST = handler;
export const OPTIONS = handler;
Workers don't expose env at module scope, so build the runtime + handler lazily on the
first request and cache them in module-scoped variables. openaiText(model, config) does
NOT accept an apiKey in its config (it auto-reads OPENAI_API_KEY from env) — for an
explicit key, use createOpenaiChat(model, apiKey, config?).
// worker.ts
import {
CopilotRuntime,
createCopilotRuntimeHandler,
BuiltInAgent,
convertInputToTanStackAI,
} from "@copilotkit/runtime/v2";
import { chat } from "@tanstack/ai";
import { createOpenaiChat } from "@tanstack/ai-openai";
interface Env {
OPENAI_API_KEY: string;
}
type Handler = (request: Request) => Promise<Response>;
let handler: Handler | undefined;
function getHandler(env: Env): Handler {
if (handler) return handler;
const runtime = new CopilotRuntime({
agents: {
default: new BuiltInAgent({
type: "tanstack",
factory: ({ input, abortController }) => {
const { messages, systemPrompts } = convertInputToTanStackAI(input);
return chat({
adapter: createOpenaiChat("gpt-4o", env.OPENAI_API_KEY),
messages,
systemPrompts,
abortController,
});
},
}),
},
});
handler = createCopilotRuntimeHandler({
runtime,
basePath: "/api/copilotkit",
cors: true,
});
return handler;
}
export default {
fetch(request: Request, env: Env) {
return getHandler(env)(request);
},
};
Do not use createCopilotExpressHandler / createCopilotHonoHandler.
// Express — requires Node 18.17+ for Readable.fromWeb + fetch body: req
import express from "express";
import { Readable } from "node:stream";
import type { ReadableStream as WebReadableStream } from "node:stream/web";
import {
CopilotRuntime,
createCopilotRuntimeHandler,
} from "@copilotkit/runtime/v2";
const app = express();
const runtime = new CopilotRuntime({
agents: {
/* ... */
} as any,
});
const handler = createCopilotRuntimeHandler({
runtime,
basePath: "/api/copilotkit",
});
app.all("/api/copilotkit/*", async (req, res) => {
const url = new URL(req.url, `http://${req.headers.host}`);
// `body: req` + `duplex: "half"` lets us stream the Node IncomingMessage
// into a Web Request without buffering (Node 18.17+).
const webReq = new Request(url, {
method: req.method,
headers: req.headers as any,
body: ["GET", "HEAD"].includes(req.method!) ? undefined : req,
duplex: "half",
} as any);
const webRes = await handler(webReq);
res.status(webRes.status);
webRes.headers.forEach((v, k) => res.setHeader(k, v));
// Stream the response body through — required for SSE on
// /agent/*/run and /agent/*/connect. Buffering via arrayBuffer()
// would collapse the stream and deliver all events at end-of-stream.
if (webRes.body) {
Readable.fromWeb(webRes.body as unknown as WebReadableStream).pipe(res);
} else {
res.end();
}
});
app.listen(3000);
// Hono — already speaks Request/Response
import { Hono } from "hono";
import {
CopilotRuntime,
createCopilotRuntimeHandler,
} from "@copilotkit/runtime/v2";
const app = new Hono();
const runtime = new CopilotRuntime({
agents: {
/* ... */
} as any,
});
const handler = createCopilotRuntimeHandler({
runtime,
basePath: "/api/copilotkit",
});
app.all("/api/copilotkit/*", (c) => handler(c.req.raw));
export default app;
Multi-route mode (default) exposes: GET /info, POST /agent/:agentId/run,
GET /agent/:agentId/connect, POST /agent/:agentId/stop/:threadId, POST /transcribe,
GET/POST /threads, GET /threads/subscribe, PATCH /threads/:threadId,
POST /threads/:threadId/archive, DELETE /threads/:threadId,
GET /threads/:threadId/messages. Thread routes are only wired when Intelligence mode
is configured.
Single-route mode exposes a single POST basePath that accepts
{ method, params, body } envelopes — use when behind a strict reverse proxy.
Wrong:
import { createCopilotExpressHandler } from "@copilotkit/runtime/v2/express";
app.use(
"/api/copilotkit",
createCopilotExpressHandler({ runtime, basePath: "/api/copilotkit" }),
);
Correct:
import { Readable } from "node:stream";
import type { ReadableStream as WebReadableStream } from "node:stream/web";
import { createCopilotRuntimeHandler } from "@copilotkit/runtime/v2";
const handler = createCopilotRuntimeHandler({
runtime,
basePath: "/api/copilotkit",
});
app.all("/api/copilotkit/*", async (req, res) => {
// Requires Node 18.17+ (Readable.fromWeb + duplex: "half")
const webReq = new Request(new URL(req.url, `http://${req.headers.host}`), {
method: req.method,
headers: req.headers as any,
body: ["GET", "HEAD"].includes(req.method!) ? undefined : req,
duplex: "half",
} as any);
const webRes = await handler(webReq);
res.status(webRes.status);
webRes.headers.forEach((v, k) => res.setHeader(k, v));
// Stream, don't buffer — /agent/*/run is SSE.
if (webRes.body) {
Readable.fromWeb(webRes.body as unknown as WebReadableStream).pipe(res);
} else {
res.end();
}
});
The Express and Hono adapters are a discouraged surface — the maintainer flags them as "avoid at all costs." They pull in heavier dependencies, add framework binding, and make it harder to port. The fetch handler works from any Express/Hono route.
Source: packages/runtime/src/v2/runtime/core/fetch-handler.ts:1-27; maintainer Phase 4d.
Wrong:
app.use(createCopilotExpressHandler({ runtime }));
Correct:
const handler = createCopilotRuntimeHandler({
runtime,
basePath: "/api/copilotkit",
});
app.all("/api/copilotkit/*", (req, res) => {
/* delegate as shown above */
});
normalizeBasePath throws "basePath must be provided for Express endpoint" at mount time
and crashes the server.
Source: packages/runtime/src/v2/runtime/endpoints/express.ts:161.
Wrong:
// Cloudflare Worker
import { createCopilotHonoHandler } from "@copilotkit/runtime/v2/hono";
export default app;
Correct:
import { createCopilotRuntimeHandler } from "@copilotkit/runtime/v2";
const handler = createCopilotRuntimeHandler({
runtime,
basePath: "/api/copilotkit",
});
export default { fetch: (req: Request) => handler(req) };
Adapters bundle Node polyfills unnecessarily in fetch-native runtimes.
Source: packages/runtime/src/v2/runtime/core/fetch-handler.ts:1-27.
Wrong:
new CopilotRuntime({
agents,
beforeRequestMiddleware: async () =>
new Response("Unauthorized", { status: 401 }),
});
Correct:
const handler = createCopilotRuntimeHandler({
runtime,
basePath: "/api/copilotkit",
hooks: {
onRequest: ({ request }) => {
if (!request.headers.get("authorization")) {
throw new Response("Unauthorized", { status: 401 });
}
},
},
});
Only Request | void returns are honored. Any other return is ignored. Responses must be
thrown.
Source: packages/runtime/src/v2/runtime/core/fetch-handler.ts:148-156.
Wrong:
// handler = createCopilotRuntimeHandler({ mode: "single-route", ... })
fetch("/api/copilotkit/agent/x/run", {
method: "POST",
body: JSON.stringify(input),
});
Correct:
fetch("/api/copilotkit", {
method: "POST",
body: JSON.stringify({
method: "agent/run",
params: { agentId: "x" },
body: input,
}),
});
// On the client, pair with <CopilotKitProvider useSingleEndpoint />.
Single-route expects a POST envelope with { method, params, body }; URL-pattern calls 404.
Source: packages/runtime/src/v2/runtime/core/fetch-handler.ts:86-90,350-401.
Wrong:
import cors from "cors";
app.use(cors());
app.use(
createCopilotExpressHandler({ runtime, basePath, cors: { origin: "..." } }),
);
Correct:
// Pick one — handler's cors option OR your own cors(), not both:
const handler = createCopilotRuntimeHandler({
runtime,
basePath: "/api/copilotkit",
cors: { origin: "https://my.app" },
});
app.all("/api/copilotkit/*", (req, res) => {
/* delegate as above */
});
Both layers add CORS headers and the duplicates break strict browser enforcement.
Source: packages/runtime/src/v2/runtime/endpoints/express.ts:100-143.
Wrong:
import { CopilotRuntime } from "@copilotkit/runtime";
import { createCopilotRuntimeHandler } from "@copilotkit/runtime/v2";
Correct:
import {
CopilotRuntime,
createCopilotRuntimeHandler,
} from "@copilotkit/runtime/v2";
Both v1 and v2 APIs compile together but route through different implementations. Always
use the /v2 subpath in v2 code.
Source: packages/runtime/src/v2/index.ts.
copilotkit/middleware — hook lifecycle into this handlercopilotkit/agent-runners — pair with a persistent runner for productioncopilotkit/intelligence-mode — thread routes flip on when Intelligence is configured