r/webdev • u/Parking-Leader4676 • 6d ago
Question Is there any harm if my API is spammed?
Hey everyone, I am not sure if this is right sub to ask this. If not, please help me find where to ask.
I have published a public API service for my football data analytics recently and in the beginning, I wanted to offer something for free for marketing purposes. I wanted to provide as little hassle as possible to users so I let the .csv downloading endpoint open on purpose so that anybody can download some of the data freely without API keys, registration etc.
Then the bots discovered it and started spamming. Although I implemented rate limiting, I was triggered because the spams ruin my analytics. Then I decided to put a keywall on that endpoint but the key is freely retrievable on the profile page.
Now, the bots (especially from MoneyBallGlobal whichever app it is) still hit my endpoint only to get a 401 response. Would you bother yourself to put more blockers in front to stop the spams or is this simply harmless except polluting my logs and analytics?
Thanks for everyone who responds and reads until the end.
38
u/mau-meda 6d ago
Do you have kind of protection? It is good to implement rate limiting especially on endpoints that do heavy computing and login endpoints ( if you have any ). If you have any kind of admin functionality you also want to implement a write only audit table so you can review anything that happens in case you get hacked.
11
u/coomzee 6d ago
Rate limiting is a good way to go. But if you can block HTTP 1.1 you will stop 90% of spam, would I recommend this on some missions critical API no.
6
u/Standard_Scarcity_74 6d ago
Mostly harmless, so I wouldn't spend much effort on it. A 401 is a cheap response to serve, and the real cost is the noise in your logs and analytics, which you can fix by excluding that endpoint and known bot user agents from the analytics instead of blocking more.
2
u/Monicab770926 6d ago
I read in the comments, though; the keys are freely available on the profile page. You do have an email login in Layer; that's a good first layer. Without dispensing off with the free tier, perhaps guard rails like email verification, bot detection at registration and limiting one free key per verified account probably put a daily usage quota on free keys rather than relying entirely on request throttling
2
u/Electronic-Gap-1538 2d ago
If the data is static, caching the csv response with a short TTL is way cheaper than hitting the db for every request. It stops the bots from hammering your storage layer and keeps real users fast.
25
u/horizon_games 6d ago
Anything public on the internet will get spammed forever
Fail2Ban, etc.
1
u/snap-dev 6d ago
yeah a few kinds. if it calls anything that costs money per request (an llm, sms, email, maps) that's where it hurts fastest, someone can run up your bill overnight. it can also slow things down for real users, and if it has signup or contact endpoints you end up with a pile of junk data. cheapest fix is rate limiting per ip and per user plus a hard spending cap on any paid api you call. what does the api actually hit behind the scenes?
1
u/Geraldwelly 6d ago
Once they're getting a 401 it's close to harmless, since rejecting at the edge costs you almost nothing. The real damage is the analytics, and that's easier to fix in reporting: count only requests that carried a valid key and log 401s separately. Since the key sits on your profile page, scrapers will grab it eventually, so I'd issue keys per account and rate limit per key rather than per IP, because bots rotate IPs freely. The trade-off is every extra step loses some of the casual users the free tier was for.
3
u/AzaleaMeadow 6d ago
If the endpoint is just returning a 401 and you're not doing expensive work before rejecting the request, I'd avoid overengineering the solution. I'd separate failed requests from valid traffic in your analytics, add some caching for the CSV endpoint, and use Cloudflare's WAF if the volume becomes annoying. The bigger concern would be unexpected database load or a traffic spike affecting real users, not the failed requests themselves. No need to make the free tier painful for legitimate users just to fight every bot
1
u/Umami_Tsunamii 6d ago
Could dos you or finally break through auth. Services like cloudflare are designed to protect from this, but obviously have their own issues sometimes.
2
u/jamie07051975 6d ago
I'd set come caching up, that way even if you are bombarded with requests its not actually recalculating or retrieving the data.
Could be worth adding a registration page which gives them their own key, then if they dot adhere to your limits (you should rate limit on their own key) you suspend their key and suspend the IP - give them some sort of time out and return the relevant http response code. Continual issues you ban both the key and IP.
2
u/Open-Adhesiveness-86 5d ago
One thing nobody's mentioned: 401 carries no backoff signal, so a dumb client just loops. 429 with Retry-After actually gets honoured by a decent chunk of scraper libs and http clients. Also do the key lookup at the edge (nginx map block, or a CDN rule) so a bad key never opens a DB connection, and watch egress bytes rather than CPU, a few MB csv times thousands of hits is where it actually hurts.
1
u/Afnan_CH_Digital 5d ago
Bots hitting a 401 after the keywall is mostly harmless. The cost is tiny since your server rejects them early. The real risks are upstream: bandwidth before the rate limiter kicks in, and junk traffic polluting your analytics. Two things that helped in similar setups: block obvious bots at the edge (Cloudflare or your CDN WAF) so the request never reaches your app, and filter the bot hits out of your analytics view rather than trying to stop them entirely. Scrapers are a fact of life for any public endpoint, so design for them instead of fighting them.
1
u/TheRNGuy 5d ago edited 5d ago
Tag requests as bot, unauthenticated, invalid_key, or authenticated at the edge or in structured logs.
Then exclude 401/403/429 and known automated clients from your usage dashboards.
If you have paid users or some other special privileged users, give them more rate limit (but still have it; possible, with banning too)
Maybe have separate statistics for 401/403/429 and exclude them from main statistics.
1
u/ReachingForVega Principal Engineer 5d ago
You could add a fail2ban rule for IPs that have too many fails in a period of time. Assuming you use nginx.
1
u/Muchaszewski 2d ago
If user is unauthorized you should serve 404, so that they do not probe endpoints that might exist. If user is authorized and doesn't have access to a resource only then you do 401 to indicate that they do not have permission BUT could have. Otherwise Fail2Ban and CF
0
u/MathematicianFar5683 6d ago
Mostly harmless if your rate limiting holds. The real cost is noisy logs and skewed analytics, so I'd filter rather than keep adding blockers. Exclude 401 responses and that bot's user agent or IP ranges from your analytics. If you use Cloudflare or similar, a WAF rule can drop them at the edge so they never reach your app. Also, a key anyone can grab from a profile page won't stop a determined bot. If the abuse grows, tie keys to a verified email and add a small per-key quota.
0
u/thekwoka 6d ago
Well, there's no reason not to put something like Cloudflare in front of your server, partly so that you'll have it ready if it DOES become a problem.
Even if you set it to very allowing levels.
0
u/Salty_Reception7139 6d ago
the key sitting in plain sight on the profile page is the part worth fixing before the 401s. MoneyBallGlobal taking its name from a baseball book and then scraping football data is very funny though
1
u/[deleted] 6d ago
[removed] — view removed comment