r/AskProgramming • u/ltsheeyy • Jul 09 '26
Other Best way to prevent API abuse?
I'm building a web app that needs to call a paid API.
I want visitors to be able to test it a few times for free (around 3 requests) but i dont want to let people abuse it and drain my API balance.
My first aproach was IP rate limiting, is there a better approach?
- I'm using this project to learn, so I might be doing this the wrong way.
3
Upvotes
15
u/johnpeters42 Jul 09 '26
Someone could use a VPN or bot farm or something to get around the limit, albeit it would be clunky on their end. You may want to just impose an across-the-board daily cap or something on all non-paid users (if the cap is hit, then all such users are blocked until the next day).
12
u/KingofGamesYami Jul 09 '26
Issue limited API keys, require a captcha to get API key. Now it's annoying enough that you (probably) won't get abused too much.
1
u/JacobStyle Jul 09 '26
I can't think of a way to do it that I couldn't also break, other than a global rate limit, maybe with some sort of "make a free account to hop into the queue for the next available use" type thing? That's clunky as hell though.
1
u/ericbythebay Jul 09 '26
IP rate limiting is gonna make your API look like crap, if anybody is using CGNAT, Cloudflare, GCP, AWS, etc.
If you want to limit access, make them register and use an authenticated API.
1
u/No-Consequence-1779 Jul 09 '26
I’ve done the same. Coocoookie will work for most. Then yes, by ip. If you see a repeat ip and no cookie then they are switching browsers or using a vpn .. most people do not use vpns yet … for this, they need to register and it returns to cooooookie. Using more than X times should require registration. Like an easy one click google account.
You want them to register to get thier email to market in the future. Maybe send the api product via email …
1
u/mxldevs Jul 09 '26
Enforce user registration. People can create multiple accounts, so if it's an issue, add additional requirements like sms verification which makes it harder on average.
1
u/MissinqLink Jul 09 '26
Cloudflare has free ways to do rate limiting. It’s the only way my we content doesn’t get completely overrun with bots.
3
u/PhilosophyRude9324 Jul 09 '26
You can rate limit based on an appkey - the appkey X is allowed Y calls.
As soon as it became a client you issue an appkey without any limitation!
3
u/Beardy4906 Jul 10 '26
Rate limit by account and make it require an account.
2
u/KindaLegallyBlindDev Jul 09 '26 edited Jul 09 '26
Well, you might use something like Nginx's rate limiting if that was the entry point to your API, but I think Captchas could help too. Be aware that people using stuff like the tor browser could easily dodge the IP rate limiting set by you and if 4+ free requests starts to become too costly for you, you might need more than one solution? Just a few thoughts.EDIT: Ignore this, answers like those of u/johnpeters42 or u/PilosophyRude9324 are much better
2
u/abd53 Jul 10 '26
IP alone won't be enough. Use a combination of IP, cookies and if you can, browser fingerprint.
Also, as others suggested, put a hard limit. And this is just redundant but we live in the "AI" era, so, DO NOT put your API key on frontend.
1
u/huuaaang Jul 09 '26
IP rate limit is definitely not the way to go. You need to authenticate users of your app and API. Track usage that way. Anything less is easy to abuse.
1
u/Riajnor Jul 09 '26
Not sure of your tech stack but i’m pretty sure you could handle this through azure’s apim
1
u/idontlikegudeg Jul 10 '26
Require users to make a login with a verified email for testing. Keep track of the number of used and if it exceeds the limit you set, show a dialog that says the fee contingent expired.
1
u/PaulPhxAz Jul 10 '26
IP Rate Limiting is good.
If they sign-up, then unique email/user limiting.
Overall Limit for "Free Tier" ( the aggregation of all free-tier per day usage ).
1
u/cobaltbluedw Jul 11 '26
If the ai result isn't generically valuable enough for people to spend thier time abusing, and your site is obscure enough that few would even know or care about the feature, then you don't have to be too concerned about abuse.
Risk mitigation is often less about prevention and more about opportunity costs. Build budgeting and rate limiting into your servers use of the API so your server doesn't make more requests than you want it to or exceed your total alloted budget and then just see how it goes.
With that set up, the worse case scenario is that someone does try to abuse the system or you get unexpected traffic and peg your budget. If that ever happens, your still not over shooting your budget, and you'll have less speculative data about how to proceed.
If it ever did happen, BTW, don't underestimate the value of making the service less palettable. If you intentionally slow down each request by one second, your users wouldn't know the difference, but people trying to game the system would lose all interest. Adding a watermark to generations would also make it less enticing fir repurposers.
0
u/Any-Information-864 Jul 12 '26
IP limiting is a reasonable start, but it’s leaky both ways: shared IPs (offices, mobile carriers behind CGNAT) block innocent users, and anyone determined just rotates IPs with a VPN. So it over-blocks real people and under-stops abusers.
Better setup, in order of what actually matters:
1. Global spend cap on your backend — this is the real safety net. Track total free requests per day against a hard limit, and set a spend cap in the API provider’s dashboard too. Do this first: even if abuse slips through, your loss is bounded. Everything else is just raising the bar.
2. For the per-visitor 3 free tries — a lightweight sign-in (Google/GitHub OAuth) tied to an account beats IP by a mile; way harder to farm than swapping IPs. If you want zero-signup trials, use a cookie/localStorage token + IP as a soft limit — it stops casual double-dipping, not determined people.
3. Add a bot check (Cloudflare Turnstile or hCaptcha) on the free endpoint — cheap, and it kills the automated scraping that drains you fastest.
Don’t over-engineer the anti-abuse — someone determined will always squeeze out a few extra. Your goal isn’t to make it impossible, just to cap the damage and stop the easy abuse. The global spend limit is what lets you sleep.
1
u/[deleted] Jul 10 '26
[removed] — view removed comment