Zebflow Docs
REST API + PostgreSQL
Zebflow Docs

REST API + PostgreSQL

JSON REST API endpoints backed by PostgreSQL. Covers list, detail (path param), create, update, delete. Uses --param (repeated key=value, the key the bind position) for safe parameterized queries — no string interpolation, no SQL injection risk.

What this builds

JSON REST API endpoints backed by PostgreSQL. Covers list, detail (path param), create, update, delete. Uses --param (repeated key=value, the key the bind position) for safe parameterized queries — no string interpolation, no SQL injection risk.


Key Concept: Parameter Binding

Path params (:id), query strings (?status=x), and body fields all land in the webhook input payload. Bind them safely to SQL $1, $2, ... via:

  • --param "1={{ input.webhook.params.id }}" — one --param per placeholder, the key is its position → $1
  • --param "1={{ $trigger.body.name }}" --param "2={{ $trigger.body.email }}" — repeat the flag, one key each → $1, $2, ...

Webhook input shape: the trigger answers under one key, webhook, holding the whole request envelope; nothing is merged to the root — a JSON body field name is input.webhook.body.name right after the trigger and $trigger.body.name anywhere in the run.

LocationAccessExample
Path param :idinput.webhook.params.id--route /api/users/:id
Query string ?status=xinput.webhook.query.status?status=active
GET requestinput.webhook.body is nullpath params and query are still at input.webhook.params / input.webhook.query
POST JSON body fieldinput.webhook.body.nameapplication/json — the parsed object sits at input.webhook.body
POST form fieldinput.webhook.body.nameapplication/x-www-form-urlencoded, percent-decoded
POST multipart text fieldinput.webhook.body.namemultipart/form-data text fields
POST multipart fileinput.webhook.files.avatara FileRef object: { __zf_type: "file_ref", ref, filename, mime, kind, size, sha256, ... }
Raw / non-object bodyinput.webhook.bodynon-object JSON or plain text fallback

Pipelines

GET /api/items — list, optional query filter

| trigger.webhook --route /api/programmes --method GET| postgres.query.run --credential my-pg --param "1={{ input.webhook.query.faculty_id ?? null }}" \    -- "SELECT unit_id::text, code, title->>'id' as title, slug FROM academic.academic_unit WHERE unit_type = 'programme' AND is_active = true AND ($1::uuid IS NULL OR parent_unit_id = $1::uuid) ORDER BY code"| javascript.script.run -- "return { ok: true, data: input.query.rows }"| web.response.send --body "{{ input.script }}"

GET /api/items/:id — detail by path param

:unit_id in trigger path → input.webhook.params.unit_id → $1:

| trigger.webhook --route /api/programmes/:unit_id --method GET| postgres.query.run --credential my-pg --param "1={{ input.webhook.params.unit_id }}" \    -- "SELECT au.unit_id::text, au.code, au.title, COUNT(DISTINCT s.student_id) as total_students FROM academic.academic_unit au LEFT JOIN academic.student s ON s.unit_id = au.unit_id AND s.is_active = true WHERE au.unit_id = $1::uuid AND au.unit_type = 'programme' GROUP BY au.unit_id, au.code, au.title"| javascript.script.run -- "return { ok: true, data: input.query.rows[0] };"| web.response.send --body "{{ input.script }}"(see **Answering with a status** below for the 404 branch)
| trigger.webhook --route /api/programmes/:unit_id --method GET| postgres.query.run --credential my-pg --param "1={{ input.webhook.params.unit_id }}" \    -- "SELECT unit_id::text, code, title FROM academic.academic_unit WHERE unit_id = $1::uuid"| javascript.script.run -- "const prog = input.query.rows[0]; return prog;"| postgres.query.run --credential my-pg --param "1={{ input.script.unit_id }}" \    -- "SELECT p.fullname, l.academic_rank, st.position FROM academic.lecturer l JOIN academic.staff st ON st.staff_id = l.staff_id JOIN app.player p ON p.player_id = st.player_id WHERE st.unit_id = $1::uuid AND l.is_active = true ORDER BY p.fullname"| javascript.script.run -- "return { ok: true, data: { ...ctx.nodes['n2'].script, lecturers: input.query.rows } }"| web.response.send --body "{{ input.script }}"

Note: every node adds its answer under its own key and keeps the rest of the payload (help("pipeline/dsl") § One answer key). The second postgres.query.run answers under query again, so it replaces the first query's rows and nothing else: input.script from the first script is still there. To reach a node's answer by name when a later node used the same key, use ctx.nodes['<node id>'] in a script (pipe-mode node ids are n0, n1, … in source order — the first script above is n2) or $nodes.<node id> inside a {{ }} expression. ctx.nodes['n2'] is the whole payload n2 left behind; what it returned is ctx.nodes['n2'].script.

POST /api/items — create from body

JSON body fields sit under input.webhook.body right after the trigger ($trigger.body anywhere later). Access as input.webhook.body.title, input.webhook.body.email, etc.:

