> ## 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.

# Python

> Install the Python SDK, authenticate, make typed calls, send events, handle errors and sign a teammate in from a member app.

The Python SDK runs on Python 3.11 and newer and uses only the standard library.

```bash theme={null}
pip install technicallyfit
```

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.

## Create a client

```python theme={null}
import os

from technicallyfit import Client

tf = Client(os.environ["TF_API_KEY"], base_url=os.environ.get("TF_API_BASE"))

organization = tf.get_organization()
print(organization["name"])
```

`base_url` defaults to the production API. The first argument is an organization key, a
teammate token, or a function that returns one. Keep organization keys on your servers.

## Typed calls

Every operation in the [API reference](https://docs.technicallyfit.com/api-reference) is a method named after it, with
typed parameters and a typed answer (`TypedDict`s generated from the reference).

```python theme={null}
import os

from technicallyfit import Client

tf = Client(os.environ["TF_API_KEY"], base_url=os.environ.get("TF_API_BASE"))

teammate = tf.upsert_teammate("member-1042", {"display_name": "Sam Rivera", "timezone": "America/Chicago"})
print(teammate["status"])

today = tf.get_today_session("member-1042")
print(today["date"], today["session"])
```

## Pagination

`paginate` follows `next_cursor` and yields each item of a list.

```python theme={null}
import os

from technicallyfit import Client

tf = Client(os.environ["TF_API_KEY"], base_url=os.environ.get("TF_API_BASE"))

for teammate in tf.paginate(tf.list_teammates, status="claimed"):
    print(teammate["external_id"])
```

## Send events

`tf.events.send` checks every event before it sends anything, gives each event that needs
one an idempotency key, and splits a long list into requests within the API's limits. The
builders are on [Observation builders](https://docs.technicallyfit.com/sdks/observation-builders.md).

```python theme={null}
import os

from technicallyfit import Client, event, self_report

tf = Client(os.environ["TF_API_KEY"], base_url=os.environ.get("TF_API_BASE"))

result = tf.events.send(
    "member-1042",
    [
        event.logged_set(
            at="2026-11-17T15:02:00Z",
            route="org-floor",
            record_id="org-floor:set:91a7",
            movement={"name": "Back squat"},
            number=2,
            reps=5,
            load={"value": 100, "unit": "kg"},
            effort=8,
        ),
        event.observation(
            self_report(quantity="pain", joint="knee", side="left", value=4, at="2026-11-17T07:30:00Z", confidence=0.8)
        ),
    ],
)

for answer in result["results"]:
    if answer["status"] == "rejected":
        print(answer["index"], answer["reason"]["code"])
```

## Errors

Every error answer raises a class named for its [code](https://docs.technicallyfit.com/guides/errors.md), and every one
extends `TechnicallyFitError` with `code`, `message`, `status`, `request_id` and `details`.
A code this version does not know raises a plain `TechnicallyFitError`.

```python theme={null}
import os

from technicallyfit import Client, ConsentRequiredError, NotFoundError, RateLimitError, TechnicallyFitError

tf = Client(os.environ["TF_API_KEY"], base_url=os.environ.get("TF_API_BASE"))

try:
    tf.get_teammate("member-1042")
except NotFoundError:
    print("No such teammate.")
except ConsentRequiredError as error:
    print("The teammate has not allowed", error.details.get("item"))
except RateLimitError as error:
    print("Try again in", error.retry_after_seconds, "seconds.")
except TechnicallyFitError as error:
    print(error.code, error.request_id)
```

## Retries and rate limits

The client sends a request again after a `429`, and after a network failure, `500`, `502`,
`503` or `504` when sending it again cannot change the outcome (reads, updates, and the
event endpoints, whose events carry their own keys). It waits the `Retry-After` the API
sent, or until the rate-limit window resets, or backs off with jitter. It never retries
another `4xx`.

```python theme={null}
import os

from technicallyfit import Client, RetryOptions

tf = Client(
    os.environ["TF_API_KEY"],
    base_url=os.environ.get("TF_API_BASE"),
    retry=RetryOptions(max_attempts=5, max_retry_after=120),
    on_rate_limit=lambda limit: print("remaining", limit.remaining),
)

tf.get_organization(retry=False)
```

A `Retry-After` longer than `max_retry_after` (60 seconds by default) is not waited out:
the call raises `RateLimitError` with `retry_after_seconds`, so you can schedule the work.

## Page through events

Use `paginate` to walk through paged endpoints such as `GET /v1/events` until `next_cursor` is `None`:

```python theme={null}
import os

from technicallyfit import Client

tf = Client(os.environ["TF_API_KEY"], base_url=os.environ.get("TF_API_BASE"))

for event in tf.paginate(tf.list_events, status="accepted", limit=50):
    print(event["id"], event["type"], event["status"])
```

## Sign a teammate in from a member app

A [member app](https://docs.technicallyfit.com/guides/member-apps.md) holds a teammate token, never an organization key.
The SDK runs the sign-in flow and gives you a credential that refreshes itself. The issuer
is the authorization server you configure.

```python theme={null}
import os

from technicallyfit import (
    Client,
    RefreshingTeammateCredential,
    TeammateSignInConfig,
    begin_teammate_sign_in,
    complete_teammate_sign_in,
)

sign_in = TeammateSignInConfig(
    issuer=os.environ["TF_API_BASE"],
    client_id="your-client-id",
    redirect_uri="com.example.gym:/callback",
    scopes=["teammates:read", "programs:read"],
)

# 1. Open pending.url in the system browser, and keep pending until the redirect returns.
pending = begin_teammate_sign_in(sign_in)

# 2. Your redirect address receives the answer.
callback_url = "com.example.gym:/callback?code=...&state=..."
tokens = complete_teammate_sign_in(sign_in, pending, callback_url)

# 3. Call the API as that teammate. New tokens arrive in on_tokens: store them securely.
credential = RefreshingTeammateCredential(sign_in, tokens, on_tokens=lambda fresh: print("save", fresh.expires_at))
tf = Client(credential, base_url=os.environ["TF_API_BASE"])
print(tf.get_me()["account_url"])

# Signing out revokes the sign-in.
credential.sign_out()
```

When the refresh token no longer works (the teammate left, signed out or the app was
turned off), calls raise `TeammateSignedOutError`: send the person back to sign-in.


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