Using the SDK
@gymgym/sdk is the only way that apps talk to the backend. It bundles three things. If you do not use TypeScript, call the same API with raw GraphQL and a personal API key.
Operations: each named operation in packages/sdk/operations becomes a method with typed variables and a typed result. query Me becomes gym.me(). mutation SaveWorkout becomes gym.saveWorkout(variables):
const gym = createGymClient() // in the browser: same origin, cookiesconst { viewer } = await gym.me()const { bodyweight } = await gym.bodyweight()Auth: gym.auth is a Better Auth client. The anonymous, passkey, admin and device authorization plugins are already set up:
await gym.auth.signIn.social({ provider: 'github', callbackURL: '/app' })await gym.auth.signIn.passkey()Offline queue: the client queues saves that you make offline in gym.queue and replays them in order when the connection returns. Pass storage to keep the queue. The web app passes localStorage and calls gym.queue.flush() when the browser comes back online.
Options
Section titled “Options”| Option | Use |
|---|---|
baseUrl |
The API origin. Leave it out in the browser to use the current origin. |
token |
A bearer token, or a function that returns one, for clients without cookies (CLI, native apps). |
fetch |
A custom fetch for tests, React Native or service bindings. |
onMutation |
Called after every successful mutation, for cache invalidation. |
Errors
Section titled “Errors”A failure throws GymError with a code: UNAUTHENTICATED, FORBIDDEN, NOT_FOUND, BAD_USER_INPUT, NETWORK or HTTP_<status>. The id of someone else and a missing id both give NOT_FOUND.
Adding an operation
Section titled “Adding an operation”Write the query or the mutation in packages/sdk/operations/<area>.graphql and run pnpm contract. The new method appears on the client. If the server field does not exist yet, add it in packages/api first. The isolation test picks up the new operation by itself.