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: "Your question" }],
session_id: "session-001"
})
});
// Handle HTTP errors (401, 400, 422)
if (!response.ok) {
const error = await response.json();
console.error(`HTTP ${response.status}:`, error.detail);
return;
}
// Handle SSE stream (including SSE error events)
const reader = response.body.getReader();
const decoder = new TextDecoder();
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: ")) continue;
const data = JSON.parse(line.slice(6));
if (data.type === "error") {
// SSE error (429 rate limit, 500 internal)
console.error(data.data.error);
if (data.data.error_type === "RateLimitExceeded") {
// Wait until quota resets at midnight UTC
}
return;
}
if (data.type === "delta" && data.delta?.content) {
// Normal delta — append to display
}
}
}