Vercel Logo

Rate limit preorders

The preorder bot doesn't have a name worth blocking. It sends whatever user agent its author felt like typing that morning, and each request looks like a perfectly normal preorder. What gives it away is speed. No person on earth needs 200 lobster rolls in four seconds.

Outcome

A WAF rate limit allows 10 preorders per IP per 10 minutes on POST /api/preorder, the bot keeps getting 429s on its second run, and a rate-limited customer sees a message that tells them to wait.

Counting requests

The bot emptied the truck back in 1.3. Put 150 rolls back on it:

npm run restock

A rate limiting rule is a custom rule whose action counts. Requests that match its conditions are counted per key, like the client's IP, inside a time window. When a key goes over the limit, the rule answers with 429 Too Many Requests before the request reaches your function.

Let's add one from the CLI with the dashboard's defaults (currently 100 requests per 60 seconds):

vercel firewall rules add "Preorder rush" \
  --condition '{"type":"path","op":"eq","value":"/api/preorder"}' \
  --condition '{"type":"method","op":"eq","value":"POST"}' \
  --action rate_limit \
  --rate-limit-window 60 \
  --rate-limit-requests 100 \
  --rate-limit-keys ip \
  --yes
vercel firewall publish --confirm-name claw-and-order

The two conditions keep the rule on the preorder endpoint alone, so page loads and lunch orders don't count toward it.

Rate limiting has its own pricing

Rate limiting is priced by usage, and what's included depends on your plan. Check WAF usage and pricing for yours. The few hundred requests in this course are a rounding error.

Send the bot:

npm run scalper -- preorder --target "$URL"
Results
  201            100
  429            100

The rule worked, and it let the bot take 100 rolls. That default was picked with APIs in mind. A real customer preorders one roll, maybe two, plus a retry if their phone drops signal. Ten from one IP covers a person with a flaky phone, and the bot burns through that in a second. Restock and tighten it, then wait out the first run's 60-second window so the bot starts from zero:

npm run restock
vercel firewall rules update "Preorder rush" --rate-limit-requests 10 --yes
vercel firewall publish --confirm-name claw-and-order
sleep 60
npm run scalper -- preorder --target "$URL"
Results
  201            10
  429            190

Ten rolls for the bot. rules update changes only the flags you pass, so the window, key, and conditions stay as they were.

If the bot gets 11 or 12, that's expected: counters are tracked per region, so a key spread across regions can go a little over.

Say what happened

Right after the bot finishes, within the minute, try a preorder from your browser. You'll get "Something went wrong." This is expected. Your browser shares an IP with the bot, so you're over the limit too. The WAF answered with a 429, and the form treats every failure the same way. Find the TODO in components/preorder-form.tsx:

components/preorder-form.tsx
    // TODO (3.1): A 429 means the rate limit caught this request. Tell the
    // customer to try again in a few minutes instead of "Something went wrong."
    // TODO (3.2, Advanced): Our own 429 for a reused phone number carries
    // { reason: "phone" }. Show its message.
 
    setStatus({ state: "error", message: "Something went wrong. Try again." });

Replace the 3.1 TODO with branches for the 429, and the 409 for a sold-out truck while we're here. Keep the 3.2 TODO; Hobby projects skip that lesson:

components/preorder-form.tsx
    // TODO (3.2, Advanced): Our own 429 for a reused phone number carries
    // { reason: "phone" }. Show its message.
 
    if (response.status === 429) {
      setStatus({
        state: "error",
        message: "The window's slammed right now. Give it a few minutes and try again.",
      });
      return;
    }
 
    if (response.status === 409) {
      setRollsLeft(0);
      setStatus({ state: "error", message: "Sold out. See you tomorrow." });
      return;
    }
 
    setStatus({ state: "error", message: "Something went wrong. Try again." });

That message keeps a hungry customer in line instead of sending them across the street to Pinchy's. Deploy with vercel deploy --prod, then refill the truck with npm run restock.

The patient bot

Preorder rush resets every 60 seconds. Run the bot, wait a minute, and run it again:

npm run scalper -- preorder --target "$URL"
sleep 65
npm run scalper -- preorder --target "$URL"

Twenty rolls. A bot that runs once a minute sells out the truck by 11:15, so all the limit did was make it wait. Make each IP's 10 last longer. A real customer only needs a few tries all morning:

npm run restock
vercel firewall rules update "Preorder rush" --rate-limit-window 600 --yes
vercel firewall publish --confirm-name claw-and-order

That's 10 per IP per 10 minutes, the longest window Hobby allows today; check the limits for your plan. A patient bot now needs over two hours to drain the truck.

The rule counts per key, and the key decides who counts as "the same visitor." Every plan can also key by ja4, but every customer on the same browser shares a fingerprint, so they'd all share one bucket of 10. For preorders, IP is the closest Hobby gets to one person. IP has its own blind spot, since people share them, and Section 5 has a lesson about exactly that.

