Setup

Setup the CopilotKit Backend Endpoint:

To sign up to the Copilot Cloud waitlist click here. For now you must host your own Backend, which is straightforward:

Install CopilotKit Backend packages

Install the CopilotKit backend packagess:

npm i @copilotkit/backend

Manually specified endpoint: Next.js edge function (typescript)

Remember to add any required environment variables. E.g. to use OpenAI:

.env.local
OPENAI_API_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
/api/copilotkit/openai/route.ts (Next.js)
import { CopilotBackend, OpenAIAdapter } from "@copilotkit/backend";

export const runtime = "edge";

export async function POST(req: Request): Promise<Response> {
  const copilotKit = new CopilotBackend();

  return copilotKit.response(req, new OpenAIAdapter());
}

Manually specified endpoint: Node.js (typescript)

Remember to add any required environment variables. E.g. to use OpenAI:

.env.local
OPENAI_API_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
index.ts (Node.js)
import http from "http";
import { CopilotBackend, OpenAIAdapter } from "@copilotkit/backend";

const HEADERS = {
  // make sure to modify CORS headers to match your frontend's origin
  "Access-Control-Allow-Origin": "http://localhost:3000",
  "Access-Control-Allow-Methods": "POST, OPTIONS",
  "Access-Control-Allow-Headers": "Content-Type",
};

const server = http.createServer((request, response) => {
  try {
    const headers = {
      ...HEADERS,
      ...(request.method === "POST" && { "Content-Type": "application/json" }),
    };
    response.writeHead(200, headers);
    if (request.method == "POST") {
      const copilotKit = new CopilotBackend();
      const openaiAdapter = new OpenAIAdapter();
      copilotKit.streamHttpServerResponse(request, response, openaiAdapter);
    } else {
      response.end("openai server");
    }
  } catch (err) {
    console.error(err);
    response.end("error");
  }
});

const port = 4201;
const host = "localhost";
server.listen(port, host);
console.log(`Listening at http://${host}:${port}`);

Firebase (GCP) Cloud Function (typescript)

See this repo for a full example: https://github.com/CopilotKit/copilotkit-firebase-demo