oRPC is a TypeScript library for type-safe RPC that can also serve plain REST routes with an OpenAPI spec. Version 1 ships rate limiting in a separate package, @orpc/experimental-ratelimit. I wanted to know whether it holds up for an API you charge for, where the limit belongs to a customer and not to an IP address. So on 25 November 2026 I wired it into a small server and looked at what came back over HTTP.
About a year after its first release, the v1 package still says "experimental", and npm lists one maintainer. On 25 November, oRPC's sponsors page showed 41 active sponsors, $1,575 pledged for the month and a median pledge of $10. So I checked the 1.16.6 source against the HTTP responses below rather than relying on the docs alone.
Key by customer, not by API key
With one bucket per API key, a customer with five keys can make five times as many requests. The five-key example comes from a six-slide deck on per-customer API rate limiting. In this server the auth middleware runs first, so the rate limit middleware's key function can read context.customer.customerId.
Here is the whole server. Two keys belong to the same customer, and a free and a pro plan get separate limiters:
import { createServer } from "node:http";
import { os, ORPCError } from "@orpc/server";
import { OpenAPIHandler } from "@orpc/openapi/node";
import { createRatelimitMiddleware, RatelimitHandlerPlugin } from "@orpc/experimental-ratelimit";
import { MemoryRatelimiter } from "@orpc/experimental-ratelimit/memory";
// Two keys, one customer. In a real API this lookup hits your key store.
const keys = new Map([
["key_a1", { customerId: "cus_acme", plan: "free" }],
["key_a2", { customerId: "cus_acme", plan: "free" }],
["key_b1", { customerId: "cus_beta", plan: "pro" }],
]);
const limiters = {
free: new MemoryRatelimiter({ maxRequests: 3, window: 10_000 }),
pro: new MemoryRatelimiter({ maxRequests: 30, window: 10_000 }),
};
const base = os.$context();
const authed = base.use(({ context, next }) => {
const header = context.headers.authorization ?? "";
const customer = keys.get(header.replace(/^Bearer /, ""));
if (!customer) throw new ORPCError("UNAUTHORIZED");
return next({ context: { customer } });
});
const limited = authed.use(
createRatelimitMiddleware({
limiter: ({ context }) => limiters[context.customer.plan],
key: ({ context }) => `customer:${context.customer.customerId}`,
}),
);
const router = {
hello: limited
.route({ method: "GET", path: "/hello" })
.handler(({ context }) => ({ customer: context.customer.customerId })),
};
const handler = new OpenAPIHandler(router, { plugins: [new RatelimitHandlerPlugin()] });
createServer(async (req, res) => {
const { matched } = await handler.handle(req, res, { context: { headers: req.headers } });
if (!matched) { res.statusCode = 404; res.end("not found"); }
}).listen(3127, "127.0.0.1");
I installed @orpc/server, @orpc/openapi and @orpc/experimental-ratelimit, all at 1.16.6, and ran it on Node 26.3. I alternated key_a1 and key_a2 for four requests, then tried the pro customer's key_b1:
key_a1 200 ratelimit-remaining: 2
key_a2 200 ratelimit-remaining: 1
key_a1 200 ratelimit-remaining: 0
key_a2 429 retry-after: 10
key_b1 200 ratelimit-limit: 30, ratelimit-remaining: 29
The two free keys shared one bucket, and key_b1 had 29 requests left after its first call. A request with no key got a 401 before it reached the limiter.
What a 429 looks like
The middleware throws oRPC's TOO_MANY_REQUESTS error, and the handler plugin adds the rate limit headers. Here's the status, those headers and the JSON body:
HTTP/1.1 429 Too Many Requests
ratelimit-limit: 3
ratelimit-remaining: 0
ratelimit-reset: 1795614179717
retry-after: 10
{"defined":false,"code":"TOO_MANY_REQUESTS","status":429,"message":"Too Many Requests","data":{"limit":3,"remaining":0,"reset":1795614179717}}
In 1.16.6, ratelimit-reset is a Unix timestamp in milliseconds. GitHub's x-ratelimit-reset is in seconds, and a client that parses every reset header as seconds would read this one as about 57,000 years from now. Retry-After is in whole seconds and only sent on the 429, so 10 means wait 10 seconds. That's the header to document as the retry delay.
With RatelimitHandlerPlugin removed, I still got the 429 and the JSON body, but the ratelimit-* and Retry-After headers were gone. The plugin is registered per handler, so if you run an RPC handler next to the OpenAPI one, add it to both.
Which algorithm you get
In 1.16.6, both the memory and the Redis adapter keep a sliding window log: one timestamp per allowed request, and old ones fall off as they age out. Redis keeps the timestamps in a sorted set and updates it with Lua. That makes it exact, at the cost of one stored entry per request. A limit of 30 requests per 10 seconds means up to 30 entries per customer. At 10,000 requests a minute, budget for up to 10,000 entries per customer.
I checked the behaviour with a limit of 3 per second:
0 ms a allowed
406 ms b allowed
407 ms c allowed
407 ms d rejected
1060 ms f allowed (a is older than a second now)
1060 ms g rejected
1463 ms h allowed (b and c expired)
1463 ms i allowed
1464 ms j rejected
Rejected requests don't use up the allowance, so a client that keeps hammering doesn't push its own reset further out. Each allowed request frees its slot when its own timestamp ages out, so capacity comes back spread out rather than all at once at a window edge.
The memory adapter counts inside one process. With three instances behind a load balancer, a customer whose requests reach all three can get up to three times the limit. To enforce one limit across instances, use the Redis or Upstash adapter.
Version 2 changes this
The docs on orpc.dev are now for version 2, which is in beta (2.0.0-beta.53 on npm today). There the package is @orpc/ratelimit, and the docs describe MemoryRateLimiter as a fixed window. A fixed window lets a client send up to twice the limit across a window boundary. Version 2 also adds a weight option to limit(), so one expensive call can count as several, and an error option for throwing your own error instead of TOO_MANY_REQUESTS. The header changes too: in 2.0.0-beta.53, ratelimit-reset is the number of seconds until the reset, not a millisecond timestamp. If you start on 1.x today, expect new imports and class names when you upgrade, retest the window boundary, and check how your clients parse ratelimit-reset.
What it doesn't cover
The limit above frees a slot within seconds. A monthly quota that follows the billing cycle renews at the start of the next period, and what happens when a customer runs out is a billing decision: block, charge for the overage, or draw down credits. A 30-day window on the same Ratelimiter interface gives you a rolling cap, which is not the same thing as a billing period. And if both limits throw TOO_MANY_REQUESTS, Retry-After tells the client how long to wait but not which limit it hit. If clients need to know, return a distinct error code. In v2 the error option lets you set one.
Usage counting is separate again. If you bill for successful responses, count those and write the counts in batches, not one database write per call. Give each batch a stable ID and deduplicate writes on it, so a retry after a lost reply doesn't count the same usage twice. None of that belongs in a rate limiter, and the oRPC package doesn't try.
Where I'd start
For a single instance with a low limit, I'd start with the memory adapter in @orpc/experimental-ratelimit, keyed by customer ID. Once requests can land on more than one instance, I'd move to Redis and size it for the highest per-customer limit.
I work on UsageRail.
I used AI to help draft this post.
Top comments (0)