API Usage Examples
Examples for the canonical streamed AI route
API Usage Examples
All streamed AI requests now go through:
POST https://impression.so/api/ai/streamThese examples use the canonical AiStreamRequest contract and consume typed SSE events.
curl: Visible Chat
curl -N https://impression.so/api/ai/stream \
-H "Content-Type: application/json" \
-H "Accept: text/event-stream" \
-H "X-Space-API-Key: your_space_api_key" \
-d '{
"mode": "visible_chat",
"agentId": "hudsonAgent",
"threadId": "thread_123",
"input": {
"message": "Give me three stronger openings for this idea.",
"uiSurface": "chat"
},
"context": {
"spaceId": "space_123",
"brandId": "brand_123",
"requestedTargets": [
{
"target": "chat_only",
"applyMode": "none"
}
],
"allowedMutationScope": "none",
"createThreadIfMissing": false,
"threadVisibility": "visible"
}
}'curl: Hidden Editor Action
curl -N https://impression.so/api/ai/stream \
-H "Content-Type: application/json" \
-H "Accept: text/event-stream" \
-H "X-Space-API-Key: your_space_api_key" \
-d '{
"mode": "hidden_action",
"agentId": "hudsonAgent",
"input": {
"actionId": "post.generate",
"uiSurface": "editor_button"
},
"context": {
"spaceId": "space_123",
"brandId": "brand_123",
"postId": "post_123",
"requestedTargets": [
{
"target": "post_node",
"postId": "post_123",
"nodeType": "LinkedInMainBody",
"applyMode": "replace"
}
],
"defaultTarget": "post_node",
"allowedMutationScope": "post_only",
"createThreadIfMissing": true,
"threadVisibility": "hidden"
}
}'JavaScript / Node.js
async function streamAiRun() {
const response = await fetch('https://impression.so/api/ai/stream', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Accept: 'text/event-stream',
'X-Space-API-Key': 'your_space_api_key',
},
body: JSON.stringify({
mode: 'visible_chat',
agentId: 'hudsonAgent',
threadId: 'thread_123',
input: {
message: 'Summarize the strongest angle here.',
uiSurface: 'chat',
},
context: {
spaceId: 'space_123',
brandId: 'brand_123',
requestedTargets: [{ target: 'chat_only', applyMode: 'none' }],
allowedMutationScope: 'none',
createThreadIfMissing: false,
threadVisibility: 'visible',
},
}),
});
if (!response.ok || !response.body) {
throw new Error(`Request failed: ${response.status}`);
}
const runId = response.headers.get('x-ai-run-id');
const reader = response.body.getReader();
const decoder = new TextDecoder();
let buffer = '';
while (true) {
const { done, value } = await reader.read();
buffer += decoder.decode(value, { stream: !done });
const frames = buffer.split('\n\n');
buffer = frames.pop() ?? '';
for (const frame of frames) {
const data = frame
.split('\n')
.filter((line) => line.startsWith('data:'))
.map((line) => line.slice(5).trim())
.join('\n');
if (!data || data === '[DONE]') continue;
const event = JSON.parse(data);
console.log(runId, event.type, event.sequence, event);
}
if (done) break;
}
}Python
import json
import requests
payload = {
"mode": "visible_chat",
"agentId": "hudsonAgent",
"threadId": "thread_123",
"input": {
"message": "Turn this into a tighter outline.",
"uiSurface": "chat",
},
"context": {
"spaceId": "space_123",
"brandId": "brand_123",
"requestedTargets": [
{
"target": "chat_only",
"applyMode": "none",
}
],
"allowedMutationScope": "none",
"createThreadIfMissing": False,
"threadVisibility": "visible",
},
}
with requests.post(
"https://impression.so/api/ai/stream",
headers={
"Content-Type": "application/json",
"Accept": "text/event-stream",
"X-Space-API-Key": "your_space_api_key",
},
data=json.dumps(payload),
stream=True,
) as response:
response.raise_for_status()
run_id = response.headers.get("x-ai-run-id")
for line in response.iter_lines(decode_unicode=True):
if not line or not line.startswith("data:"):
continue
data = line[len("data:") :].strip()
if data == "[DONE]":
continue
event = json.loads(data)
print(run_id, event["type"], event["sequence"])Resume Example
If the connection drops, resend the same request with runId and afterSequence:
{
"runId": "run_123",
"afterSequence": 42,
"mode": "visible_chat",
"agentId": "hudsonAgent",
"threadId": "thread_123",
"input": {
"message": "Resume this run",
"uiSurface": "chat"
},
"context": {
"spaceId": "space_123",
"brandId": "brand_123",
"requestedTargets": [
{
"target": "chat_only",
"applyMode": "none"
}
],
"allowedMutationScope": "none",
"createThreadIfMissing": false,
"threadVisibility": "visible"
}
}