Developers

Build chatbots in Next.js

A chatbot needs replies that appear quickly, stay on-topic, and cost little per conversation. Streaming the answer token by token makes even long replies feel instant. Below is a working Next.js starting point using the openai npm package inside a server route.

Next.js example

typescript
// app/api/chat/route.ts — runs on the server only
import OpenAI from "openai";

const client = new OpenAI({
  apiKey: process.env.SOMNUS_API_KEY,
  baseURL: "https://gateway-production-c837.up.railway.app/v1",
});

export async function POST(req: Request) {
  const { message } = await req.json();
  const stream = await client.chat.completions.create({
    model: "claude-sonnet-5",
    messages: [{ role: "user", content: message }],
    stream: true,
  });
  const encoder = new TextEncoder();
  return new Response(new ReadableStream({
    async start(controller) {
      for await (const chunk of stream) {
        controller.enqueue(encoder.encode(chunk.choices[0]?.delta?.content ?? ""));
      }
      controller.close();
    },
  }));
}

Models that fit

ModelMakerList price (in / out per 1M)Through SomnusWhy
Claude Haiku 4.5Anthropic$1 / $5$0.5 / $2.5low cost per turn
Gemini 3.8 FlashGoogle$0.75 / $3.75$0.38 / $1.88cheap and quick for high volume
Claude Sonnet 5Anthropic$2 / $10$1 / $5better answers for harder questions

Somnus column uses the minimum 2× credit; larger top-ups go further.

Making it production-ready in Next.js

  • Keep SOMNUS_API_KEY in server-only environment variables (no NEXT_PUBLIC_ prefix).
  • Stream responses from a Route Handler for chat UIs.
  • Keep conversation history short — summarise older turns instead of resending them.
  • Start on a small model and route hard questions to a larger one.

Avoid

  • Resending the entire chat history on every turn multiplies input cost.
  • Not streaming makes the bot feel slow even when it isn't.

Questions

Do I need a special SDK for chatbots in Next.js?

No — the openai npm package inside a server route is enough, because Somnus uses the OpenAI chat-completions format.

Run this code with free credit

Create an account and get $5 of free API credit — enough to try every example on this page.

Get $5 free credit

Related

Browse more: Alternatives · Compare · Use cases · Developers · Solutions · Tools

WindowsStart with $5 free