Articles ·
SQLite over HTTP, without holding a connection
SQLite was built to be opened by one program. Here is how Served puts it behind an HTTP API without inventing a protocol: one connection per request, and transactions as arrays.
SQLite is a library. You link it into a program, the program opens a file, and that is the database. Putting it behind a network API means deciding what to do about the one thing SQLite assumes and HTTP does not have: a connection that stays open.
Most answers to this invent a protocol. There are sessions, or streams, or a special client that keeps a socket alive so that a transaction can span several messages. We went the other way and kept nothing between requests at all.
One connection per request
When a request arrives, Served opens the database file, runs your SQL, closes the file and replies. The next request does the same from scratch.
That sounds expensive and is not. Opening a SQLite database is opening a file. On the machine we develop on, with the file on an NVMe disk, a whole read request takes under a tenth of a millisecond in the storage layer. A write takes about two milliseconds, nearly all of it waiting for the disk to confirm the commit, which it would have to do anyway.
What it buys is that a whole category of problem cannot occur:
- A setting from one request cannot leak into another.
PRAGMA foreign_keys = ONlasts for the request it is in. - A transaction left open by a crashed client does not hold a lock. Closing the connection rolls it back.
- There is no pool to size, no idle handle to expire, no limit on how many databases can exist. A database nobody is using is a closed file.
- With no connection open, a database in WAL mode folds its log back into the main file. At rest, each database is exactly one file, which is what makes "download your database" a simple thing to offer.
What stays and what goes
SQLite keeps some settings in the file and some on the connection. With a connection per request, the difference matters, so it is worth knowing which is which.
| Stays, because it is in the file | Gone at the end of the request |
|---|---|
journal_mode, user_version, application_id, auto_vacuum, page_size, and of course your tables | foreign_keys, synchronous, cache_size, temporary tables, attached in-memory databases, any open transaction |
If you want foreign keys enforced, say so in the same request:
PRAGMA foreign_keys = ON; DELETE FROM lists WHERE id = 3;
Transactions as arrays
Without a connection that persists, a transaction cannot be "begin now, commit later". So a transaction is a single request: send a JSON array of statements and they run as one unit.
[
{"sql": "UPDATE accounts SET balance = balance - ? WHERE id = ?", "args": [50, 1]},
{"sql": "UPDATE accounts SET balance = balance + ? WHERE id = ?", "args": [50, 2]}
]
If the second statement fails, the first is undone, and the error tells you which statement it was. You get one result back for each statement, so a transaction can read as well as write.
The one thing this cannot do is let your program look at a result and then decide what to run next inside the same transaction. In practice that is rarer than it sounds, because SQL can carry the decision: UPDATE ... WHERE balance >= ?, INSERT ... ON CONFLICT, RETURNING. Where it truly is needed, a hosted database reached over HTTP is the wrong tool, and SQLite linked into your program is the right one.
Bare SQL is a valid request
No SQL statement begins with { or [. So if the body of a request starts with anything else, Served treats it as SQL, whatever the content type says.
curl https://api.served.dev/v1/databases/todo/query \ -H "Authorization: Bearer $SERVED_KEY" \ -d "SELECT count(*) FROM items"
A string can hold several statements separated by semicolons, the way a script given to the sqlite3 command does. You get back the rows of the last statement that produced any.
What the values look like
SQLite has five kinds of value and JSON has room for four of them directly. Integers and reals are numbers, text is a string, NULL is null, and a blob is {"base64": "..."}. Parameters go in the same way.
Nothing is interpreted on the way. A column declared DATETIME holding the text 2026-01-02 03:04:05 comes back as that text. Many database drivers try to help here by guessing types from column names. We wrote our own thin layer over SQLite's C interface so that nothing would.
What you give up
A network hop on every query. If your code runs a hundred small queries to draw a page, a database in the same process will always beat one across the internet, and you should batch them into one request or keep SQLite local.
What you get in return is a database that any program, in any language, on any machine, can use with the HTTP client it already has, and a file you can take home.
Served is hosted SQLite. Real SQLite behind an HTTP API, $3 a month, and the file is yours to download whenever you like.