Internal Variables
In addition to the form variables that the person or caller
running the query fills in, a query can use internal variables: :-prefixed
values that never get an input field. Some are supplied by Kvery when the query
runs — :log and the
path variables of a custom URL — and others
you declare yourself in a statement of the query
(:name = …;), including
:headers and :body for HTTP requests.
The :log variable
:log is a built-in variable that Kvery replaces with a JSON object describing the
run, with these fields in this order:
ip— the IP address of the caller,referer_url— the referring page,executed_by_nameandexecuted_by_email— the user who runs the query (signed in to Kvery, with an MCP token, or with Basic Auth on the public query API); for anonymous runs both areNon logged user.
Read a single field with an index, for example :log[executed_by_email]. Kvery
itself does not read or store the value: it is there for your own SQL, for example
to record who ran a write query:
INSERT INTO audit_log (action, user_email, ip)
VALUES ('export', ':log[executed_by_email]', ':log[ip]');
Because it is provided by the platform, :log behaves consistently across the
editor, scheduled runs and
API calls.
Its value depends on the caller (IP address, referer, user), so a query that uses
:log is not served from or stored in the
result cache: it runs every time.
Path variables of a custom URL
When a query is shared with a custom URL that contains
$variables (for example users/$id), the matching part of the called URL is
available as an internal variable of the same name (:id):
SELECT * FROM users WHERE id = ':id';
A path variable takes letters, digits, _ and -. It is also a request value of
the same name, so a $id form variable gets it as well (there a URL or body
parameter named id takes precedence).
Declaring variables
Declare a variable with a statement of its own that starts with :name =. The
value it gets depends on what follows the =:
- A
SELECTruns on the query's connection. One row with one column gives that value (an empty value or0givesNULL); one row with several columns gives a JSON object of the row. Several rows with one column give a comma-separated list of the values, with each value in apostrophes when the first one is not a number, ready forIN (:name); several rows with several columns give a JSON object of such a list for every column. No rows giveNULL. - An
INSERTruns and gives the ID of the inserted row (0when no row was inserted). - An HTTP request such as
GET('…')gives the body of the response (see HTTP requests). - Anything else is taken as text, without its apostrophes.
Read a field of a JSON value with :name[key] (also nested, :name[a][b]); a key
that the value does not have gives NULL. With ON ERROR RETURN '…' (optionally
followed by SET HTTPCODE '…') on the declaration, the run answers with that message
when the value is empty, 0 or NULL:
:customer = SELECT id, name FROM customers WHERE id = '$id'
ON ERROR RETURN 'Customer not found' SET HTTPCODE '404';
:order_ids = SELECT id FROM orders WHERE customer_id = '$id';
SELECT ':customer[name]' AS customer, product, quantity
FROM order_items WHERE order_id IN (:order_ids);
:headers and :body
Declare :headers and :body as JSON objects to add headers and body fields to
the HTTP requests the query makes with GET('…'), POST('…') and the other
HTTP request directives:
:headers = {"Authorization": "Bearer my-token"};
:body = {"status": "active"};
:result = RPOST('https://api.example.com/items');
Webhooks (ON SUCCESS …, ON ERROR …) do not use them: they send only their own
payload.
Internal vs. form variables
| Form variables | Internal variables | |
|---|---|---|
| Who supplies the value | The person/caller running the query | Kvery (:log, path variables) or a statement of the query (:name = …;) |
| Prefix | $ (e.g. $status, $from_date) | : (e.g. :log) |
| Shows an input field | Yes | No |
| Safe insertion | Yes (apostrophes escaped) | Yes, inside quotes (see below) |
How internal values are inserted
An internal variable — :log or one you declare from a statement, such as
:name = (SELECT name FROM customers WHERE id = 1); — is replaced by its value in
the later statements:
- Inside quotes (
':name') the value is escaped like a form value, so a stored text with an apostrophe (or, on MySQL and MariaDB, a backslash) stays inside the quoted text. - Without quotes (
IN (:ids),:count + 1, a column name) the value is inserted as it is — use this only for numbers, lists of numbers and names. - In the texts of directives (
return(':name'),ON ERROR RETURN '…', webhook payloads) the value is inserted as it is. - A value that contains a directive of Kvery —
return(orreturnk(, an HTTP call such asGET('…'),on error return 'or the beginning of a webhook directive such ason success POST '— is inserted asNULL, so data read from a database never runs as a directive. Block and comment markers andsnippet(in a value (for example[20%]or*/5 * * * *) are inserted as they are and are not read as Kvery's syntax, because blocks, comments and snippets are processed before the variables are filled in.
Combining them
Internal and form variables work together in one query. Use form variables to let the user filter, and internal variables to bring in what Kvery knows about the run:
SELECT *
FROM orders
WHERE created_at >= '$from_date'
AND created_by = ':log[executed_by_email]';
Here $from_date is a form variable the user fills in, and
:log[executed_by_email] is the e-mail address of the user who runs the query.
Tips
- Treat internal variables as reserved — don't reuse their names for your own form variables.
- See the related directives for controlling output, status codes and webhooks alongside your variables.