Getting Started
Installation
Get started with Limitly in seconds. Install the SDK and you're ready to protect your APIs - no configuration needed.
Installation
Install Limitly in seconds. No API keys or Redis setup required.
Prerequisites
- Node.js 14+
- npm
Install
npm install limitly-sdkBasic Usage
Recommended: Use your own Redis
import { createClient } from 'limitly-sdk';
// Recommended for production: Use your own Redis
const client = createClient({
redisUrl: process.env.REDIS_URL || 'redis://localhost:6379',
serviceId: 'my-app'
});
const result = await client.checkRateLimit('user-123');
if (result.allowed) {
console.log(`Allowed. ${result.remaining} remaining.`);
}Without Redis URL (development/testing):
// ⚠️ Shares hosted Redis - may collide with other users
const client = createClient({ serviceId: 'my-app' });Configuration
Bring Your Own Redis (Recommended)
Always use your own Redis for production to avoid tenant collisions:
// Recommended: Full tenant isolation
const client = createClient({
redisUrl: process.env.REDIS_URL || 'redis://localhost:6379',
serviceId: 'my-app'
});Without Redis URL:
// ⚠️ Shares hosted Redis - may collide with other users using same serviceId
const client = createClient({ serviceId: 'my-api-service' });Framework Setup
Next.js
// lib/rate-limit.ts
import { createClient } from 'limitly-sdk';
// Recommended: Use your own Redis
export const client = createClient({
redisUrl: process.env.REDIS_URL,
serviceId: 'nextjs-api'
});
// app/api/route.ts
export async function GET(request: Request) {
const userId = request.headers.get('x-user-id') || 'anonymous';
const result = await client.checkRateLimit(userId);
if (!result.allowed) {
return Response.json({ error: 'Rate limit exceeded' }, { status: 429 });
}
return Response.json({ success: true });
}Express.js
// middleware/rate-limit.ts
import { createClient } from 'limitly-sdk';
// Recommended: Use your own Redis
const client = createClient({
redisUrl: process.env.REDIS_URL,
serviceId: 'express-api'
});
export async function rateLimitMiddleware(req, res, next) {
const identifier = req.user?.id || req.ip || 'anonymous';
const result = await client.checkRateLimit(identifier);
if (!result.allowed) {
return res.status(429).json({ error: 'Rate limit exceeded' });
}
next();
}Troubleshooting
Connection errors: Check internet connection and firewall settings
Timeout errors: Increase timeout: timeout: 10000
Not working: Verify identifier is passed and service ID is correct