POST /api/generate/stream
Generates code using AI based on a user prompt. Supports both creating new code and editing existing files with intelligent file selection.
Request body
The user’s code generation request or instruction
Whether this is an edit operation on existing code. Defaults to false
The sandbox ID to operate on. Required when isEdit is true
Additional context for code generation
Record of file paths to their contents. Keys are file paths, values are file contents as strings
Package manifest information (e.g., package.json)
Array of recent conversation messages for context
Either “user” or “assistant”
Optional conversation ID for tracking multi-turn interactions
Response
Returns a server-sent events (SSE) stream with Content-Type: text/event-stream. Events are sent as JSON objects prefixed with data: .
Stream event types
Sent every 5 seconds to prevent connection timeout
Status message, typically “Generating…”
Progress updates during generation
Human-readable status message
Incremental chunks of generated content
Sequential index of this chunk
A complete generated file
File path relative to project root
A required npm package
Package name (e.g., “react-router-dom”)
Final event indicating generation is complete
Array of all generated files with path and content
Array of required package names as strings
Type of edit performed (if applicable)
Error event if generation fails
Error codes
Invalid JSON body or missing required prompt field
Example request
curl -X POST https://your-domain.com/api/generate/stream \
-H "Content-Type: application/json" \
-d '{
"prompt": "Create a React counter component with increment and decrement buttons",
"isEdit": false
}'
Example with context
curl -X POST https://your-domain.com/api/generate/stream \
-H "Content-Type: application/json" \
-d '{
"prompt": "Add a reset button to the counter",
"isEdit": true,
"sandboxId": "sb_abc123",
"context": {
"files": {
"src/Counter.tsx": "export default function Counter() { ... }"
},
"recentMessages": [
{
"role": "user",
"content": "Create a counter component"
},
{
"role": "assistant",
"content": "I created a Counter component with increment and decrement."
}
]
}
}'
Example SSE response
data: {"type":"status","message":"Analyzing request..."}
data: {"type":"stream","data":{"content":"Creating Counter component","index":0}}
data: {"type":"file","data":{"path":"src/Counter.tsx","content":"import { useState } from 'react';\n\nexport default function Counter() { ... }"}}
data: {"type":"package","data":{"name":"react"}}
data: {"type":"complete","data":{"files":[{"path":"src/Counter.tsx","content":"..."}],"packages":["react"]}}
Smart file selection
When isEdit is true and sandboxId is provided without explicit context.files, the endpoint automatically:
- Retrieves the sandbox file list
- Uses LLM-based intent analysis to identify relevant files
- Fetches only the targeted file contents
- Falls back to
App.tsx/App.jsx if no files are selected
This ensures efficient context usage while maintaining high edit accuracy.