Skip to content

TypeScript SDK

@detent/sdk is the official TypeScript/JavaScript client for Detent. Source on GitHub.

Terminal window
npm install @detent/sdk
import { Detent } from "@detent/sdk";
const detent = new Detent({ apiKey: process.env.DETENT_KEY });
import { Detent } from "@detent/sdk";
const detent = new Detent({ apiKey: process.env.DETENT_KEY });
const { allowed, remaining } = await detent.limit({
namespace: "checkout", key: "user_42",
algorithm: "sliding_window", limit: 100, windowMs: 60_000,
});

limit() takes { namespace, key, algorithm, limit, windowMs } and resolves to { allowed, remaining, reset_ms, limit } — see fail-open & the verdict model for what the fields mean and how /v1/limit behaves during an outage.

For concurrent_limit (see concurrency & leases), the SDK exposes acquire()/release() plus a withLease() helper that releases automatically:

const lease = await detent.acquire({ namespace: "checkout", key: "user_42", limit: 5, windowMs: 300_000 });
if (lease.allowed) {
try {
// do the work
} finally {
await detent.release(lease.leaseId);
}
}
// or, equivalently:
await detent.withLease({ namespace: "checkout", key: "user_42" }, async () => {
// do the work
});

Unlike /v1/limit, the SDK does not fail open on the network by default for leases — if a lease request can’t reach Detent, acquire()/withLease() reject rather than silently letting the work proceed. This is a deliberate default: an in-flight concurrency guard that fails open under a network partition can let unbounded work through. Configure this behavior explicitly if your use case needs different handling.