The SDK does two things. guard and hold put a limit around your
code in one call. And every API operation is a typed method, so your
editor knows each field and answer.
console.log(`Try again in ${error.retryAfterSeconds} seconds`);
}
}
When you only know the cost afterwards, such as an AI reply your product
pays for by the token, hold reserves an estimate (5 cents here), runs your code and
charges what it actually used. If your
code throws, the hold is released:
Each API operation is a method, named like its command in the
CLI: limitry limits create is
limitry.limits.create().
import { createClient } from"@limitry/sdk";
const limitry = createClient();
await limitry.limits.create({
name: "AI replies per day",
subjectKind: "user",
action: "ai-call",
amount: 100,
window: "day",
});
const { items } = await limitry.limits.list();
Ids in the path are arguments, and everything else is one object:
limitry.limits.get(limitId).
A method returns the response’s data. A failure throws an ApiError
with the error’s code, message, status and
requestId.
Every write sends an idempotency key, and a
failed attempt (a network error, a 5xx, a 429) is retried with the
same key, so it happens once. To make your own retry safe too, pass
your key as the last argument:
limitry.checks.create(check, { idempotencyKey: id }).
Every method, linked to its fields and a full example under
Endpoints:
createClient() reads your key from the LIMITRY_API_KEY
environment variable. Without it, it throws an error naming the
variable. Where there’s no process.env, such as a browser or a
Cloudflare Worker, pass the key: createClient({ token: env.LIMITRY_API_KEY }).
A test key (lim_test_…) acts in test, and a live key (lim_live_…)
in live. See Test and live.
A personal access token names the workspace, and acts in test
unless you name live:
createClient({ token, workspace: "acme", environment: "live" }).
limitry.GET, limitry.POST and the other HTTP methods call any path,
with the same types. They return { data, error, response } instead of
throwing, and unwrap turns that into the data or an ApiError.