| trigger.webhook --route /api/posts --method POST| logic.if --when "input.webhook.body.title && input.webhook.body.body"(false pin → `web.response.send --status 400`; see **Answering with a status**)| postgres.query.run --credential my-pg --write --param "1={{ $trigger.body.title }}" --param "2={{ $trigger.body.body }}" --param "3={{ $trigger.body.author_id }}" \    -- "INSERT INTO posts (title, body, author_id, created_at) VALUES ($1, $2, $3, now()) RETURNING id, title"| javascript.script.run -- "return { ok: true, data: input.query.rows?.[0] }"| web.response.send --body "{{ input.script }}"

PUT /api/items/:id — update by path param + body

Combine path param and body fields with --param:

| trigger.webhook --route /api/posts/:id --method PUT| postgres.query.run --credential my-pg --write --param "1={{ input.webhook.body.title }}" --param "2={{ input.webhook.body.body }}" --param "3={{ input.webhook.params.id }}" \    -- "UPDATE posts SET title = $1, body = $2, updated_at = now() WHERE id = $3 RETURNING id, title"| javascript.script.run -- "return { ok: true, data: input.query.rows[0] };"| web.response.send --body "{{ input.script }}"(see **Answering with a status** below for the 404 branch)

DELETE /api/items/:id — delete by path param

| trigger.webhook --route /api/posts/:id --method DELETE| postgres.query.run --credential my-pg --write --param "1={{ input.webhook.params.id }}" \    -- "DELETE FROM posts WHERE id = $1 RETURNING id"| javascript.script.run -- "return { ok: true, deleted: input.query.rows?.[0]?.id ?? null }"| web.response.send --body "{{ input.script }}"

The same API on Sekejap

The project's own database (sekejap.query.run, no credential) takes the same --param binding. Three differences change these statements:

  • A row's key is _key. CREATE TABLE posts (_key TEXT PRIMARY KEY DEFAULT ulid(), title TEXT, body TEXT, author_id TEXT, created_at TIMESTAMPTZ DEFAULT now(), updated_at TIMESTAMPTZ) mints one per INSERT.
  • RETURNING takes column names and *, no AS alias and no expression: INSERT … RETURNING _key, title, and rename in the next node. UPDATE … WHERE _key = $1 RETURNING _key, title answers the changed row (none when the key does not exist, which is the 404 branch).
  • DELETE … RETURNING is refused. Delete by key and read rows_affected:
| trigger.webhook --route /api/posts/:id --method DELETE| sekejap.query.run --write --param "1={{ input.webhook.params.id }}" -- "DELETE FROM posts WHERE _key = $1"| web.response.send --body "{{ { ok: true, deleted: input.query.rows_affected > 0 ? $trigger.params.id : null } }}"

help("db/sekejap") has the whole dialect.


--param: one placeholder per flag

One bind valueSeveral bind values
Best forA single $1 from a known pathMultiple placeholders, conditional values
Syntax--param "1={{ input.<dot.path> }}"--param "1={{ a }}" --param "2={{ b }}" (repeat the flag)
TypingA whole {{ expr }} keeps its JSON typeSame, per key
Example--param "1={{ input.webhook.params.unit_id }}"--param "1={{ input.webhook.body.title }}" --param "2={{ input.webhook.params.id }}"

Answering with a status

A script cannot set the response. It returns a value; the graph decides what happens next. To answer 404, branch and let web.response.send answer:

[find]  postgres.query.run --credential my-pg --param "1={{ input.webhook.params.id }}" -- "SELECT …"[found] logic.if --when "input.query.rows && input.query.rows.length > 0"[ok]    web.response.send --body "{{ { ok: true, data: input.query.rows[0] } }}"[gone]  web.response.send --status 404 --body "{{ { ok: false, error: 'not found' } }}"[find] -> [found][found]:true -> [ok][found]:false -> [gone]

web.response.send owns the response: --status, --header (Location, Set-Cookie), --body, --template. The branch is visible in the editor, which a key hidden in a payload would not be.


Nodes Used

  • trigger.webhook — HTTP endpoints; path params in input.webhook.params.<name>, user-submitted data (JSON, form-urlencoded, multipart text fields) under input.webhook.body.<name>
  • postgres.query.run --credential <id> — parameterized SQL; --param "1={{ input.webhook.path }}" (one placeholder) or repeat --param for several, plus --write for INSERT/UPDATE/DELETE
  • javascript.script.run (TypeScript: typescript.script.run) — validation, 404 guard, response shaping, chaining multiple queries; its answer sits under script, the rest of the payload is kept, so a terminal script needs an explicit web.response.send --body "{{ input.script }}" to answer cleanly

A script cannot set the response. It returns a value; the graph decides what happens next. Branch with logic.if and let web.response.send answer — --status, --header (Location, Set-Cookie), --body. See help("pipeline/examples/webhook-restapi-postgres") § Answering with a status.