cookbook/Get Started

Quickstart: Build a Modern Streaming Chat App

Build a production-ready Next.js 16 chat application with real-time SSE streaming.


Building the Chat Route Handler

import { NextResponse } from "next/server";

export async function POST(req: Request) {
  const { messages, model = "claude-sonnet-5.5" } = await req.json();

  const upstream = await fetch("https://unlockedai.net/v1/chat/completions", {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      Authorization: `Bearer ${process.env.UNLOCKED_API_KEY}`,
    },
    body: JSON.stringify({
      model,
      stream: true,
      messages,
    }),
  });

  return new Response(upstream.body, {
    headers: {
      "Content-Type": "text/event-stream",
      "Cache-Control": "no-cache",
      Connection: "keep-alive",
    },
  });
}

Need an Unlocked AI Key or Credits?

Generate secret API keys backed by non-expiring pay-as-you-go credits starting at $5. No monthly minimums.