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

# Real-time

> Stream a teammate's session over WebSocket for live cues during the workout, then fold the session once at the end into ordinary observations.

> [!NOTE]
>
> The organization API is in early access and is not yet available to every organization.
> Live sessions are currently enabled in staging and gated behind early access in production.

A live session lets your app stream readings over a WebSocket while a teammate trains. The
live gateway acknowledges each numbered batch, emits live cues as thresholds are crossed,
and lets a dropped client resume where it left off. When the session ends, TechnicallyFit
folds the session's readings once into summaries and stores them through the same ingestion
path as [Sending events](https://docs.technicallyfit.com/guides/sending-events.md).

* **Authentication:** Connect to `wss://<host>/v1/live` with an organization key
  (`X-API-Key`) or a teammate token (`Authorization: Bearer <token>`) carrying the
  `events:write` scope.
* **Consent:** Before a teammate claims their account, live sessions are always accepted.
  After the claim, the teammate must allow the `events` consent item.
* **One ingestion path:** A live session does not write raw samples to the teammate's
  history. When you close the session, the folded summaries enter through the same consent,
  route limit, and duplicate checks as `POST /v1/teammates/{external_id}/events`.

Set these two values once in your terminal. Every example in these docs reads them, so
no example has a host or a key typed into it.

```bash theme={null}
export TF_API_BASE="https://api.technicallyfit.com"
export TF_API_KEY="tf_test_your_key_here"
```

Use a test key (it starts with `tf_test_`) while you build. Never paste a live key into a
page, a ticket or a chat.

## Opening and streaming a session

Open a session for one teammate, send live readings in numbered batches, listen for live
cues, and close the session when the workout finishes. Both published SDKs provide a live
helper (`openLiveSession` in `@technicallyfit/sdk` and `open_live_session` in
`technicallyfit[live]`).

```typescript TypeScript theme={null}
import { openLiveSession } from '@technicallyfit/sdk';

const session = await openLiveSession({
  credential: process.env.TF_API_KEY ?? '',
  baseUrl: process.env.TF_LIVE_BASE ?? 'wss://live.technicallyfit.com',
  teammate: 'member-1042',
  onCue: (cue) => {
    console.log('Live cue:', cue.rule_id, cue.cue_id, cue.payload);
  },
});

const ack = await session.sendReadings([
  {
    quantity: 'bar_velocity',
    measure: 'mean',
    value: 0.72,
    unit: 'm/s',
    at: '2026-11-27T15:00:01Z',
    set_index: 1,
  },
  {
    quantity: 'bar_velocity',
    measure: 'mean',
    value: 0.54,
    unit: 'm/s',
    at: '2026-11-27T15:00:04Z',
    set_index: 1,
  },
]);
console.log('Acknowledged seq:', ack.seq, 'accepted:', ack.accepted_count);

const closed = await session.close();
console.log('Folded readings:', closed.reading_count, closed.results);
```

```python Python theme={null}
import os

from technicallyfit import open_live_session

session = open_live_session(
    credential=os.environ["TF_API_KEY"],
    base_url=os.environ.get("TF_LIVE_BASE", "wss://live.technicallyfit.com"),
    teammate="member-1042",
    on_cue=lambda cue: print("Live cue:", cue.rule_id, cue.cue_id, cue.payload),
)

ack = session.send_readings(
    [
        {
            "quantity": "bar_velocity",
            "measure": "mean",
            "value": 0.72,
            "unit": "m/s",
            "at": "2026-11-27T15:00:01Z",
            "set_index": 1,
        },
        {
            "quantity": "bar_velocity",
            "measure": "mean",
            "value": 0.54,
            "unit": "m/s",
            "at": "2026-11-27T15:00:04Z",
            "set_index": 1,
        },
    ]
)
print("Acknowledged seq:", ack.seq, "accepted:", ack.accepted_count)

closed = session.close()
print("Folded readings:", closed.reading_count, closed.results)
```

## Wire protocol

Every message on `/v1/live` is a JSON object with a `type` field.

### 1. Open a session

Send `session.open` after the WebSocket connects. Pass an optional `idempotency_key` if your
client may retry opening the same session.

```json Client -> Server theme={null}
{
  "type": "session.open",
  "teammate": "member-1042",
  "idempotency_key": "live-demo-2026-11-27-01",
  "source": { "route": "gym-floor", "device": "bar-sensor" }
}
```

```json Server -> Client theme={null}
{
  "type": "session.opened",
  "session_id": "7d2f1a8c-4b9e-4c1d-8a3f-1e2d3c4b5a60",
  "resumed": false,
  "last_acked_seq": 0
}
```

### 2. Stream numbered batches and acknowledgements

Send readings in `readings.batch` frames with a monotonically increasing `seq` starting at
`1`. The gateway acknowledges each batch with `readings.ack`. If you resend a batch whose
`seq` was already acknowledged, the gateway acknowledges it again without double-counting
its readings.

```json Client -> Server theme={null}
{
  "type": "readings.batch",
  "seq": 1,
  "readings": [
    {
      "quantity": "bar_velocity",
      "measure": "mean",
      "value": 0.72,
      "unit": "m/s",
      "at": "2026-11-27T15:00:01Z",
      "set_index": 1
    }
  ]
}
```

```json Server -> Client theme={null}
{
  "type": "readings.ack",
  "seq": 1,
  "accepted": 1
}
```

### 3. Live cues

While readings arrive, the gateway tracks session summaries (such as a mean, a peak, or
velocity loss within a set) and evaluates live cue rules. When a cue fires, the gateway
sends a `cue.emitted` frame. Each cue respects a cooldown window so the same cue does not
repeat on every rep, even if the client disconnects and resumes.

```json Server -> Client theme={null}
{
  "type": "cue.emitted",
  "cue_id": "vel-loss-set-1-1",
  "rule_id": "velocity_loss_stop",
  "rule_version": 1,
  "message": "Velocity dropped more than 20 percent from the first rep of the set.",
  "at": "2026-11-27T15:00:04Z",
  "set_index": 1
}
```

### 4. Resuming after a drop

If the network drops or a server restarts during a deploy, reconnect to `/v1/live` and send
`session.resume` with the `session_id` and your `last_acked_seq`. The gateway restores your
session checkpoint and cue cooldowns, replies with `session.opened` (`"resumed": true`), and
you continue from `last_acked_seq + 1`.

```json Client -> Server theme={null}
{
  "type": "session.resume",
  "session_id": "7d2f1a8c-4b9e-4c1d-8a3f-1e2d3c4b5a60",
  "last_acked_seq": 1
}
```

```json Server -> Client theme={null}
{
  "type": "session.opened",
  "session_id": "7d2f1a8c-4b9e-4c1d-8a3f-1e2d3c4b5a60",
  "resumed": true,
  "last_acked_seq": 1
}
```

### 5. Closing and the close answer

Send `session.end` when the workout finishes. The gateway folds the session's readings once
into summaries and writes them through the events ingestion path. The `session.ended` frame
returns one result per folded observation, using the same `accepted`, `duplicate`, and
`rejected` statuses as [Sending events](https://docs.technicallyfit.com/guides/sending-events.md#results).

```json Client -> Server theme={null}
{
  "type": "session.end"
}
```

```json Server -> Client theme={null}
{
  "type": "session.ended",
  "session_id": "7d2f1a8c-4b9e-4c1d-8a3f-1e2d3c4b5a60",
  "reading_count": 2,
  "results": [
    {
      "index": 0,
      "status": "accepted",
      "event_id": "3cac0ee1-ee26-42f5-a51c-208e776a4ead",
      "type": "observation"
    }
  ]
}
```

| Status | Meaning |
| - | - |
| `accepted` | Folded summary was stored as an observation for the teammate. |
| `duplicate` | Already stored under this session's key; nothing changed. |
| `rejected` | Not stored. `reason.code` and `reason.message` explain why. |

## Limits and errors

Every refusal or error arrives as a `session.error` frame with a stable `code`, a plain
sentence `message`, and a `retryable` flag.

```json Server -> Client theme={null}
{
  "type": "session.error",
  "code": "RATE_LIMIT_EXCEEDED",
  "message": "Too many readings sent per second for this live session. Slow down and retry.",
  "retryable": true
}
```

| Code | Meaning |
| - | - |
| `UNAUTHORIZED` | Missing or invalid credential, or missing `events:write` scope. |
| `CONSENT_REQUIRED` | The teammate claimed their account and has not enabled the `events` consent item. |
| `FEATURE_DISABLED` | Live sessions are not enabled for this organization. |
| `SESSION_LIMIT_EXCEEDED` | The organization or gateway has reached its concurrent live session limit. |
| `RATE_LIMIT_EXCEEDED` | Readings arrived faster than the per-session readings-per-second limit. |
| `SESSION_NOT_FOUND` | The `session_id` sent to `session.resume` has expired or does not exist. |

See [Rate limits](https://docs.technicallyfit.com/guides/rate-limits.md#live-session-limits) and [Plans](https://docs.technicallyfit.com/guides/plans.md) for
the default live session limits and how organization contracts configure them.


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