DEVSAIO

Build a streaming AI chat app with Next.js

A Next.js App Router chat page with a server route that streams from any model, keeping your key off the client.

DEVSAIO team · 2 min read
Share on X

1. Create the app

bash
npx create-next-app@latest ai-chat
cd ai-chat && npm install openai
echo "DEVSAIO_API_KEY=YOUR_KEY" > .env.local

2. The API route

ts
// app/api/chat/route.ts
import OpenAI from "openai";

const client = new OpenAI({ baseURL: "https://devsaio.lol/v1", apiKey: process.env.DEVSAIO_API_KEY });

export async function POST(req: Request) {
  const { messages } = await req.json();
  const stream = await client.chat.completions.create({
    model: "claude-sonnet-4-6",
    messages: [{ role: "system", content: "You are a concise assistant." }, ...messages.slice(-16)],
    stream: true,
    max_tokens: 1000,
  });
  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();
    },
  }), { headers: { "Content-Type": "text/plain; charset=utf-8" } });
}

3. The page

tsx
// app/page.tsx
"use client";
import { useState } from "react";

type Msg = { role: "user" | "assistant"; content: string };

export default function Chat() {
  const [msgs, setMsgs] = useState<Msg[]>([]);
  const [input, setInput] = useState("");

  async function send(e: React.FormEvent) {
    e.preventDefault();
    const next = [...msgs, { role: "user" as const, content: input }];
    setMsgs([...next, { role: "assistant", content: "" }]);
    setInput("");
    const res = await fetch("/api/chat", { method: "POST", body: JSON.stringify({ messages: next }) });
    const reader = res.body!.getReader(); const dec = new TextDecoder(); let text = "";
    for (;;) {
      const { value, done } = await reader.read(); if (done) break;
      text += dec.decode(value, { stream: true });
      setMsgs([...next, { role: "assistant", content: text }]);
    }
  }

  return (
    <main style={{ maxWidth: 680, margin: "40px auto", fontFamily: "system-ui" }}>
      {msgs.map((m, i) => <p key={i}><b>{m.role === "user" ? "You" : "AI"}:</b> {m.content}</p>)}
      <form onSubmit={send}><input value={input} onChange={(e) => setInput(e.target.value)} style={{ width: "80%" }} /> <button>Send</button></form>
    </main>
  );
}

React escapes {m.content} automatically, so model output can't inject HTML.

Next steps

  • Render Markdown safely with a library such as react-markdown.
  • Add authentication and per-user limits before you share it.
  • Prefer a higher-level toolkit? See the Vercel AI SDK guide.
Try it with one API key

Claude, GPT, Gemini, DeepSeek, image and video models through one OpenAI- and Anthropic-compatible endpoint. Prepaid, pay only for what you use.

Keep reading