Named queries
A named query is SQL kept on the gateway: a statement, the parameters it takes, the database it runs on and who may run it. A caller names the query and gives values; the gateway checks them, binds them to the statement and runs it. This page lists the routes. The dashboard side is Named queries.
Routes
| Method and path | Role | Purpose |
|---|---|---|
GET /api/v1/queries | engineer | Every query, by name, with its SQL, parameters and activity |
GET /api/v1/queries/{id} | engineer | One query |
POST /api/v1/queries | engineer | Create a query (an administrator to make one that reads data public) |
PUT /api/v1/queries/{id} | engineer | Replace a query (an administrator if it is, or becomes, a public query that reads data) |
DELETE /api/v1/queries/{id} | engineer | Delete it (204) |
POST /api/v1/queries/test | engineer | Run a query as it stands in the editor, saved or not; an update is rolled back |
POST /api/v1/queries/{name}/run | viewer, and the role the query asks for | Run a query by name |
POST /api/v1/public/queries/{name}/run | none | Run a query that is public |
The query
QueryBody, used by POST and PUT. A read returns the same fields, with id, connectionName, createdBy, createdAt, updatedAt and activity added.
| Field | Type | Required | Meaning |
|---|---|---|---|
name | string | yes | Up to 64 lowercase letters, digits, - and _, starting with a letter; unique (409). It is what callers use in the path |
description | string | no | Up to 500 characters |
connectionId | string | yes | The database connection it runs on (400 if it does not exist). A connection with queries on it cannot be deleted |
kind | string | yes | query: one SELECT (or WITH ... SELECT), run in a read-only transaction. update: one INSERT, UPDATE or DELETE, committed when it succeeds |
sql | string | yes | The statement, up to 20,000 characters. Each parameter is written @name |
parameters | array | no | The values the statement takes, below. Every @name in the SQL has to be listed |
access | string | no | Who may run it: public, viewer, operator or engineer (default engineer) |
enabled | boolean | no | A query that is off answers 409 to a signed-in caller and 404 to a stranger |
maxRows | integer | no | The most rows a query answers with, 1 to 20,000 (default 1000) |
clientRatePerMinute | integer | no | For a public query: calls from one client address in a minute (default 10) |
totalRatePerMinute | integer | no | For a public query: calls from all clients together in a minute (default 120) |
The statement is checked when it is saved and again when it runs. A query has to pass the same check as a dashboard panel's SQL. An update has to be a single INSERT, UPDATE or DELETE, without a second statement and without DROP, ALTER, CREATE, TRUNCATE, GRANT, EXEC, CALL and the other words that change the database itself or reach outside it. The check is a guard against mistakes and the obvious misuse, not a sandbox: give the database connection a user that may only do what the queries need.
Who may make a query public depends on its kind, because what a public query answers with is seen by everyone. An engineer, or an API key with the engineer role, may set access to public on an update: its reach is an INSERT, UPDATE or DELETE of bound values, at a limited rate. Only an administrator may make a query that reads data (kind query) public, or change one that is, and only an administrator may turn a public update into a read (403). An engineer can still delete any public query.
Parameters
| Field | Type | Meaning |
|---|---|---|
name | string | Up to 32 letters, digits and _, starting with a letter; unique without regard to case. The SQL uses it as @name |
type | string | text, integer, number, boolean or time |
required | boolean | A required value has to be sent (and, for text, not blank) unless the parameter has a default |
maxLength | integer | For text: the longest value, 1 to 8,000 (default 255) |
defaultValue | string | Used when the caller leaves the parameter out. Written as text whatever the type |
| Type | The caller sends | The database receives |
|---|---|---|
text | A JSON string | A string. A NUL character is refused |
integer | A JSON number with no fraction | A 64-bit integer |
number | A JSON number | A double |
boolean | true or false | A boolean |
time | ISO 8601 text (UTC when it has no zone) or milliseconds since 1970 | A UTC time |
A value of the wrong type or length, a required value that is missing, a name the query does not have, or params that is not an object, is a 400 with a sentence saying which. A value that is null or left out is NULL in the statement.
Running a query
POST /api/v1/queries/contact-messages/run
Content-Type: application/json
{ "params": { "since": "2026-11-01T00:00:00Z" } }
A caller needs the role in the query's access; public needs none. A query answers with its rows:
{
"kind": "query",
"columns": [{ "name": "id", "type": "number" }, { "name": "received_at", "type": "time" }],
"rows": [[12, 1763116800000]],
"rowCount": 1,
"truncated": false,
"elapsedMs": 6
}
A column's type is time (the values are milliseconds since 1970, UTC), number, boolean or string; a NULL stays null. truncated is true when the query has more rows than maxRows. An update answers with the rows it changed:
{ "kind": "update", "rowsAffected": 1, "kept": true, "elapsedMs": 4 }
| Status | Meaning |
|---|---|
400 | A value does not fit its parameter, or the database refused the statement. An engineer is told the database's message; a viewer or operator is not |
403 | The caller's role is below the query's access |
404 | There is no query with that name |
409 | The query is turned off, its database connection is off or was removed, or the statement no longer passes the check |
Running a public query
POST /api/v1/public/queries/{name}/run takes the same body and gives the same answer, with no session and no key. It is for what a page or a program outside the plant has to do without signing in: a contact form, a check-in. It is closed to everything but public queries, and it tells a stranger as little as it can:
- A query that is not public, is turned off, or does not exist answers the same
404, so the route can't be used to find out what is there. - A failure after the checks (the database is unreachable, a constraint is broken) answers
503or500with a plain sentence. The database's message and where it lives stay in the gateway's log. - The request body is at most 32 KB (
413). - A query allows
clientRatePerMinutecalls a minute from one client address andtotalRatePerMinutefrom all clients together; past either it answers429withRetry-Afterin seconds. The client address is the one the nearest proxy saw (the last entry ofX-Forwarded-For), or the connection's own. The total is the limit that always holds, whatever addresses callers claim. The counts live in memory and start again at a restart.
A page on another site can call it from a browser when its origin is in AUTH_TRUSTED_ORIGINS (see Trusted origins); send the request with credentials: "omit", because a public query needs no cookie.
Testing a query
POST /api/v1/queries/test takes connectionId, kind, sql, parameters, optionally maxRows, and params (the values to run with). A query runs and answers as above. An update runs inside a transaction that is rolled back, and answers "kept": false with the rows it would have changed. Nothing is counted in the query's activity.
Activity
Each query carries counts since the gateway started, kept in memory: runs, failures, lastAt, lastOk, lastError, lastMs and perMinute (runs in the last 60 seconds). A call that fails its checks counts as a failure.