Quickstart: server

Call chat, leads, and escalation from your backend with an integration key.

Server Integration key dev · https://api.dev.oprag.ai

@oprag/sdk references no DOM globals at module scope, so it imports cleanly in Node, a Route Handler, or an edge runtime.

server/oprag.ts TypeScript
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_...
});
server/ask.ts TypeScript
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