Adding Rate Limits to a Netlify Chat Endpoint with Postgres for Palm Beach Contractors
Description and use
The author implemented an IP‑based rate limit in Postgres to stop bot spam on a Netlify chat endpoint, preserving database integrity for local contractors.
Problem
Automated scrapers were flooding a Netlify chat endpoint with spam, exhausting Postgres connections and corrupting lead data.
Outcome
The IP‑hashing and hourly counter in Postgres immediately stopped bot traffic, preventing database exhaustion and keeping lead pipelines clean for local contractors.
Workflow
- Deploy Netlify Function with Node.js
- Connect to Postgres via DATABASE_URL with SSL
- On each POST request: 1) Retrieve client IP from headers, 2) Hash IP with SHA‑256, 3) Delete rate_limit rows older than 1 hour, 4) Count requests for the hashed IP in the last hour, 5) If count ≥5, return 429 and close DB, 6) Else insert new rate_limit record, 7) Proceed with chat logic, 8) Close DB and return 200
Evidence
bots were hammering the endpoint fifty times a minute, trying to inject SQL strings and garbage URLs into my database
I needed a hard server‑side rate limit tied to the visitor's IP address, stored directly in my Postgres database without adding heavy third‑party SaaS dependencies
Implementing this simple IP hashing and Postgres counter immediately stopped the bot traffic on my site
- Record date: 2026-11-03T14:14:14.287Z