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

# Sample: send a gym's history

> Send weeks of past visits and logged sets in bulk with the TypeScript SDK, with keys that make a rerun safe.

When a gym starts with TechnicallyFit it often has months of visits and training on record
already. This sample sends that history through `POST /v1/events:bulk` with the TypeScript
SDK. The same pattern works for a nightly import from a door system or a training app.

## Set up the location and its equipment first

A visit names a location by its `external_id`, so the location must exist before its history
arrives. `PUT` creates it or updates it in place, and the equipment calls replace the whole
list each time, so a rerun leaves the same state.

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

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

await tf.PUT('/v1/locations/{location_external_id}', {
  params: { path: { location_external_id: 'club-014' } },
  body: { name: 'Downtown', time_zone: 'America/New_York' },
});
await tf.PUT('/v1/inventory/baseline', {
  body: {
    items: [
      { token: 'barbell', count: 8 },
      { token: 'dumbbell', count: 20, attributes: { max_load_kg: 50 } },
      { token: 'rower', count: 4 },
    ],
  },
});
await tf.PUT('/v1/locations/{location_external_id}/overrides', {
  params: { path: { location_external_id: 'club-014' } },
  body: { overrides: [{ kind: 'out_of_service', token: 'rower', note: 'awaiting part' }] },
});
```

The key needs `locations:write` for these calls and `events:write` for the history.

## Key every event by what it is

Give each event a key built from the teammate and the thing it records, never a random one.
A visit is keyed by the teammate and the day; a set by the teammate, the day and the set. Run
the import again and every event answers `duplicate`, so nothing is stored twice.

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

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

/** One visit to a location and the three sets done on it. */
function visitWithSets(teammate: string, day: string, at: string, location: string): BulkEvent[] {
  const visit: BulkEvent = {
    type: 'visit_check_in',
    teammate,
    at,
    idempotency_key: `history:${teammate}:visit:${day}`,
    data: { location },
  };
  const sets = [1, 2, 3].map(
    (number): BulkEvent => ({
      ...event.loggedSet({
        at,
        route: 'history-import',
        recordId: `history:${teammate}:${day}:squat:${String(number)}`,
        movement: { name: 'Back squat' },
        set: { number, reps: 5, load: { value: 80, unit: 'kg' } },
      }),
      teammate,
    }),
  );
  return [visit, ...sets];
}

const events = [
  ...visitWithSets('member-001', '2026-11-16', '2026-11-16T12:05:00Z', 'club-014'),
  ...visitWithSets('member-002', '2026-11-16', '2026-11-16T12:09:00Z', 'club-014'),
];
```

* `at` is an RFC 3339 time with an offset. Work it out from the location's own time zone, so
  a 7 o'clock visit counts at 7 o'clock there.
* `location` is the `external_id` of one of your locations. See
  [Locations and equipment](https://docs.technicallyfit.com/guides/locations-and-equipment.md).
* A logged set is keyed by its `source.route` and `source.record_id` instead of an idempotency
  key. See [Sending events](https://docs.technicallyfit.com/guides/sending-events.md).

## Send it and read the results

`sendBulk` checks every event first, then splits the list into requests within the count and
size limits. Each event gets its own result.

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

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

const answer = await tf.events.sendBulk(events);
const counts = { accepted: 0, duplicate: 0, rejected: 0 };
for (const result of answer.results) counts[result.status] += 1;
console.log(counts);
for (const result of answer.results)
  if (result.status === 'rejected') console.log(result.index, result.reason?.code);
```

A first run answers `accepted` for each event. A second run with the same history answers
`duplicate` for each one. A `rejected` event carries its reason, such as `unknown_location`
or `consent_required`; fix it and send that event again with the same key.

## What the gym sees

Visits feed the [traffic figures](https://docs.technicallyfit.com/guides/gym-traffic.md) and the
[at-risk list](https://docs.technicallyfit.com/guides/at-risk-teammates.md), each for the teammates who allow it. Traffic
withholds any hour with fewer than five teammates, so history spread thinly over many hours
shows less than history that clusters at the gym's real peaks. The at-risk check compares a
teammate's last two weeks with the eight before, so send at least ten weeks to see it work.


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