Enterprise: header keys and token bucket

Enterprise rate limits can key by user agent or any request header, such as an API key or a customer ID. They can also count with a token bucket, which refills gradually instead of resetting all at once. With a fixed window, the patient bot got all 10 back the moment the window reset. A token bucket hands them back a few at a time.

Hands-on exercise 3.1

Rate limit the preorder endpoint, then make the limit kind to customers and hard on patient bots.

Requirements:

  1. Restock, add Preorder rush for POST /api/preorder keyed by IP at 100 requests per 60 seconds, and publish. Run the bot.
  2. Restock, lower the limit to 10, publish, and run the bot again.
  3. Handle 429 and 409 in components/preorder-form.tsx and deploy.
  4. Run the bot twice, 65 seconds apart, and count the rolls.
  5. Restock, change the window to 600 seconds, and publish.
  6. Commit the code and a rules snapshot.

Implementation hints:

  • Hobby's rate limiting allowance is tight (limits), so edit Preorder rush instead of adding a second rule.
  • The WAF's 429 comes with its own body, so branch on response.status.
  • vercel firewall traffic list --since 10m --path /api/preorder --dimension rule shows the rule's matches.

Try It

After the restock with the 600-second window, run the bot twice:

npm run scalper -- preorder --target "$URL"
sleep 65
npm run scalper -- preorder --target "$URL"
Results
  201            10
  429            190

...

Results
  429            200

Ten rolls, total. The traffic report shows the rule doing the work:

vercel firewall traffic list --since 15m --path /api/preorder --dimension rule

Now preorder from your browser. You share the bot's IP, so you're in its bucket, and the form says "The window's slammed right now. Give it a few minutes and try again."

A real customer on their own phone would get a roll. You get a polite message about the window, which is the best a rate limit can do for somebody who shares an IP with a bot.

One more check, for the sold-out branch. The deployment's rate limit won't let one IP empty the truck, so we'll sell out the copy on your laptop instead, where no Firewall sits in front. Start it with npm run dev and open http://localhost:3000/preorder first, because the form disables its button when it loads with zero rolls. Then send the bot in a second terminal:

npm run scalper -- preorder --target http://localhost:3000

Back in the open form, place a preorder. It says "Sold out. See you tomorrow." and the button switches to Sold out.

Commit

vercel firewall rules list --json > firewall/rules.json
git add components/preorder-form.tsx firewall/rules.json
git commit -m "feat(preorder): rate limit preorders to 10 per IP per 10 min" -m "The form explains a 429 instead of saying something went wrong."

Done-When

  • Preorder rush is published for POST /api/preorder, keyed by IP, 10 requests per 600 seconds
  • Two bot runs a minute apart take about 10 rolls total
  • A rate-limited preorder shows the "give it a few minutes" message
  • On localhost, a preorder after the bot empties the truck shows "Sold out. See you tomorrow." and disables the button
  • The code and firewall/rules.json are committed

Troubleshooting

The bot still gets 201 for every request. The rule isn't live, or its conditions don't match. Run vercel firewall diff for unpublished changes, then vercel firewall rules inspect "Preorder rush" and check the path is exactly /api/preorder and the method is POST.

rules add refuses with a limit error. Your project already has a rate limiting rule, or it's at its custom rule limit. On Hobby, the main path expects only Pinchy's scraper and Preorder rush at this point.

The form still says "Something went wrong" on a 429. The new code isn't deployed. vercel deploy --prod deploys your local files, so save the file first.

The second bot run still gets 10 rolls. The window change wasn't published, or the second run started more than 600 seconds after the first. Check the window with vercel firewall rules inspect "Preorder rush" and vercel firewall diff, then run the pair again. Restocking doesn't help or hurt here, since it resets rolls, not the Firewall's count.

Solution

The finished rule, from scratch:

vercel firewall rules add "Preorder rush" \
  --condition '{"type":"path","op":"eq","value":"/api/preorder"}' \
  --condition '{"type":"method","op":"eq","value":"POST"}' \
  --action rate_limit \
  --rate-limit-window 600 \
  --rate-limit-requests 10 \
  --rate-limit-keys ip \
  --yes
vercel firewall publish --confirm-name claw-and-order

The form's error branches, after the response.ok check:

components/preorder-form.tsx
    if (response.status === 429) {
      setStatus({
        state: "error",
        message: "The window's slammed right now. Give it a few minutes and try again.",
      });
      return;
    }
 
    if (response.status === 409) {
      setRollsLeft(0);
      setStatus({ state: "error", message: "Sold out. See you tomorrow." });
      return;
    }
 
    setStatus({ state: "error", message: "Something went wrong. Try again." });

The bot can still preorder. It waits in line like everybody else now, and the line is ten minutes long.

Was this helpful?

supported.