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

# Wait for Ambient before Fetch

> Poll ambient session status until completed before calling content APIs

# Wait for Ambient before Fetch

After you end an ambient session, note generation runs asynchronously. Poll session status until it reaches `completed`, then call content APIs. Treat `skipped`, `failed`, and `aborted` as terminal outcomes.

## Problem

Partners often call note or session content APIs immediately after ending an ambient session and receive empty or incomplete results. Generation is asynchronous, so the session may still be processing even when the WebSocket is closed and REST end returns success.

Poll [Get ambient session status](https://developer.suki.ai/api-reference/ambient-content/status.md) until status is `completed` before retrieving content. Treat `skipped`, `failed`, and `aborted` as terminal states that stop polling.

## Architecture

![Flowchart: End ambient session, GET /status, Status?, Call content APIs, Stop and handle.](https://mintcdn.com/suki-1e08f176/eryJVVKcBRotEMfQ/documentation/assets/diagrams/cookbooks-wait-for-status-before-retrieve-1-light.svg?fit=max\&auto=format\&n=eryJVVKcBRotEMfQ\&q=85\&s=1eee10f0b6b0936037a4c2c78651d019)

```mermaid actions={false} theme={"theme":{"light":"one-light","dark":"material-theme-darker"}}
%%{init: {'theme':'base', 'themeVariables': { 'primaryColor':'#FFFBDE','primaryTextColor':'#1C1C1C','primaryBorderColor':'#FFD147','lineColor':'#6F5410','secondaryColor':'#FFFDF5','tertiaryColor':'#FFEB94','fontSize':'13px'}}}%%
flowchart LR
  A[End ambient session] --> B[GET /status]
  B --> C{Status?}
  C -->|completed| D[Call content APIs]
  C -->|processing| B
  C -->|skipped/failed/aborted| E[Stop and handle]
```

## Prerequisites

> [!TIP]
>
> Completed [Partner onboarding](https://developer.suki.ai/documentation/get-started/partner-onboarding.md) and have `partner_id` with a valid Partner Token flow.

> [!TIP]
>
> An ambient session that has been ended with REST `POST /end` after streaming stopped.

> [!TIP]
>
> `sdp_suki_token` and `sdp_provider_id` headers ready for status and content API calls.

## Solution

> [card] Before
>
> [End Ambient after Streaming](https://developer.suki.ai/documentation/cookbooks/end-ambient-after-streaming.md)

> [card] This Recipe
>
> Poll `GET /status` until `completed`, or stop on a terminal failure.

> [card] Next
>
> [Fetch Note by Note ID](https://developer.suki.ai/documentation/cookbooks/get-note-content-by-note-id.md)

Poll the ambient session status endpoint on a fixed interval until status is `completed` or a terminal failure state. Only call content or note APIs after `completed`.

> [tab] TypeScript
>
> ```typescript theme={"theme":{"light":"one-light","dark":"material-theme-darker"}}
> // wait-for-status-before-retrieve.ts
>
> const BASE_URL = process.env.SUKI_BASE_URL ?? "https://sdp.suki.ai";
> const SDP_SUKI_TOKEN = process.env.SDP_SUKI_TOKEN!;
> const SDP_PROVIDER_ID = process.env.SDP_PROVIDER_ID!;
>
> function restHeaders(): Record<string, string> {
>   return {
>     sdp_suki_token: SDP_SUKI_TOKEN,
>     sdp_provider_id: SDP_PROVIDER_ID,
>   };
> }
>
> async function waitUntilCompleted(
>   ambientSessionId: string,
>   pollMs = 2000,
>   maxAttempts = 60
> ): Promise<string> {
>   for (let i = 0; i < maxAttempts; i++) {
>     const res = await fetch(
>       `${BASE_URL}/api/v1/ambient/session/${ambientSessionId}/status`,
>       { headers: restHeaders() }
>     );
>
>     if (!res.ok) {
>       throw new Error(`Get ambient session status failed: ${res.status}`);
>     }
>
>     const { status } = (await res.json()) as { status: string };
>     if (status === "completed") return "completed";
>     if (status === "skipped" || status === "failed" || status === "aborted") {
>       return status;
>     }
>
>     await new Promise((r) => setTimeout(r, pollMs));
>   }
>
>   throw new Error("Timed out waiting for session status");
> }
>
> async function getSessionContent(ambientSessionId: string): Promise<unknown> {
>   const res = await fetch(
>     `${BASE_URL}/api/v1/ambient/session/${ambientSessionId}/content?cumulative=false`,
>     { headers: restHeaders() }
>   );
>   if (!res.ok) {
>     throw new Error(`Get session content failed: ${res.status}`);
>   }
>   return res.json();
> }
>
> const ambientSessionId = process.env.AMBIENT_SESSION_ID!;
> const outcome = await waitUntilCompleted(ambientSessionId);
>
> if (outcome === "completed") {
>   const content = await getSessionContent(ambientSessionId);
>   console.log(JSON.stringify(content, null, 2));
>   // Prefer note_id + GET /ambient/note/{note_id}/content for the shared clinical note.
> } else {
>   console.error("Terminal status:", outcome);
> }
> ```

> [tab] Python
>
> ```python theme={"theme":{"light":"one-light","dark":"material-theme-darker"}}
> # wait_for_status_before_retrieve.py
> # pip install requests
>
> import json
> import os
> import time
>
> import requests
>
> BASE_URL = os.environ.get("SUKI_BASE_URL", "https://sdp.suki.ai")
> SDP_SUKI_TOKEN = os.environ["SDP_SUKI_TOKEN"]
> SDP_PROVIDER_ID = os.environ["SDP_PROVIDER_ID"]
>
>
> def rest_headers() -> dict[str, str]:
>     return {
>         "sdp_suki_token": SDP_SUKI_TOKEN,
>         "sdp_provider_id": SDP_PROVIDER_ID,
>     }
>
>
> def wait_until_completed(
>     ambient_session_id: str,
>     poll_s: float = 2.0,
>     max_attempts: int = 60,
> ) -> str:
>     for _ in range(max_attempts):
>         res = requests.get(
>             f"{BASE_URL}/api/v1/ambient/session/{ambient_session_id}/status",
>             headers=rest_headers(),
>             timeout=30,
>         )
>         res.raise_for_status()
>         status = res.json().get("status")
>         if status == "completed":
>             return "completed"
>         if status in ("skipped", "failed", "aborted"):
>             return status
>         time.sleep(poll_s)
>     raise TimeoutError("Timed out waiting for session status")
>
>
> def get_session_content(ambient_session_id: str):
>     res = requests.get(
>         f"{BASE_URL}/api/v1/ambient/session/{ambient_session_id}/content",
>         params={"cumulative": "false"},
>         headers=rest_headers(),
>         timeout=30,
>     )
>     res.raise_for_status()
>     return res.json()
>
>
> ambient_session_id = os.environ["AMBIENT_SESSION_ID"]
> outcome = wait_until_completed(ambient_session_id)
>
> if outcome == "completed":
>     content = get_session_content(ambient_session_id)
>     print(json.dumps(content, indent=2))
>     # Prefer note_id + GET /ambient/note/{note_id}/content for the shared clinical note.
> else:
>     print("Terminal status:", outcome)
> ```

**1. End the ambient session first**

Send the RU9G end marker, close the WebSocket, and call REST end before polling. See [End ambient after streaming](https://developer.suki.ai/documentation/cookbooks/end-ambient-after-streaming.md).

**2. Poll GET /status on an interval**

Call `GET /api/v1/ambient/session/{ambient_session_id}/status` every 2 seconds with Partner Token headers. See [Get ambient session status](https://developer.suki.ai/api-reference/ambient-content/status.md).

**3. Stop on completed or terminal states**

Return when status is `completed`. Stop polling and handle the outcome when status is `skipped`, `failed`, or `aborted`.

**4. Retrieve content after completed**

Call session content or note content APIs only when status is `completed`. Use [Fetch note by Note ID](https://developer.suki.ai/documentation/cookbooks/get-note-content-by-note-id.md) next.

## Try it

> [card]
>
> * **Status reaches completed before content calls**
>
>   After REST end, polling should eventually return `completed`. Calling content APIs before that often returns empty or partial data.
>
> * **Terminal states stop the loop**
>
>   When status is `skipped`, `failed`, or `aborted`, exit polling and branch your workflow. Do not keep calling content APIs expecting a note.
>
> * **Short sessions may return skipped**
>
>   Sessions under about one minute often return `skipped`. Plan your integration to handle that outcome without retrying content retrieval.

## Common mistakes

> [!WARNING]
>
> * Call content APIs immediately after REST end without polling status first.
> * Assume WebSocket close alone means the note is ready for retrieval.
> * Keep polling after `skipped`, `failed`, or `aborted` instead of treating them as terminal.

## Related cookbooks

Ambient
API

### [Fetch Note by Note ID](https://developer.suki.ai/documentation/cookbooks/get-note-content-by-note-id.md)

Retrieve the note with note\_id.

5 min

Ambient
API

### [Resolve a Remote Session Conflict](https://developer.suki.ai/documentation/cookbooks/resolve-session-conflict.md)

End remote session, then retry.

5 min

Run This Recipe

[Open Full Tutorial](https://developer.suki.ai/documentation/tutorials/ambient-websocket-code-example.md)
[View API Reference](https://developer.suki.ai/api-reference/ambient-content/status.md)

Step-by-step tutorial and API reference for this workflow.


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