HTTP Transport
The HTTP transport uses standard HTTP requests with Server-Sent Events (SSE) for streaming. This is the simplest and most compatible transport option.
When to Use HTTP Transport
| Use Case | Recommendation |
|---|---|
| Next.js, Remix, or similar frameworks | ✅ Use HTTP |
| Standard web apps without special requirements | ✅ Use HTTP |
| Serverless deployments (Vercel, etc.) | ✅ Use HTTP |
| Need custom real-time events | Consider Socket Transport |
Basic Setup
Client
Server (Next.js API Route)
Session Creation
Sessions should be created server-side before rendering the chat. There are two patterns:
Pattern 1: Create Session on Page Load
Pattern 2: Server-Side Session Creation (App Router)
This pattern is cleaner as the session is ready before the component renders.
Error Handling
Handle errors with structured error information:
See Error Handling for comprehensive error handling patterns.
Stop Streaming
Allow users to cancel ongoing streams. When stop() is called:
- The HTTP request is aborted via the signal
- Any partial content is preserved in the message
- Tool calls in progress are marked as
cancelled - Status changes to
idle
Important: For stop to work end-to-end, pass the options.signal to your fetch() call and forward request.signal to session.execute() on the server.
Express Server
For non-Next.js backends:
Transport Options
The request function receives a discriminated union. Spread the request onto your payload:
Protocol
Request Format
The request function receives a discriminated union with type to identify the request kind:
Trigger Request (start a new turn):
Continue Request (after client tool handling):
Response Format
The server responds with an SSE stream:
If client tools are needed, the stream pauses with a client-tool-request event:
The client handles the tools and sends a continue request to resume.
See Streaming Events for the full list of event types.
Next Steps
- Quick Start — Complete Next.js integration guide
- Client Tools — Handling tools on the client side
- Messages — Working with message state
- Streaming — Building streaming UIs
- Error Handling — Handling errors with type guards