@limityourapi/node
v3.0.1
Published
Official Node.js SDK for LimitYourAPI — API Revenue Infrastructure. Quota enforcement, billing sync, and AI cost protection.
Downloads
103
Maintainers
Readme
@limityourapi/node
Official Node.js SDK for LimitYourAPI — API Revenue Infrastructure for quota enforcement, billing sync, and AI cost protection.
LimitYourAPI lets you meter usage, enforce entitlements, sync customer billing states, and protect AI budgets with sub-millisecond decision latency.
1. What is LimitYourAPI?
LimitYourAPI is API Revenue Infrastructure designed to sit in your API path. Instead of just blocking bad actors, it maps API keys to Stripe/Razorpay subscriptions, tracks consumer usage profiles in real-time, alerts on quota thresholds, and implements limits on high-cost AI tokens or computational endpoints before they occur.
2. Why not Redis?
While you can build basic rate limiting in Redis using custom scripts:
- No Bounded Logic: Custom Redis scripts do not have a built-in state management for Stripe pricing tables, trial durations, or tier features.
- Race Conditions: Implementing multiple concurrent limits (e.g. daily quota + concurrent burst protection) across different meters is complex and prone to latency/race bugs.
- Fail-Open Complexity: Writing fail-safe edge routing, backoffs, and circuit breaking in your app code takes weeks of engineering.
- No Observability: Redis does not provide out-of-the-box usage pipelines, Stripe sync triggers, or upgrade signals.
LimitYourAPI abstracts this database hot-path logic into a single sub-millisecond check.
3. Install
Install the package via npm:
npm install @limityourapi/nodeEnsure you have Node.js 18 or higher (uses native, zero-dependency fetch).
4. First enforce()
Import the client and verify customer entitlements against a meter:
import LimitYourAPI from "@limityourapi/node";
const limit = new LimitYourAPI({
apiKey: process.env.LIMIT_API_KEY, // rl_...
});
const result = await limit.enforce({
customer: "cus_98234",
meter: "api_calls",
consume: 1
});
console.log(result.allowed); // true
console.log(result.remaining); // 9999
console.log(result.resetIn); // 1726355. Billing example
Link customer usage directly to Stripe plans:
const result = await limit.enforce({
customer: "cus_123",
meter: "api_calls",
consume: 10,
route: "/v1/inference"
});
if (!result.allowed && result.upgradeAvailable) {
console.log(`Plan ${result.plan} usage limits hit. Redirecting customer to Stripe upgrade...`);
// Redirect to result.reason or Stripe Portal
}6. AI token example
Prevent runaway LLM costs by tracking token usage before processing:
// Estimate token usage (e.g. using tiktoken)
const tokensRequired = 2500;
const result = await limit.enforce({
customer: "cus_123",
meter: "ai_tokens",
consume: tokensRequired
});
if (!result.allowed) {
throw new Error("AI budget warning: Monthly AI token limit exceeded.");
}
// Call your OpenAI / Anthropic models safely...7. Dashboard preview
Monitor revenue protection, upgrade signals, AI token spend, and active API customer rollups inside the Stripe-inspired LimitYourAPI Dashboard V3:
- Revenue Protected: Value of consumption checked and safely metered.
- API Customers: Interactive list of consuming identifiers and plan states.
- Upgrade Opportunities: Real-time listing of customers nearing 80%+ limit usage.
- AI Cost Protected: USD projections of LLM token usages.
8. SDK Links
License
Apache-2.0 © LimitYourAPI
