← klaro.services
SentinelConsentraKlaroShieldBundlesPricing
Frameworks

Next.js App Router API route

The same klaro instance can be shared across every route in your app -- construct it once, import it everywhere.

← All recipes
// app/api/chat/route.ts
import { NextRequest, NextResponse } from "next/server";
import { Klaro, retries, budget, secrets } from "@klaroshield/sdk";
import OpenAI from "openai";

const openai = new OpenAI();
const klaro = new Klaro()
  .use(retries({ max: 3 }))
  .use(budget({ maxMonthlyUsd: 50 }))
  .use(secrets({ mode: "mask" }));

const chat = klaro.wrap(openai.chat.completions.create.bind(openai.chat.completions));

export async function POST(req: NextRequest) {
  const { messages } = await req.json();
  const result = await chat({ model: "gpt-4o-mini", messages });
  return NextResponse.json(result);
}

Checked against the shipped @klaroshield/sdk API. Full docs at klaro.services/klaroshield/docs.

More in Frameworks

Express route handlerFastify route handlerNestJS injectable serviceMask PII in a Next.js server action