Skip to main content
Version: 0.1

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 pathRolePurpose
GET /api/v1/queriesengineerEvery query, by name, with its SQL, parameters and activity
GET /api/v1/queries/{id}engineerOne query
POST /api/v1/queriesengineerCreate a query (an administrator to make one that reads data public)
PUT /api/v1/queries/{id}engineerReplace a query (an administrator if it is, or becomes, a public query that reads data)
DELETE /api/v1/queries/{id}engineerDelete it (204)
POST /api/v1/queries/testengineerRun a query as it stands in the editor, saved or not; an update is rolled back
POST /api/v1/queries/{name}/runviewer, and the role the query asks forRun a query by name
POST /api/v1/public/queries/{name}/runnoneRun 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.

FieldTypeRequiredMeaning
namestringyesUp to 64 lowercase letters, digits, - and _, starting with a letter; unique (409). It is what callers use in the path
descriptionstringnoUp to 500 characters
connectionIdstringyesThe database connection it runs on (400 if it does not exist). A connection with queries on it cannot be deleted
kindstringyesquery: one SELECT (or WITH ... SELECT), run in a read-only transaction. update: one INSERT, UPDATE or DELETE, committed when it succeeds
sqlstringyesThe statement, up to 20,000 characters. Each parameter is written @name
parametersarraynoThe values the statement takes, below. Every @name in the SQL has to be listed
accessstringnoWho may run it: public, viewer, operator or engineer (default engineer)
enabledbooleannoA query that is off answers 409 to a signed-in caller and 404 to a stranger
maxRowsintegernoThe most rows a query answers with, 1 to 20,000 (default 1000)
clientRatePerMinuteintegernoFor a public query: calls from one client address in a minute (default 10)
totalRatePerMinuteintegernoFor 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​

FieldTypeMeaning
namestringUp to 32 letters, digits and _, starting with a letter; unique without regard to case. The SQL uses it as @name
typestringtext, integer, number, boolean or time
requiredbooleanA required value has to be sent (and, for text, not blank) unless the parameter has a default
maxLengthintegerFor text: the longest value, 1 to 8,000 (default 255)
defaultValuestringUsed when the caller leaves the parameter out. Written as text whatever the type
TypeThe caller sendsThe database receives
textA JSON stringA string. A NUL character is refused
integerA JSON number with no fractionA 64-bit integer
numberA JSON numberA double
booleantrue or falseA boolean
timeISO 8601 text (UTC when it has no zone) or milliseconds since 1970A 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 }
StatusMeaning
400A 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
403The caller's role is below the query's access
404There is no query with that name
409The 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 503 or 500 with 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 clientRatePerMinute calls a minute from one client address and totalRatePerMinute from all clients together; past either it answers 429 with Retry-After in seconds. The client address is the one the nearest proxy saw (the last entry of X-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.