Adding Rate Limits to a Netlify Chat Endpoint with Postgres for Palm Beach Contractors
Описание и применение
Автор создал серверный лимит запросов по IP, используя таблицу Postgres, чтобы остановить спам‑боты, атакующие чат‑эндпоинт Netlify, и сохранил чистоту базы данных для местных подрядчиков.
Задача
Automated scrapers were flooding a Netlify chat endpoint with spam, exhausting Postgres connections and corrupting lead data.
Результат
The IP‑hashing and hourly counter in Postgres immediately stopped bot traffic, preventing database exhaustion and keeping lead pipelines clean for local contractors.
Рабочий процесс
- 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
Доказательства
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
- Дата записи: 2026-11-03T14:14:14.287Z