Skip to main content

Public Query API

Every query in Kvery has a callable HTTP endpoint: a shared query answers anyone who has its URL (for a share with a password, see Securing the endpoint), and a query that is not shared answers only calls with Basic Auth. Your application, a script, or another service can hit that endpoint and receive the query's result as JSON — no database credentials on the client side, just a URL.

The endpoint​

Each shared query is addressed by its hash:

https://app.kvery.io/query/api/{hash}

The {hash} is the share identifier of the query — it has one from its creation — as the API (JSON) block of the share window shows it (see Share URLs and embedding). Treat it like a secret: anyone with the URL can call the endpoint of a share that has no password.

The endpoint runs the published version of the query: an unpublished draft does not change what callers get until you publish it. Only an editor of the query who is signed in to Kvery can request the draft, by adding ?draft to the URL.

HTTP methods and write queries​

The endpoint of a share hash answers GET, POST, PUT, PATCH and DELETE requests alike: the method does not decide what the query does, its SQL does. Kvery runs a query as a write query when its SQL contains INSERT, UPDATE, DELETE, REPLACE, ALTER or CREATE followed by a space — anywhere in the text, in any letter case, also inside a comment or a string. Every other query is a read query and returns its rows.

  • A read query takes its values from the query string or the request body, with any method.
  • A write query takes its values from the query string and the request body of a POST, PUT, PATCH or DELETE request. The query-string values of a GET request are not passed to it; a body sent with a GET request, the path variables of a custom URL and the bearer token ($token, see below) still reach it. When its statements are written in blocks ({% … %}) and the last block starts with SELECT or EXEC, it runs like a read query: it takes its values with any method and returns the rows of that block.

To make an endpoint answer a single method, give the query a Custom URL in the share dialog and choose the method in the selector in front of it (GET, POST, PUT, PATCH or DELETE). That URL then answers only requests of this method; other methods get 404. With All, the URL answers every method. The browser and mobile share links are offered only for All and GET.

Passing parameters​

Form variables in your SQL (for example $status) become request inputs. Supply them as query-string parameters or in the request body as JSON or form fields (a write query: see above) — the parameter name matches the variable name without the $ (so $status is passed as status=…):

# GET with a parameter
curl "https://app.kvery.io/query/api/{hash}?status=open"
# POST a write query
curl -X POST "https://app.kvery.io/query/api/{hash}" \
-H "Content-Type: application/json" \
-d '{"email":"[email protected]"}'

A value that contains Kvery's own query syntax is refused with status 400 — for example a block or comment marker ({%, %}, [%, %], /*, */), snippet(, return( or an HTTP call such as GET(', also when a line break splits it. So values such as */5 * * * * or [50%] are refused too. See How values are inserted for the full list and the message.

Request headers as values​

The request headers of an API call are request values as well (except Host, Cookie, Accept, Accept-Encoding, Connection, Content-Length and empty headers): each header is available under its own name and under its name in lower case. A header whose name is a valid variable name (letters, digits and _, for example Authorization) fills the form variable of that name ($authorization). A header takes precedence over a query-string or form-encoded body value of the same name; in a read query a field of a JSON body takes precedence over the header. A write query called with GET takes no header values, as it takes no query-string values.

Header values are request data

A caller can send any header with any value, so a header value reaches your SQL like any other request value: treat it as untrusted input.

The Authorization and Proxy-Authorization headers of a call that Kvery itself authenticated — Basic Auth of a query that is not shared — are not request values of the run, and HTTP request directives of the query never forward them; nor do they forward the cookies or the device ID header of the mobile app.

Caching​

GET responses that anyone may get — the query is shared without a password, its group has no IP whitelist, the request carries no Authorization header and doesn't come from a signed-in Kvery user — are served with Cache-Control: private, max-age=0: neither the caller nor proxies and CDNs reuse the response, so every request gets the current result. Every other response (Basic Auth, a bearer token or any other Authorization header, a signed-in user, share password, IP whitelist) is marked private, no-store. Queries can also enable Kvery's own result cache, which short-circuits identical reads for speed; cached results are only returned to callers who pass the same checks. Write requests are never cached. See Export & cache for how caching behaves.

