limityourapi
v1.0.0
Published
Official Node.js SDK for LimitYourAPI — production-grade API rate limiting as a service. Express middleware, fail-open, zero dependencies.
Maintainers
Readme
limityourapi
Official Node.js SDK for LimitYourAPI — production-grade API rate limiting as a service.
Add enterprise-grade rate limiting to any Node.js app in 2 lines. Zero dependencies. Fail-open by default. TypeScript support included.
✨ Features
- 🚀 2-line setup — Express middleware included
- 📦 Zero dependencies — uses native
fetch(Node 18+) - 🛡️ Fail-open — your app stays up even if the limiter is down
- 🔧 TypeScript — full type definitions included
- ⚡ Edge support — optional edge-first routing for global apps
- 🪣 Token & cost-based — AI/LLM endpoint support built-in
Installation
npm install limityourapiQuick Start
import { LimitYourAPIClient } from 'limityourapi';
const limiter = new LimitYourAPIClient({
baseUrl: 'https://limityourapi.onrender.com',
apiKey: process.env.LIMITYOURAPI_KEY, // rl_...
});Express Middleware (recommended)
import express from 'express';
import { LimitYourAPIClient } from 'limityourapi';
const app = express();
const limiter = new LimitYourAPIClient({
baseUrl: 'https://limityourapi.onrender.com',
apiKey: process.env.LIMITYOURAPI_KEY,
});
// Protect all routes — one line!
app.use(limiter.middleware());
// Or protect specific routes
app.get('/api/sensitive',
limiter.middleware({ endpoint: '/api/sensitive' }),
(req, res) => res.json({ data: 'protected' })
);
app.listen(3000);Direct Check
const result = await limiter.check({ endpoint: '/api/users' });
console.log(result);
// {
// allowed: true,
// remaining: 98,
// resetIn: 55,
// limit: 100,
// rule: "Default API Limit",
// status: 200
// }
if (!result.allowed) {
console.log(`Rate limited! Retry in ${result.resetIn}s`);
}Token/Cost-Based Limiting (for AI endpoints)
const result = await limiter.checkWithTokens({
endpoint: '/api/ai/generate',
tokens: 150, // tokens consumed
cost: 0.003, // estimated cost in $
});
console.log(result.remainingTokens); // tokens left in window
console.log(result.remainingBudget); // budget left in windowMiddleware Options
| Option | Type | Default | Description |
|--------|------|---------|-------------|
| endpoint | string | req.path | Fixed endpoint to check |
| failOpen | boolean | true | Allow requests if limiter is down |
| onBlocked | function | null | Custom 429 handler |
| getTokenCount | function | null | Extract token count from request |
| getEstimatedCost | function | null | Extract cost from request |
Custom Block Handler
app.use(limiter.middleware({
onBlocked: (req, res, result) => {
res.status(429).json({
error: 'Slow down!',
retry_after: result.resetIn,
upgrade_url: 'https://limityourapi.onrender.com/pricing',
});
},
}));Response Headers
The middleware automatically sets these standard headers:
| Header | Description |
|--------|-------------|
| X-RateLimit-Limit | Maximum requests allowed |
| X-RateLimit-Remaining | Requests remaining in window |
| X-RateLimit-Reset | Seconds until window resets |
Fail-Open Behavior
By default, if the rate limiter service is unreachable or times out, requests are allowed through (fail-open). This ensures your app stays available even during limiter outages.
// Override: reject requests when limiter is down
app.use(limiter.middleware({ failOpen: false }));Client Options
| Option | Type | Default | Description |
|--------|------|---------|-------------|
| baseUrl | string | required | LimitYourAPI service URL |
| apiKey | string | required | Your API key (rl_...) |
| timeout | number | 5000 | Request timeout in ms |
| edgeUrl | string | null | Edge endpoint URL |
| useEdge | boolean | false | Route via edge first |
Getting Your API Key
- Sign up at LimitYourAPI Dashboard
- Navigate to API Keys → Create Key
- Copy your key (starts with
rl_)
Or via CLI:
# Register
curl -X POST https://limityourapi.onrender.com/auth/register \
-H "Content-Type: application/json" \
-d '{"email":"[email protected]","password":"SecurePass123!"}'
# Login
TOKEN=$(curl -s -X POST https://limityourapi.onrender.com/auth/login \
-H "Content-Type: application/json" \
-d '{"email":"[email protected]","password":"SecurePass123!"}' | jq -r '.token')
# Create API Key
curl -X POST https://limityourapi.onrender.com/apikeys \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{"name":"my-app"}'License
MIT © Yash
