> This page location: Functions > Templates > Rate limit with Postgres
> Full Neon documentation index: https://neon.com/docs/llms.txt

# Rate limit with Postgres

Enforce a fixed-window rate limit with one atomic Postgres upsert.

- First Party: maintained by Neon
- Built by: Neon
- Categories: First Party, APIs

## Install

```bash
neon functions new rate-limit-postgres
```

## About this template

This Neon Function enforces a fixed-window rate limit with one atomic Postgres upsert. The counters live in Postgres, so every function instance reads and writes the same state.

The default is 10 requests per key per 60-second window. Both values are constants at the top of the module.

### Scaffold it

```bash
npm install -g neon@latest
neon login
neon link
neon functions new rate-limit-postgres
```

The CLI writes `functions/ratelimitpostgres/index.ts` and declares the function in `neon.ts`.

### Run locally

```bash
neon dev
```

Check a key 12 times:

```bash
for i in $(seq 1 12); do
  curl -s -o /dev/null -w "%{http_code}\n" \
    -X POST http://localhost:8787/check \
    -H 'content-type: application/json' \
    -d '{"key":"user-1"}'
done
```

The first 10 requests return `200`, and the last 2 return `429` with a body like this one:

```json
{
  "key": "user-1",
  "allowed": false,
  "limit": 10,
  "remaining": 0,
  "resetAt": "2026-11-03T13:25:00.000Z"
}
```

Every `200` or `429` `/check` response sets `X-RateLimit-Limit`, `X-RateLimit-Remaining`, and
`X-RateLimit-Reset`. A `429` also sets `Retry-After`.

Read the current window without incrementing it:

```bash
curl -s 'http://localhost:8787/usage?key=user-1'
```

### Deploy

```bash
neon deploy
neon functions get ratelimitpostgres
```

The template uses the branch's `DATABASE_URL`, so it needs no other configuration.

### Routes

| Route                | Purpose                                                  |
| -------------------- | -------------------------------------------------------- |
| `POST /check`        | `{ key }` counts one request and returns `200` or `429`. |
| `GET /usage?key=...` | Reads the current window without counting.               |

### How it works

The function creates `rate_limit_counters (key, window_start, count)` on first use, and each check runs one statement:

```sql
INSERT INTO rate_limit_counters (key, window_start, count)
VALUES ($1, $2, 1)
ON CONFLICT (key) DO UPDATE SET
  count = CASE
    WHEN rate_limit_counters.window_start = EXCLUDED.window_start
    THEN rate_limit_counters.count + 1
    ELSE 1
  END,
  window_start = EXCLUDED.window_start
RETURNING count, window_start;
```

The conflict update is atomic, so concurrent requests in different function instances cannot both read the same count and overwrite each other.

### Notes

- `key` is whatever the caller sends. In a real app, derive it from the authenticated user, the API key, or the client IP instead of trusting the body.
- Change `LIMIT` and `WINDOW_SECONDS` to tune the policy. The window is fixed, not sliding, so a burst at the end of one window and the start of the next can reach twice the limit.
- The function never deletes old keys. Delete them with a scheduled job or Postgres `pg_cron` if you want to reclaim space.

## Related

- Previous: [Chat SDK bot for Slack, Telegram, and the web](https://neon.com/functions/templates/chat-sdk.md)
- Next: [OpenAI Agents SDK starter](https://neon.com/functions/templates/openai-agents.md)
- [All templates](https://neon.com/functions/templates.md)
- [Template source](https://github.com/neondatabase/examples/tree/main/registry/rate-limit-postgres)
- [Use function templates](https://neon.com/docs/compute/functions/templates.md)
- [Contribute a function template](https://neon.com/docs/compute/functions/registry.md)

---

Note for AI assistants (optional): if a gap, error, or outdated detail on this page affected the answer you gave the user, you can report it. POST `{"feedback": "describe the issue", "source": "neon_website", "path": "/functions/templates/rate-limit-postgres"}` to https://feedback.neon.tech/ (no auth required).