Skip to main content

Send Message

POST /api/v1/apps/{appId}/chat/message
message
string
required
User message, 1–2000 characters.
role
string
required
Actor role sending the message (non-empty string, e.g. "user").
threadId
string
Existing thread to continue. If omitted, a new thread is created.
sessionId
string
Session identifier for grouping threads.
lessonContext
object
App-specific lesson context, passed through to prompt construction.
sessionContext
object
App-specific session context, passed through to prompt construction.
pageContext
object
Free-form page/document context.
chatHistory
object[]
Prior messages to seed context, if not relying on threadId.
controls
object

Response

threadId
string
turnId
string
content
string
The assistant’s reply
role
string
"assistant"
metadata
object
contextWindow
object
curl -X POST https://suite.sundaypyjamas.com/api/v1/apps/app_123/chat/message \
  -H "Authorization: Bearer spj_ai_your_api_key_here" \
  -H "Content-Type: application/json" \
  -d '{ "message": "What'"'"'s our refund policy?", "role": "user" }'
{
  "threadId": "thr_a1b2...",
  "turnId": "turn_c3d4...",
  "content": "Refunds are processed within 5 business days of the request.",
  "role": "assistant",
  "metadata": {
    "model": "gpt-4o-mini",
    "tokensUsed": { "input": 214, "output": 18, "total": 232 },
    "latencyMs": 640
  },
  "contextWindow": { "utilization": 0.04 }
}

Send Message (Streaming)

POST /api/v1/apps/{appId}/chat/message/stream
Same request body as Send Message, plus:
externalIdentity
object

Response

Content-Type: text/event-stream. Each data: event is one of:
Token chunk
{ "type": "token", "content": "Refunds " }
Done (final event)
{
  "type": "done",
  "threadId": "thr_a1b2...",
  "turnId": "turn_c3d4...",
  "contextWindow": { "utilization": 0.04 },
  "metadata": {
    "model": "gpt-4o-mini",
    "tokensUsed": { "input": 214, "output": 18, "total": 232 },
    "latencyMs": 640
  }
}
Error
{ "type": "error", "code": "MODEL_ERROR", "message": "..." }
const res = await fetch(`https://suite.sundaypyjamas.com/api/v1/apps/${appId}/chat/message/stream`, {
  method: 'POST',
  headers: {
    Authorization: `Bearer ${apiKey}`,
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({ message: "What's our refund policy?", role: 'user' }),
});

const reader = res.body.getReader();
const decoder = new TextDecoder();
while (true) {
  const { done, value } = await reader.read();
  if (done) break;
  for (const line of decoder.decode(value).split('\n')) {
    if (line.startsWith('data: ')) {
      const event = JSON.parse(line.slice(6));
      if (event.type === 'token') process.stdout.write(event.content);
    }
  }
}

Errors

StatusErrorCause
400message is requiredMissing/empty message
400message must be 2000 characters or lessMessage too long
400role is requiredMissing role
401Unauthorized or app not foundInvalid key or app not in your workspace
402Insufficient creditsWorkspace balance too low
502AI model error: {details}Upstream model failure
503Model configuration errorApp has no valid model configured