IntegrationNext.js

Add Multi-Model AI to Your Next.js App in Minutes

Use a Next.js Route Handler as a secure proxy to call LLMWise and stream results to the browser without exposing your API key.

You only pay credits per request. No monthly subscription. Paid credits never expire.

Replace multiple AI subscriptions with one wallet that includes routing, failover, and optimization.

Why teams start here first
No monthly subscription
Pay-as-you-go credits
Start with trial credits, then buy only what you consume.
Failover safety
Production-ready routing
Auto fallback across providers when latency, quality, or reliability changes.
Data control
Your policy, your choice
BYOK and zero-retention mode keep training and storage scope explicit.
Single API experience
One key, multi-provider access
Use Chat/Compare/Blend/Judge/Failover from one dashboard.
Quick start
npm install llmwise

Full example

Next.js
// app/api/llmwise/chat/route.ts — server-only proxy (keeps API key secret)
export async function POST(req: Request) {
  const body = await req.json();

  const upstream = await fetch("https://llmwise.ai/api/v1/chat", {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      Accept: "text/event-stream",
      Authorization: "Bearer " + process.env.LLMWISE_API_KEY,
    },
    body: JSON.stringify({ ...body, stream: true }),
  });

  // Stream upstream SSE straight through to the browser.
  return new Response(upstream.body, {
    status: upstream.status,
    headers: {
      "Content-Type": "text/event-stream",
      "Cache-Control": "no-cache",
    },
  });
}

// app/page.tsx — minimal client that parses SSE JSON and renders deltas
"use client";
import { useState } from "react";

async function* readSSE(res: Response) {
  const reader = res.body?.getReader();
  if (!reader) return;
  const decoder = new TextDecoder();
  let buffer = "";

  while (true) {
    const { value, done } = await reader.read();
    if (done) break;
    buffer += decoder.decode(value, { stream: true });

    let idx = 0;
    while ((idx = buffer.indexOf("\n\n")) !== -1) {
      const raw = buffer.slice(0, idx);
      buffer = buffer.slice(idx + 2);

      for (const line of raw.split("\n")) {
        if (!line.startsWith("data: ")) continue;
        const data = line.slice(6);
        if (data === "[DONE]") return;
        yield JSON.parse(data);
      }
    }
  }
}

export default function Page() {
  const [model, setModel] = useState("auto");
  const [input, setInput] = useState("");
  const [out, setOut] = useState("");
  const [loading, setLoading] = useState(false);

  async function send() {
    setLoading(true);
    setOut("");
    const res = await fetch("/api/llmwise/chat", {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({
        model,
        messages: [{ role: "user", content: input }],
        stream: true,
      }),
    });

    for await (const ev of readSSE(res)) {
      if (ev.error) break;
      if (ev.delta) setOut((p) => p + ev.delta);
      if (ev.event === "done") break;
    }
    setLoading(false);
  }

  return (
    <div style={{ maxWidth: 720, margin: "0 auto", padding: 16 }}>
      <select value={model} onChange={(e) => setModel(e.target.value)}>
        <option value="auto">auto</option>
        <option value="gpt-5.2">gpt-5.2</option>
        <option value="claude-sonnet-4.5">claude-sonnet-4.5</option>
        <option value="gemini-3-flash">gemini-3-flash</option>
        <option value="deepseek-v3">deepseek-v3</option>
      </select>
      <div style={{ marginTop: 12, whiteSpace: "pre-wrap", minHeight: 200 }}>{out}</div>
      <textarea value={input} onChange={(e) => setInput(e.target.value)} style={{ width: "100%" }} />
      <button disabled={loading} onClick={send}>{loading ? "Thinking..." : "Send"}</button>
    </div>
  );
}
Evidence snapshot

Next.js integration overview

Everything you need to integrate LLMWise's multi-model API into your Next.js project.

Setup steps
5
to first API call
Features
8
capabilities included
Models available
9
via single endpoint
Starter credits
40
trial 7 days · paid credits never expire

What you get

