Repository navigation
Counting API usage per customer in Next.js route handlers, without a background process #101024
Replies: 1 comment 4 replies
|
Hii @supertj This is an interesting approach, especially for serverless environments where traditional background timers aren't reliable. A few things I found worth highlighting:
Overall, this is a clean approach to API metering in serverless Next.js applications. The load-testing results are also encouraging. I'd be interested to see how this performs under real-world failure scenarios, especially during instance termination and network interruptions. If this answer solve your all doubts so can you please mark my answer as solved |
Uh oh!
There was an error while loading. Please reload this page.
On a long-running Node server, usage metering is simple: keep counts in memory and send them every minute on a timer. Next.js route handlers on Vercel don't give you that. An instance can be frozen between requests, so a timer may never fire, and anything still in memory when the instance goes away is lost.
What we ended up doing in the UsageRail SDK is moving all the background work into
after()fromnext/server. Each handler is wrapped once; the wrapper checks the API key, rate limit and quota before the handler runs, and after the response is sent,after()sends the counts and picks up changes such as revoked keys:Things that are specific to Next.js:
rail.route()takes the template as its first argument, written like the folder:/v1/items/[id]. If you leave it to the raw path, every ID becomes its own row in your usage data.node:crypto, which the Edge runtime doesn't have. Route handlers default to Node.js, so this only bites if you setexport const runtime = "edge".consumerhas to be synchronous. If you identify callers with your own auth (auth: "custom"), the function that returns the account ID can't await a database lookup. Do the lookup inmiddleware.tsand pass the ID on in a request header your middleware sets, and make sure the middleware overwrites that header on every request, so a caller can't send their own.redirect()andnotFound()throw. The wrapper catches them and records the real status: a 307 or 308 counts as usage, a 404 doesn't. Any other thrown error is recorded as a 500, which doesn't count either.next builddoesn't need the key. It's only required when the module loads at run time, so builds that get secrets at run time still work.The part we were least sure about was
after()itself: if the platform stops an instance before that work finishes, the unsent counts are gone. In our October 2026 tests on Vercel, nothing was lost: every successful response was counted in a run of 60,000 requests at 100 a second, a run of 120 requests a minute apart over two hours, and a burst of 70 simultaneous requests that started 70 new instances.Disclosure: I work on UsageRail. Setup, options and how counts get sent: https://usagerail.com/docs/nextjs
All reactions