mindra

Stream Events

Receive real-time execution events via SSE

2 min read

Stream Events

After triggering a workflow execution, connect to the stream URL to receive real-time events via Server-Sent Events (SSE).

Endpoint

GET /api/v1/workflows/execute/{execution_id}/stream

Path Parameters

ParameterTypeDescription
execution_idstringThe execution ID from the run response

Example

curl -N "https://api.mindra.co/api/v1/workflows/execute/exec_abc123/stream" \
  -H "x-api-key: YOUR_API_KEY"

Event Types

The stream emits the following event types:

chunk

A text chunk from the agent's response. Collect these to build the full response.

event: chunk
data: {"content": "Based on the information I found..."}

tool_executing

The agent is about to execute a tool. This event fires before the tool runs.

event: tool_executing
data: {"tool_name": "web_search", "tool_input": {"query": "latest updates"}}

tool_result

A tool has finished executing and returned a result.

event: tool_result
data: {"tool_name": "web_search", "result": "..."}

approval_request

A high-risk tool execution requires human approval before proceeding. The workflow pauses until the request is approved or rejected.

event: approval_request
data: {"approval_id": "apr_xyz789", "tool_name": "send_email", "tool_input": {"to": "user@example.com", "subject": "Summary report"}}

See Approvals for how to handle this event.

done

The workflow execution has completed.

event: done
data: {"execution_id": "exec_abc123", "status": "completed", "final_answer": "Here is the result of your task..."}

Consuming SSE in TypeScript

const response = await fetch(
  `https://api.mindra.co/api/v1/workflows/execute/${executionId}/stream`,
  {
    headers: { "x-api-key": process.env.MINDRA_API_KEY },
  }
);

const reader = response.body.getReader();
const decoder = new TextDecoder();

while (true) {
  const { done, value } = await reader.read();
  if (done) break;

  const text = decoder.decode(value);
  const lines = text.split("\n");

  for (const line of lines) {
    if (line.startsWith("event: ")) {
      const eventType = line.slice(7);
      console.log("Event:", eventType);
    }
    if (line.startsWith("data: ")) {
      const data = JSON.parse(line.slice(6));
      console.log("Data:", data);
    }
  }
}

Consuming SSE in Python

import requests
import json

response = requests.get(
    f"https://api.mindra.co/api/v1/workflows/execute/{execution_id}/stream",
    headers={"x-api-key": "YOUR_API_KEY"},
    stream=True,
)

for line in response.iter_lines():
    if line:
        decoded = line.decode("utf-8")
        if decoded.startswith("data: "):
            data = json.loads(decoded[6:])
            print(data)