Developers

Call Claude, GPT and Gemini from Next.js

In Next.js, call Somnus from a Route Handler or Server Action so the key stays on the server, and stream the reply back to your React UI.

Install

bash
npm install openai

Basic request

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();
    },
  }));
}

Streaming

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();
    },
  }));
}

JSON output

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();
    },
  }));
}

Next.js notes

  • Keep SOMNUS_API_KEY in server-only environment variables (no NEXT_PUBLIC_ prefix).
  • Stream responses from a Route Handler for chat UIs.
  • Add per-user rate limits before going public.

Questions

Which models can I use?

Any of the 50+ models on Somnus — change the model string. The examples use claude-sonnet-5.

Where do I get a key?

Create an account, and your key appears in the dashboard with $5 of free credit.

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