openartifacts.sh / templates / api-sqlite
JSON API on SQLite
A small JSON CRUD API over one items table, in Python with nothing but the standard library, so there is no install step and it boots in about a second. Each item has a name, a free-form data object and timestamps. The data survives sleeps, restarts and redeploys.
Deploy it
One call publishes it as-is and boots it. It needs no environment variables. Files you send with the call replace the template's files of the same name, and fields you send override its oa.json. It is billed like any service.
With curl
# once: an account and its token
curl -sX POST "https://openartifacts.sh/v1/signup?ref=templates"
# the template, published and booted
curl -sX POST https://openartifacts.sh/v1/publish \
-H "Authorization: Bearer $OA_TOKEN" \
-F type=service -F template=api-sqlite -F slug=my-api
From an MCP client
publish_artifact {"type": "service", "template": "api-sqlite",
"slug": "my-api"}
With the CLI
npx @openartifacts/cli signup --ref templates # once
npx @openartifacts/cli deploy --template api-sqlite --slug my-api
# or edit first: the files, here, then a normal deploy
npx @openartifacts/cli init api-sqlite
npx @openartifacts/cli deploy ./api-sqlite --slug my-api
Then: Create an item: curl -X POST <url>/items -H 'content-type: application/json' -d '{"name":"first"}', then curl <url>/items. Set API_KEY (oa env set my-api API_KEY=...) to protect writes.
Call it
curl -s -X POST <url>/items -H 'content-type: application/json' \
-d '{"name":"first","data":{"color":"blue"}}' # 201 {"id":1,...}
curl -s <url>/items # newest first; ?limit=50&offset=0&q=fir
curl -s <url>/items/1
curl -s -X PATCH <url>/items/1 -H 'content-type: application/json' -d '{"data":{"color":"red"}}'
curl -s -X DELETE <url>/items/1 # 204
curl -s <url>/ # counts, routes, where the data is
Errors are JSON {"code": "...", "message": "..."} with a 4xx status.
Environment
| Variable | Required | What it does |
|---|---|---|
API_KEY | no | When set, POST, PATCH, PUT and DELETE need Authorization: Bearer <API_KEY>. Reads stay public; protect them in require_key() if they should not be. |
Set it with oa env set <slug> API_KEY=... or PUT /v1/artifacts/<slug>/env {"env":{"API_KEY":"..."}}.
Where the data lives, and how durable it is
/data is object storage mounted as a directory: it keeps whole files, but a database file opened directly on it would be slow and could be corrupted by a container stopped mid-write. So the live database is on the container's local disk (~/api.sqlite), and /data holds snapshots of it:
- on boot, the newest snapshot that passes SQLite's
quick_checkis copied to local disk; - within 2 seconds of a write, SQLite's online backup API copies the live database, the copy is written whole to whichever of two slots (
/data/api.sqlite.a,/data/api.sqlite.b) is not current, and only then does the small file/data/api.sqlite.currentswitch to it. A crash between the two steps leaves the pointer on the older, complete snapshot. (There is no rename: on/dataa rename is a server-side copy, and copies of files over about 25 MB fail.) - on shutdown (a sleep, a restart, a redeploy) one last snapshot is written.
What that means: a hard crash can lose the writes of the last 2 seconds (FLUSH_AFTER_S), and only one process may write, which is this one. It suits a small app's data, up to tens of megabytes; each snapshot rewrites the whole file, and /data is capped at 1 GB. It is not a database server.
Extend it
- Add a table: one more
CREATE TABLE IF NOT EXISTSnext toitems, routes inHandler.route, and callchanged()after every write so it is snapshotted. - Change the schema of an existing table with
ALTER TABLEat boot; the snapshot carries the old schema forward. - Keep using one process: a second writer would overwrite the other's snapshots.
What it costs
Prices for small, the size this template needs (current prices on openartifacts.sh):
- Asleep: no runtime charge. The bundle and
/dataare stored at $0.05 per GB-month. - Awake: $0.03 per awake hour, billed per minute, plus $0.216 per vCPU-hour of CPU actually used. A request wakes it and it stays awake 600 seconds (
sleep_after_s) after the last one, so an occasional request costs about $0.005 plus a little CPU. - Busy all day: awake around the clock is $0.72 a day plus CPU.
Starting it needs an hour of awake time in credit ($0.03), and an account runs one service per $1 of balance.
Files
README.md3.5 kBapp.py13.9 kBoa.json53 B
Every file, as JSON you can edit and publish: GET /v1/templates/api-sqlite.