Guides / SaaS
API usage tracking helps you:
// middleware/api-usage.ts (Express/Node.js)
import { logit } from "@/lib/logit";
export async function apiUsageMiddleware(req: Request, res: Response, next: NextFunction) {
const start = Date.now();
const customerId = req.user?.organizationId ?? "anonymous";
const plan = req.user?.plan ?? "unknown";
res.on("finish", async () => {
const duration = Date.now() - start;
await logit.now("api-usage", {
event: "API request",
description: `${req.method} ${req.path} — ${res.statusCode} (${duration}ms)`,
icon: "⚡",
notify: false,
tags: {
customerId,
plan,
method: req.method,
status: res.statusCode.toString(),
endpoint: req.path,
},
metadata: {
customerId,
duration,
statusCode: res.statusCode,
path: req.path,
method: req.method,
},
}).catch(console.error); // fire-and-forget
});
next();
}
// For expensive operations (AI calls, large exports), log separately
await logit.now("api-usage-premium", {
event: "Premium API call",
description: `Customer ${customerId} — /api/ai/generate (${tokens} tokens)`,
icon: "🤖",
notify: false,
tags: { customerId, plan, operation: "ai-generate" },
metadata: { customerId, tokens, model, duration },
});
| Field | Value |
|---|---|
| Name | Customer API abuse |
| Channel | api-usage |
| Event name | API request |
| Threshold | 1000 |
| Window | 1 hour |
| Cooldown | 1 hour |
| Action | Push notification |
| Push title | ⚡ API usage spike |
| Push body | 1000+ API calls in 1 hour |
Since customerId is a tag (not just metadata), you can filter the api-usage channel by customer ID in the LogIt dashboard to see their full usage timeline.
api-usage channel with notify: false — you don't want a ping per API call.duration in metadata to identify slow endpoints alongside usage volume.Try LogIt free
7-day trial. No credit card required.