Next.js

Run the SDK on the server only: Route Handlers, Server Actions, and Server Components that perform data work on the server. Do not put your Noryen API key in client bundles.

Environment variables

Add NORYEN_API_KEY to .env.local (no NEXT_PUBLIC_prefix). For hosted deployments, configure the same secret in your host's environment.

Optional: NEXT_PUBLIC_NORYEN_API_URL if you need the browser to know a public API URL for other features—it is also read by the SDK for endpoint resolution on the server when set.

Route Handler (App Router)

app/api/chat/route.ts
import { noryen } from "@noryen/sdk";
import OpenAI from "openai";
export async function POST(req: Request) {
noryen.init({ apiKey: process.env.NORYEN_API_KEY! });
const openai = noryen.wrap(
new OpenAI({ apiKey: process.env.OPENAI_API_KEY! }),
);
const body = await req.json();
const completion = await openai.chat.completions.create({
model: "gpt-4o-mini",
messages: body.messages,
});
await noryen.flush();
return Response.json(completion);
}

Call flush() before returning the response so the batch is sent inside the same invocation.

Server Actions

actions.ts
"use server";
import { noryen } from "@noryen/sdk";
import OpenAI from "openai";
export async function runPrompt(prompt: string) {
noryen.init({ apiKey: process.env.NORYEN_API_KEY! });
const openai = noryen.wrap(
new OpenAI({ apiKey: process.env.OPENAI_API_KEY! }),
);
const res = await openai.chat.completions.create({
model: "gpt-4o-mini",
messages: [{ role: "user", content: prompt }],
});
await noryen.flush();
return res.choices[0]?.message?.content ?? "";
}
Never call noryen.init with the secret key inside a Client Component. If you need streaming from the client, proxy through a Route Handler that owns the SDK and key.

All installation guides