Skip to content

NpgsqlRestYour SQL is the API

Annotate PostgreSQL functions and SQL files with comments to declare HTTP routes, auth, caching, retries, and rate limits. Get a fast, typed REST API โ€” no controllers, no models, no boilerplate. Then test it with SQL files too.

~ npgsqlrest
$โ–Œ
#1 of 14
frameworks benchmarked,
4,500+ req/sยน
0
lines of C# or Python
in a production appยฒ
5ร—
faster iteration on
signature changesยณ
MIT
licensed,
open source
  • Declare, don't code โ€” caching, auth, retries, rate limiting โ€” all declared as SQL annotations.
  • PostgreSQL at the center โ€” the opposite of Clean Architecture: the database drives everything.
  • Types flow outward โ€” PostgreSQL types generate TypeScript clients automatically.
  • No middle tier โ€” no controllers, no models, no mapping layers, no boilerplate.
  • Iterate 5ร— faster โ€” schema is the single source of truth; signature changes propagate to typed clients automatically, and an entire class of type-drift bugs simply cannot happen.
  • Production-grade by default โ€” response caching, rate limiting, retries, PostgreSQL multi-host failover, load balancing, and Excel/HTML response rendering โ€” configured in JSON, not custom middleware.
  • You write SQL, not a URL query language โ€” unlike client-composed query APIs, the API surface is exactly the SQL you wrote: joins, CTEs, window functions โ€” auditable with grep.
  • The whole dev loop in SQL โ€” tests are plain .sql files run against real endpoints in-process (--test), and watch mode (--watch) restarts on SQL, config, and even database routine changes.
  • Built for the AI era โ€” one type system to reason about, machine-verified output (schema check at startup, generated TypeScript checked by tsc), and @mcp tools for AI agents since v3.17.

The Whole Idea, in 19 Slides โ€‹

PostgreSQL in. REST API, typed TypeScript client, and AI-agent tools out โ€” with real, reproducible numbers from a product in production. Use the arrows, thumbnails, or your keyboard (โ†/โ†’, F for fullscreen, N for speaker notes).

The backend that writes itself ยท 20261 / 19
Slide 1: The backend that writes itself โ€” title

A Fully Declarative Backend โ€‹

SQL declares what data. Annotations declare what behavior. Configuration declares what infrastructure. Tests declare what correctness โ€” also in SQL. There is no imperative glue anywhere โ€” no controllers, no services, no mappers to keep in sync. And it is built on the declarative language that has been running the world's data for 50 years โ€” the one every developer, and every LLM, already knows: SQL.

Declarative Annotations โ€‹

Declare what you want from your endpoint โ€” caching, authorization, timeouts, retries, rate limiting โ€” right where the SQL lives.

SQL File

~ sql/users.sql
$โ–Œ

PostgreSQL Function

~ sql/get_users.sql
$โ–Œ

Tests Are SQL Files Too โ€‹

No test framework, no running server, no mocks. npgsqlrest --test invokes the real endpoint pipeline in-process, on the test's own transaction โ€” insert fixtures, call the endpoint (it sees your uncommitted rows), assert with SQL, roll back.

sql
-- tests/get_users.test.sql
begin;

insert into users (email) values ('fixture@example.com');

/*
GET /api/get-users
# @claim user_id=1
*/
select status = 200, 'authenticated caller gets 200' from _response;
select body::jsonb @> '[{"email": "fixture@example.com"}]', 'fixture is listed' from _response;

rollback;
console
$ npgsqlrest ./config.json --test

PASS  tests/get_users.test.sql  (2 assertions, 52ms)
19 passed, 0 failed, 0 error(s)  โ€”  19 assertions in 9 files
endpoint coverage: 2/2 (100%)

Parallel isolated connections, throwaway test databases, per-test clones, tags, JUnit XML, and endpoint coverage with a CI threshold gate. And with --watch, the running server restarts on SQL file, configuration, and database routine changes โ€” create or replace a function in psql and the endpoint is live seconds later, TypeScript client regenerated.

From the Blog โ€‹


Build, Test, Publish and ReleaseLicenseGitHub StarsGitHub ForksCrafted with Claude
โค๏ธ Support this project: Patreon ยท Buy Me a Coffee
Released under the MIT License.
Copyright ยฉ 2024-2026 VB Consulting