OpenArtifacts

openartifacts.sh / templates / api-postgres

JSON API on Postgres

Node.js 22, one driver (pg 8.13.1) · size small · 4 files, 13.0 kB

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

VariableRequiredWhat it does
API_KEYnoWhen set, POST, PATCH, PUT and DELETE need Authorization: Bearer <API_KEY>. Reads stay public.
DATABASE_URLset by the platformpostgres://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.

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

What it costs

Prices for small, the size this template needs (current prices on openartifacts.sh):

Starting it needs an hour of awake time in credit ($0.03), and an account runs one service per $1 of balance.

Files

Every file, as JSON you can edit and publish: GET /v1/templates/api-postgres.


← all templates · openartifacts.sh · docs.md · skill.md