| title | Quickstart: Backend |
|---|---|
| description | How to setup the CopilotBackend endpoints |
To sign up to the Copilot Cloud waitlist click here. For now you must host your own Backend, which is straightforward:
Install the CopilotKit backend packagess:
```bash npm npm i @copilotkit/backend ``` ```bash yarn yarn add @copilotkit/backend ``` ```bash pnpm pnpm add @copilotkit/backend ```Remember to add any required environment variables. E.g. to use OpenAI:
OPENAI_API_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxximport { 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());
}Remember to add any required environment variables. E.g. to use OpenAI:
OPENAI_API_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxximport 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}`);See this repo for a full example: https://github.com/CopilotKit/copilotkit-firebase-demo
