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

# TypeScript

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

The TypeScript SDK runs on Node 20 and newer, in browsers, in React Native and in edge
runtimes. It has no runtime dependencies.

```bash theme={null}
npm install @technicallyfit/sdk
```

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

```ts theme={null}
import { createClient } from '@technicallyfit/sdk';

const tf = createClient({
  credential: process.env.TF_API_KEY ?? '',
  baseUrl: process.env.TF_API_BASE,
});

const organization = await tf.GET('/v1/organization');
console.log(organization.name);
```

`baseUrl` defaults to the production API. `credential` is an organization key, a teammate
token, or a function that returns one (for a key you load from a secret store). Keep
organization keys on your servers.

## Typed calls

Each method takes a path from the [API reference](https://docs.technicallyfit.com/api-reference) and is typed from it:
path parameters, query parameters, the body and the answer.

```ts theme={null}
import { createClient } from '@technicallyfit/sdk';

const tf = createClient({ credential: process.env.TF_API_KEY ?? '', baseUrl: process.env.TF_API_BASE });

const teammate = await tf.PUT('/v1/teammates/{external_id}', {
  params: { path: { external_id: 'member-1042' } },
  body: { display_name: 'Sam Rivera', timezone: 'America/Chicago' },
});
console.log(teammate.status);

const today = await tf.GET('/v1/teammates/{external_id}/sessions/today', {
  params: { path: { external_id: 'member-1042' } },
});
console.log(today);
```

## Pagination

`paginate` follows `next_cursor` for every list that has one and yields each item.

```ts theme={null}
import { createClient } from '@technicallyfit/sdk';

const tf = createClient({ credential: process.env.TF_API_KEY ?? '', baseUrl: process.env.TF_API_BASE });

for await (const teammate of tf.paginate('/v1/teammates', { params: { query: { status: 'claimed' } } })) {
  console.log(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).

```ts theme={null}
import { createClient, event, observation } from '@technicallyfit/sdk';

const tf = createClient({ credential: process.env.TF_API_KEY ?? '', baseUrl: process.env.TF_API_BASE });

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

for (const answer of result.results) {
  if (answer.status === 'rejected') console.log(answer.index, answer.reason?.code);
}
```

## Errors

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

```ts theme={null}
import { ConsentRequiredError, NotFoundError, RateLimitError, TechnicallyFitError, createClient } from '@technicallyfit/sdk';

const tf = createClient({ credential: process.env.TF_API_KEY ?? '', baseUrl: process.env.TF_API_BASE });

try {
  await tf.GET('/v1/teammates/{external_id}', { params: { path: { external_id: 'member-1042' } } });
} catch (error) {
  if (error instanceof NotFoundError) console.log('No such teammate.');
  else if (error instanceof ConsentRequiredError) console.log('The teammate has not allowed', error.details['item']);
  else if (error instanceof RateLimitError) console.log('Try again in', error.retryAfterSeconds, 'seconds.');
  else if (error instanceof TechnicallyFitError) console.log(error.code, error.requestId);
  else throw error;
}
```

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

```ts theme={null}
import { createClient } from '@technicallyfit/sdk';

const tf = createClient({
  credential: process.env.TF_API_KEY ?? '',
  baseUrl: process.env.TF_API_BASE,
  retry: { maxAttempts: 5, maxRetryAfterSeconds: 120 },
  onRateLimit: (limit) => {
    if (limit.remaining !== null && limit.remaining < 5) console.log('Close to the rate limit.');
  },
});

await tf.GET('/v1/organization', { retry: false });
```

A `Retry-After` longer than `maxRetryAfterSeconds` (60 by default) is not waited out: the
call throws `RateLimitError` with `retryAfterSeconds`, 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 `null`:

```ts theme={null}
import { createClient } from '@technicallyfit/sdk';

const tf = createClient({ credential: process.env.TF_API_KEY ?? '', baseUrl: process.env.TF_API_BASE });

for await (const event of tf.paginate('/v1/events', {
  params: { query: { status: 'accepted', limit: 50 } },
})) {
  console.log(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.

```ts theme={null}
import {
  RefreshingTeammateCredential,
  beginTeammateSignIn,
  completeTeammateSignIn,
  createClient,
} from '@technicallyfit/sdk';
import type { TeammateSignInConfig } from '@technicallyfit/sdk';

const signIn: TeammateSignInConfig = {
  issuer: process.env.TF_API_BASE ?? '',
  clientId: 'your-client-id',
  redirectUri: 'com.example.gym:/callback',
  scopes: ['teammates:read', 'programs:read'],
};

// 1. Open pending.url in the system browser, and keep pending until the redirect returns.
const pending = await beginTeammateSignIn(signIn);

// 2. Your redirect address receives the answer.
const callbackUrl = 'com.example.gym:/callback?code=...&state=...';
const tokens = await completeTeammateSignIn(signIn, pending, callbackUrl);

// 3. Call the API as that teammate. New tokens arrive in onTokens: store them securely.
const credential = new RefreshingTeammateCredential(signIn, tokens, {
  onTokens: (next) => console.log('save the new tokens', next.expiresAt),
});
const tf = createClient({ credential, baseUrl: process.env.TF_API_BASE });
const me = await tf.GET('/v1/teammates/@me');
console.log(me.account_url);

// Signing out revokes the sign-in.
await credential.signOut();
```

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


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