Securing the endpoint​

  • Set a password on the share. The share link then asks for it in the browser, and the API endpoint answers only calls with Basic Auth — the e-mail address and password of a Kvery user who can access the query, as for a query that is not shared; the share dialog notes this under the API link. An API call cannot send the share password itself. A browser session or a mobile device that has already entered the share password is accepted as well. Wrong share passwords are counted per client address (an IPv6 address counts by its /64 network) and per link: after 20 wrong passwords, every attempt from that address for that link is refused, even with the right password, until 5 minutes have passed since the last wrong attempt.
  • Set an expiry so the hash stops working after a date.
  • Check a token in the query's SQL: a token sent in an Authorization: Bearer … header reaches the query as its token parameter ($token), so the SQL can compare it with a value of your own. Kvery doesn't write it into the query's run page and masks it in the query log.
  • Restrict the group with an IP whitelist.
  • Apply rate limits so the endpoint can't be hammered.

Calling a query that is not shared​

The endpoint of a query that is not shared only answers calls with Basic Auth: the e-mail address and password of a Kvery user who can access the query. Basic Auth sends the password with every request and does not ask for the two-factor code, so for scripts and integrations prefer MCP tokens and the MCP server's REST API. Failed Basic Auth attempts are counted for an address and e-mail address together (10), for an address (50) and for an e-mail address (100); an IPv6 address counts by its /64 network. Once a limit is reached, Basic Auth is blocked: the endpoint answers 429 Too Many Requests with a Retry-After header until 15 minutes have passed since the last failed attempt. Password checks that are still running count toward the limits too (see the MCP server limits): a call that would go over a limit waits up to 3 seconds, and answers 429 with Retry-After: 1 if nothing frees up.

The response​

The endpoint answers with JSON (Content-Type: application/json):

ResultBody
Rows{"status":1,"response":[{"id":1,"name":"Ada"},…]} — one object per row, the column names as keys
A statement that returned no rows and succeeded (for example an INSERT){"status":1,"response":""}
A read query that found no rows{"status":0,"response":""}
An error{"status":0,"response":"<message>"}

When the SQL of a form field (a default value or a label written as SQL) fails, the answer is the error message as plain text (Content-Type: text/plain) with the status of the error. A return() value is sent exactly as the query builds it, with Content-Type: application/json also when it is not JSON.

Add ?ri=<n> to the URL to get only the row with the index n (counted from 0) as a bare object, without the status / response envelope: {"id":1,"name":"Ada"}. When the result has no such row, the answer is {"status":0,"response":"Response index not found! (<n>)"}. A query that answers with return() or returnk() sends that answer instead.

Errors​

StatusresponseWhen
400Missing parameters: <names>A required form field has no value
400A parameter value is not allowed: it contains syntax of the query engine (…)A value contains Kvery's query syntax (see above)
400The username or password is incorrect!Wrong Basic Auth credentials (MCP server calls get 401 instead)
401Unauthorized access!A query that is not shared, or whose share has a password, was called without Basic Auth; or the Basic Auth user cannot run it (no access, or member tags do not allow it)
403IP not allowed!The caller's address is not on the group's IP whitelist
403The request was refused because it was not sent from this site.A browser that is signed in to Kvery sent a POST, PUT, PATCH or DELETE call from a page of another site (the body is {"responseCode":"ERROR","response":"…"}); calls from servers and scripts, and calls without a Kvery session, are not affected
404Query not found!Unknown hash, expired share, or a path variable of a custom URL is missing
404You have reached the package limit.<br />Subscribe or upgrade to a larger package!The group's plan is not active, or the group holds more queries than its plan allows (a browser signed in to Kvery that has the query's group open is redirected to the packages page instead)
429Rate limit exceeded. Try again later.The query's rate limit is reached, or Basic Auth is blocked after failed attempts
500The database's messageThe SQL failed. Only an editor of the query who calls the URL from a browser signed in to Kvery gets the database's message; every other caller, Basic Auth calls included, gets Something went wrong!

ON ERROR RETURN '…' replaces the database's message with your own, and SET HTTPCODE sets the status of that answer (see below).

When the query has a rate limit, its answers carry the headers X-RateLimit-Limit, X-RateLimit-Remaining and X-RateLimit-Reset (the Unix time when the current window ends); a 429 answer also has Retry-After.

Shaping the response​

Use directives to control status codes and error messages (SET HTTPCODE, ON ERROR RETURN). Column modifiers hide nothing from the JSON: it contains every column of the result — hidden:: columns too. With the query's Slug JSON keys (response time may be slower) option, which is on for new queries, each key is the slug of the column name without its modifier prefix and JSON text values are returned as nested JSON; with the option off, each key keeps its prefix (for example hidden::password_hash). A call can choose either form with jsonSlug=1 / jsonSlug=0 (see URLs and embedding). Leave columns that callers must not see out of the SELECT.

Next steps​

  • Send many operations in one call with batch requests.
  • Generate interactive docs for a whole group with Swagger.