Skip to main content

Streaming SSE

The iNwealth API returns responses as Server-Sent Events (SSE). This allows you to display the answer progressively as it’s generated, just like ChatGPT or Claude.

Event types

EventDescription
deltaText fragment — append to your display
metadataSources, visited Knowledge Graph nodes, markers
doneStream complete

Raw SSE format

event: delta
data: {"text": "The Plan d'Epargne "}

event: delta
data: {"text": "Retraite (PER) offers "}

event: delta
data: {"text": "several tax advantages..."}

event: metadata
data: {"visited_nodes": ["fr_per_overview"], "markers": [...]}

event: done
data: {}

JavaScript (fetch + ReadableStream)

const response = await fetch("https://api.inwealth.fr/api/agent", {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    "Authorization": "Bearer sk_live_your_key_here"
  },
  body: JSON.stringify({
    messages: [{ role: "user", content: "Tax advantages of PER?" }],
    session_id: "session-001"
  })
});

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

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

  const chunk = decoder.decode(value);
  for (const line of chunk.split("\n")) {
    if (line.startsWith("data: ")) {
      const data = JSON.parse(line.slice(6));
      if (data.text) {
        fullText += data.text;
        // Update your UI progressively
        updateDisplay(fullText);
      }
    }
  }
}

Python (httpx)

import httpx

with httpx.stream(
    "POST",
    "https://api.inwealth.fr/api/agent",
    headers={"Authorization": "Bearer sk_live_your_key_here"},
    json={
        "messages": [{"role": "user", "content": "Tax advantages of PER?"}],
        "session_id": "session-001",
    },
) as response:
    for line in response.iter_lines():
        if line.startswith("data: "):
            print(line[6:])

Multi-turn conversation

The API is stateless. Send the full conversation history in messages:
{
  "messages": [
    {"role": "user", "content": "What is the PER?"},
    {"role": "assistant", "content": "The PER is a retirement savings plan..."},
    {"role": "user", "content": "What about for non-residents?"}
  ],
  "session_id": "session-001",
  "residence": "ch"
}
Reuse the same session_id across turns to benefit from Anthropic’s prompt caching, reducing latency and token costs.