Build a chatbot
A website chat whose replies come from a Gasket agent, with the key kept on your server.
The browser never talks to Gasket. It talks to your backend, which holds GASKET_API_KEY and calls the agent. Conversation history lives in your app and is sent with each message; Gasket runs are stateless.
request path
Browser → your backend (holds the key) → POST /v1/agents/:id/runs → Gasket → agent → tools → replyYour backend
// app/api/chat/route.ts
export async function POST(req: Request) {
const { messages } = await req.json(); // [{ role: "user" | "assistant", content }]
const res = await fetch(`https://usegasket.xyz/api/v1/agents/${process.env.GASKET_AGENT_ID}/runs`, {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.GASKET_API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({ messages }),
});
const run = await res.json();
if (run.status === "completed") return Response.json({ reply: run.output.text });
if (run.status === "requires_approval") return Response.json({ reply: "A teammate needs to approve that first.", runId: run.id });
return Response.json({ reply: run.error?.message ?? "Something went wrong." }, { status: 502 });
}Your page
chat.js (browser)
const history = [];
async function send(text) {
history.push({ role: "user", content: text });
const { reply } = await fetch("/api/chat", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ messages: history }),
}).then((r) => r.json());
history.push({ role: "assistant", content: reply });
return reply;
}A complete example
The Gasket repository includes examples/support-chat: a single-file Node server and page you can run with GASKET_API_KEY and GASKET_AGENT_ID set. Every message you send appears under Requests.
NoteNever put an API key in browser code. Anyone can read it there.
Something wrong or missing? Tell us · Stuck? Help center