Stopping Chat Spam on Netlify with a Simple Rate‑Limit
Description and use
A simple method to stop chat spam on Netlify: store a counter in Postgres, gate requests with an Edge Function, and return a friendly JSON error when the limit is exceeded.
Problem
Public chat endpoints can be flooded with bot messages, degrading user experience.
Outcome
Spam traffic is reduced, backend load is protected, and users receive clear feedback, all without additional SaaS costs.
Workflow
- Persist a lightweight counter in Postgres: store a row per IP or user token with timestamp and hit count, resetting if older than a day.
- Gate the request with a Netlify Edge Function: read the counter before the request reaches the main API, short‑circuit with 429 if threshold exceeded (e.g., 5 messages per minute).
- Return a friendly JSON payload: {"error": "Rate limit exceeded – please wait a moment before sending another message."} to inform users and bots.
Evidence
Persist a lightweight counter in Postgres. I store a row per IP address (or per user token) with a timestamp and a hit count.
Gate the request with a Netlify Edge Function. The Edge Function reads the counter before the request reaches my main API. If the count exceeds the threshold (e.g., 5 messages per minute), the function short‑circuits and returns a 429 response.
Give a friendly “slow down” message. Instead of a generic error, I return a short JSON payload: { "error": "Rate limit exceeded – please wait a moment before sending another message." }
- Record date: 2026-10-16T13:56:17.404Z