> ## Documentation Index
> Fetch the complete documentation index at: https://docs.verahealth.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Reconnecting a stream

> Resume an in-flight Chat API response after a disconnect using the workflow run ID.

Every `POST /api/chat/v2` request runs on a durable workflow. If your client disconnects mid-stream — network switch, app backgrounded, tab reload — you can reconnect and continue receiving events without re-asking the question.

## 1. Capture the run ID

Early in the stream you'll receive:

```json theme={null}
{"type": "workflow-run-id", "workflowRunId": "wf_01H9Y..."}
```

Persist `workflowRunId` somewhere your client can read it after a reconnect.

## 2. Reconnect when needed

Reconnect when both are true:

1. Your stream ended **without** a `finish` event.
2. You have the saved `workflowRunId`.

Call `GET /api/chat/v2/stream/{runId}` with the **same API key** that started the run:

```bash theme={null}
curl -N https://www.verahealth.ai/api/chat/v2/stream/wf_01H9Y... \
  -H "x-api-key: YOUR_API_KEY"
```

The server replays any events buffered during the disconnect, then continues delivering live events. The event format is identical to the original stream — see [streaming events](https://docs.verahealth.ai/docs/guides/streaming.md).

## 3. JavaScript example

```javascript theme={null}
async function resumeStream(workflowRunId, apiKey) {
  const response = await fetch(
    `https://www.verahealth.ai/api/chat/v2/stream/${workflowRunId}`,
    {headers: {"x-api-key": apiKey}},
  );

  if (!response.ok) throw new Error(`Resume failed: ${response.status}`);

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

  while (true) {
    const {done, value} = await reader.read();
    if (done) break;
    buffer += decoder.decode(value, {stream: true});

    const frames = buffer.split("\n\n");
    buffer = frames.pop() ?? "";

    for (const frame of frames) {
      const line = frame.trim();
      if (!line.startsWith("data: ")) continue;
      const event = JSON.parse(line.slice(6));
      console.log(event);
    }
  }
}
```

## Authorization

Reconnection requires the same API key that originated the run. Calls made with any other key return **404** — the same response returned for runs that don't exist.

## Retrying

Reconnects are safe to retry with exponential backoff while the workflow is still running.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.