Skip to main content
A production chat UI should call Planck from your backend. The browser sends messages to your application, your server adds the Planck API key and opens the upstream stream, and your server forwards SSE bytes back to the browser.
Never put PLANCK_API_KEY in browser code, a NEXT_PUBLIC_* variable, a mobile bundle, or a response sent to the client.

Next.js App Router

Server route

Create app/api/chat/route.ts. This validates the incoming shape, starts a Planck stream, and forwards the upstream body without buffering it.
app/api/chat/route.ts
Forwarding request.signal allows a browser cancellation to propagate to Planck when the runtime supports request abort signals.

Client component

This component sends conversation history, decodes SSE frames, renders text deltas, handles stream errors, and exposes a Stop generating button.
app/chat/page.tsx
For a real application, use stable message IDs instead of array indexes and apply your existing design system, authentication, rate limits, and persistence.

FastAPI proxy

Install the server dependencies:
The proxy opens the upstream response before returning StreamingResponse, which lets it preserve non-2xx errors instead of incorrectly starting a successful stream.
app.py
Run the server with:
The same browser SSE parser from the Next.js example can consume this /api/chat route.

Production hardening

Useful request metadata

Add server-controlled headers when proxying to make production traffic easier to inspect:
Never trust a browser-supplied user or organization identifier without checking it against the authenticated session.

Streaming protocol

Review SSE chunks, usage, cancellation, and stream errors.

Tool calling

Add safe server-side functions to the chat loop.

Sessions

Group all turns from one conversation.

Custom properties

Attribute requests to environments, releases, or product features.