+Keep your LLMWise API key server-side in a Route Handler
+Stream responses to the browser using SSE (no WebSockets required)
+Switch models per request, or use model="auto" for routing
+Works with App Router Route Handlers (and similar serverless functions)
+Supports Mesh failover routing via fallback chains
+Same OpenAI-style messages format (role + content)
+Low-friction to integrate into existing Next.js apps
+Works on Node.js and many edge-style runtimes that support streaming fetch

Step-by-step integration

1Install dependencies (optional SDK)

You can call LLMWise with plain fetch (no SDK required). If you want typed helpers for server-side calls, install the official SDK.

npm install llmwise
2Create a secure proxy Route Handler

Create a POST handler that forwards requests to https://llmwise.ai/api/v1/chat. This keeps your API key server-only and streams SSE to the browser.

// app/api/llmwise/chat/route.ts
export async function POST(req: Request) {
  const body = await req.json();
  const upstream = await fetch("https://llmwise.ai/api/v1/chat", {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      Accept: "text/event-stream",
      Authorization: "Bearer " + process.env.LLMWISE_API_KEY,
    },
    body: JSON.stringify({ ...body, stream: true }),
  });
  return new Response(upstream.body, { headers: { "Content-Type": "text/event-stream" } });
}
3Parse SSE JSON events in the browser

LLMWise streaming is SSE with JSON chunks that include a delta field. Read lines starting with data: and append ev.delta.

async function* readSSE(res: Response) {
  const reader = res.body?.getReader();
  if (!reader) return;
  const decoder = new TextDecoder();
  let buffer = "";
  while (true) {
    const { value, done } = await reader.read();
    if (done) break;
    buffer += decoder.decode(value, { stream: true });
    let idx = 0;
    while ((idx = buffer.indexOf("\n\n")) !== -1) {
      const raw = buffer.slice(0, idx);
      buffer = buffer.slice(idx + 2);
      for (const line of raw.split("\n")) {
        if (!line.startsWith("data: ")) continue;
        const data = line.slice(6);
        if (data === "[DONE]") return;
        yield JSON.parse(data);
      }
    }
  }
}
4Add environment variables

Store your LLMWise API key in .env.local. Next.js will automatically load it on the server side. Never expose the key to the client — the Route Handler keeps it server-only.

# .env.local
LLMWISE_API_KEY=your_api_key_here
5Add a model switcher for A/B testing

Since the only difference between models is the model string, you can add a dropdown (or use model="auto") to route by task type. You can also upgrade to Compare/Blend/Judge for multi-model workflows.

// Pass model in the request body — the Route Handler reads it
await fetch("/api/llmwise/chat", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({
    model: selectedModel,
    messages,
    stream: true,
  }),
});

Common questions

Does LLMWise work with Next.js App Router and Server Components?
Yes. Use a Route Handler (recommended) to keep your API key server-side and stream to the browser. For server-side generation, call https://llmwise.ai/api/v1/chat directly from Server Components (non-stream).
Can I deploy a Next.js app with LLMWise to Vercel Edge Functions?
Often yes, as long as your runtime supports streaming fetch and Response bodies. Start with Node.js runtime first; then move to edge if you’ve verified streaming behavior in your deployment environment.
How do I handle rate limiting in a Next.js app using LLMWise?
LLMWise handles provider-level rate limits with built-in failover. For your own app-level rate limiting, add middleware in your Route Handler that checks request frequency per user. The credit system also acts as a natural rate limiter since each request costs credits.
Is the LLMWise API key exposed to the browser in a Next.js app?
No. The API key lives in .env.local and is only accessible in server-side code (Route Handlers, Server Components, getServerSideProps). Your browser calls your own /api/... endpoint, and that route handler calls LLMWise with the secret key.

One wallet, enterprise AI controls built in

You only pay credits per request. No monthly subscription. Paid credits never expire.

Replace multiple AI subscriptions with one wallet that includes routing, failover, and optimization.

Chat, Compare, Blend, Judge, MeshPolicy routing + replay labFailover without extra subscriptions