import express from 'express';
import { Limitly } from '@limitly/limitly-js';
const app = express();
const limitly = new Limitly({
apiKey: process.env.LIMITLY_API_KEY!
});
app.use(async (req, res, next) => {
const apiKey = req.headers['x-api-key'] as string;
if (!apiKey) {
return res.status(401).json({ error: 'API Key required' });
}
const result = await limitly.validation.validate(
apiKey,
req.path,
req.method
);
if (!result.success) {
return res.status(429).json({ error: 'Rate limit exceeded' });
}
next();
});
app.get('/api/users', (req, res) => {
res.json({ message: 'Users retrieved successfully' });
});