openartifacts.sh / templates / api-postgres
JSON API on Postgres
A small JSON CRUD API over one items table, on the service's own Postgres 18: "database": "postgres" in oa.json makes the platform run Postgres in the same container and set DATABASE_URL (and PGHOST, PGPORT, PGUSER, PGPASSWORD, PGDATABASE) before start runs. Node's http module plus one pinned driver (pg). Each item has a name, a free-form data object (jsonb) and timestamps.
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-postgres -F slug=my-pg-api
From an MCP client
publish_artifact {"type": "service", "template": "api-postgres",
"slug": "my-pg-api"}
With the CLI
npx @openartifacts/cli signup --ref templates # once
npx @openartifacts/cli deploy --template api-postgres --slug my-pg-api
# or edit first: the files, here, then a normal deploy
npx @openartifacts/cli init api-postgres
npx @openartifacts/cli deploy ./api-postgres --slug my-pg-api
Then: Create an item: curl -X POST <url>/items -H 'content-type: application/json' -d '{"name":"first"}', then curl <url>/items. Look inside with oa db sql my-pg-api "select count(*) from items".
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, database size, routes
Errors are JSON {"code": "...", "message": "..."} with a 4xx/5xx status.
Look inside
oa db status <slug> # state, size, archive health, last restore
oa db sql <slug> "select count(*) from items" # psql as the app's role
oa db backup <slug> # a base backup now
oa db dump <slug> > items.dump # pg_dump -Fc; pg_restore it anywhere
There is no connection from outside: Postgres listens on 127.0.0.1 inside the container only, so psql from a laptop cannot reach it. Use oa db sql, or a dump.
Environment
| Variable | Required | What it does |
|---|---|---|
API_KEY | no | When set, POST, PATCH, PUT and DELETE need Authorization: Bearer <API_KEY>. Reads stay public. |
DATABASE_URL | set by the platform | postgres://app:...@127.0.0.1:5432/app. Never stored in a version and not listed by oa env. |
Set API_KEY with oa env set <slug> API_KEY=....
How durable it is (honestly)
The database lives on the container's local disk, which is gone after every sleep. Durability is an archive in object storage: a base backup plus every piece of WAL (Postgres's write-ahead log), shipped continuously.
- A planned stop loses nothing: a sleep, a restart, a redeploy and a platform rollout all shut Postgres down cleanly and upload the last WAL before the container goes.
- A hard crash can lose the last ~5 seconds of commits: when a container dies without a stop (out of memory, a host failure), the writes since the last 5-second upload are gone. The database that comes back is consistent, never half-written.
- Every wake is a restore, and it grows with the database: a few seconds for a small one, longer for hundreds of MB. So a database service sleeps after an hour without requests (
sleep_after_s3600) instead of 10 minutes. The first request after a sleep waits for the restore. - Limits: up to 256 MB on
small(1 GB onmedium/large). Over the limit the app's writes are refused (this template answers507 database_full), reads keep working, andoa db sqlcan still delete. One database per service, no replicas, no external connections. - Removing
"database"fromoa.jsonkeeps the archive; deleting the service deletes it.
For data you cannot afford to lose a few seconds of, or anything bigger, use a hosted Postgres (Neon, Supabase) and set DATABASE_URL yourself with oa env set.
Extend it
- Add tables in
migrate()(CREATE TABLE IF NOT EXISTS ...); it runs at every start, after the database is ready. Migrations belong instart, notinstall: the database is not running yet whileinstallruns. - Any Postgres driver or ORM works with
DATABASE_URL(Prisma, Drizzle, SQLAlchemy, Django, Rails).
What it costs
Prices for small, the size this template needs (current prices on openartifacts.sh):
- Asleep: no runtime charge. The bundle and the database archive are 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 (Postgres's CPU counts too). A request wakes it and it stays awake 3600 seconds (
sleep_after_s) after the last one, so an occasional request costs about $0.03 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.md4.4 kBoa.json121 Bpackage.json137 Bserver.mjs8.4 kB
Every file, as JSON you can edit and publish: GET /v1/templates/api-postgres.