## Session lifecycle

### Automatic waiting queue

When your account has no available live-session slot, a new request enters a first-in, first-out queue shared by all its API keys. Waiting is automatic by default. Queued sessions do not allocate a stream, reserve credits or accrue usage. The current SDK polls every 3 seconds and connects automatically once admitted. Normal billable connection setup begins only after admission, when signaling starts; this can precede the first visible frame.

There can be up to 20 waiting sessions per account. Each may wait up to 5 minutes and must poll within 30 seconds to remain in the queue. Closing the page or cancelling removes the request; abandoned entries expire. Queueing does not increase simultaneous capacity. A revoked key, expired wait or exhausted balance prevents admission. For higher simultaneous usage, contact support.

Use the current `https://api.ikawn.com/sdk/mirror.js` entry point, or upgrade your pinned SDK. Older bundles do not understand queued responses. Custom clients must handle `202` with `status: "queued"`, retain the returned scoped token, and POST `{}` to `/sessions/{id}/queue` every 3 seconds. Wait until a connection is returned before publishing media. POST `{}` to `/sessions/{id}/end` to cancel. Reuse the original idempotency key after an uncertain create; do not create a new queue entry on each retry.



```
const waiting = new AbortController();
const client = createMirrorClient({
  sessionProvider, // forward capability and queue; return 201 or 202 JSON unchanged
  onQueue: ({ position, status }) => {
    if (status !== 'admitted') showWaiting(position); // no charge while waiting
  }
});
cancelButton.onclick = () => waiting.abort();
const live = await client.connect(camera, {
  referenceImage: garmentFile,
  signal: waiting.signal,
  onRemoteStream
});
```



For explicit fail-fast behavior, send `{"capability":"tryon","queue":false}`. Full waiting queues still return `429 queue_full`, and request-rate limits still apply. Photo-generation capacity is separate from this live-session queue.

Sessions run while connected and funded. Call `disconnect()` on the raw SDK connection, or `stop()` in the wrapper above, when the user stops or leaves your experience. Stop your camera tracks too.

A response with `status: "ending"` means shutdown is not confirmed yet. Keep this state visible; do not label it as a completed session.

### Disconnects, retries and cleanup

- The SDK sends a best-effort stop on page exit. Browser delivery is not guaranteed; use explicit disconnect whenever possible.

- An established stream becomes stale after 30 seconds without a heartbeat. The next successful one-minute watchdog run requests cleanup, normally up to about 90 seconds plus service latency. Startup has a separate 150-second grace.

- Outages can delay shutdown. Key expiry, revocation, exhausted seconds or service failure can also end access.

- A session still allocating or awaiting reconciliation blocks a duplicate allocation. Retry the original request ID rather than generating new ones.
