Edge API
The Edge API could be used to validate HTTP requests, authorize access, and dispatch
messages to a Session. This src/api.ts example sends a prompt to the
Session runtime:
src/api.ts
import { defineApi } from "@cantelop/sdk/api";
type Message = { prompt: string };
export default defineApi<Message>(({ app, router }) => {
router.route("POST", "/chat", async ({ request }) => {
const body = await request.json() as {
prompt: string;
sessionId?: string;
};
const workspace = await app.workspaces.open({ slug: "default" });
const session = app.sessions.open({
...(body.sessionId === undefined ? {} : { id: body.sessionId }),
workspaceId: workspace.id,
keepAliveSeconds: 300,
});
const message = await session.dispatch({ prompt: body.prompt });
return Response.json({ sessionId: session.id, message }, { status: 202 });
});
});
Omit sessionId for a new Session; reuse it to continue one. The 202 response
confirms acceptance, not completion. Receive output through an
events route.
The shared default Workspace is an example. Add request validation and
Session and Workspace authorization before dispatching.
See the SDK Edge API guide for complete implementations.