Quickstart: server
Call chat, leads, and escalation from your backend with an integration key.
@oprag/sdk references no DOM globals at module scope, so it imports cleanly in Node, a Route Handler, or an edge runtime.
import { createClient } from "@oprag/sdk";
export const oprag = createClient({
apiUrl: process.env.OPRAG_API_URL!, // https://api.dev.oprag.ai
apiKey: process.env.OPRAG_SECRET_KEY!, // sk_live_...
}); import { OpragError } from "@oprag/sdk";
import { oprag } from "./oprag";
export async function ask(question: string, ctx: { sessionId?: string; visitorId?: string }) {
try {
const res = await oprag.chat.ask({ question, ...ctx });
// The response is a union — check the branch before reading `answer`.
if (res.type === "lead_capture_prompt") {
return { kind: "lead" as const, prompt: res.promptMessage, fields: res.fields };
}
return { kind: "answer" as const, answer: res.answer, sources: res.sources ?? [] };
} catch (err) {
if (err instanceof OpragError) {
// `code` is derived client-side from the status; the API sends only `error`.
return { kind: "error" as const, status: err.status, code: err.code };
}
throw err;
}
} Fronting a browser UI with this is the backend-for-frontend recipe.
Ready to ship?
Get started free