mq-web-api
HTTP/REST server that exposes the mq markdown query language over the network.
API Endpoints
| Method |
Path |
Description |
GET |
/health |
Liveness check (status and version) |
GET |
/ready |
Readiness check; 503 once the server is shutting down |
GET |
/metrics |
Prometheus metrics (opt-in, see METRICS_ENABLED) |
POST |
/{query} |
Curl-friendly shortcut: query in the path, raw body (Markdown/HTML/XML/JSON/CSV/...) |
GET |
/api/v1/query |
Execute a query (query-string parameters) |
POST |
/api/v1/query |
Execute a query (JSON body) |
POST |
/api/v1/batch |
Execute a query against multiple documents in one request |
POST |
/api/v1/check |
Type-check a query |
POST |
/api/v1/format |
Format a query |
GET |
/api/v1/functions |
List builtin mq functions |
GET |
/api/v1/functions/{name} |
Docs for a single builtin function |
GET |
/api/v1/selectors |
List builtin mq selectors |
GET |
/api/v1/selectors/{name} |
Docs for a single builtin selector (with or without a leading .) |
POST |
/api/v1/lint |
Lint a query |
GET |
/api/v1/openapi.json |
OpenAPI specification |
GET |
/docs |
Swagger UI |
Legacy paths (/api/query, /api/check, /api/format, /openapi.json) redirect permanently to the /api/v1/ equivalents.
POST /{query}
Curl-friendly shortcut for one-off queries: the mq query goes in the URL path, the input content is the raw request body.
curl --data-binary @doc.md https://api.mqlang.org/.h1
| Parameter |
Location |
Required |
Description |
query |
Path |
Yes |
mq query expression. Reserved characters (|, ?, #) must be percent-encoded, e.g. .h1 | .text → .h1%20%7C%20.text |
| (body) |
Body |
No |
Raw input content |
input_format |
Query |
No |
markdown (default), mdx, text, html, raw, null, csv, tsv, psv, json, yaml, toml, xml, hcl, toon. See auto-detection below. |
output_format |
Query |
No |
markdown (default), html, text, json, none |
html, xml, and json are auto-detected from the body's leading bytes when input_format is omitted (<!doctype html>/<html, <?xml, and {/[ respectively). csv, tsv, psv, yaml, toml, hcl, and toon have no reliable content signature and always need an explicit input_format.
# HTML/XML/JSON are auto-detected — no ?input_format= needed
curl --data-binary @page.html https://api.mqlang.org/.h1
curl --data-binary @data.json 'https://api.mqlang.org/json::json_to_markdown_table()'
# CSV (and tsv/psv/yaml/toml/hcl/toon) need an explicit input_format,
# then use the matching builtin module inside the query
curl --data-binary @data.csv 'https://api.mqlang.org/csv::csv_to_markdown_table()?input_format=csv'
# Force a format explicitly if auto-detection isn't what you want
curl --data-binary @page.html "https://api.mqlang.org/.h1?input_format=text"
Use --data-binary, not -d/--data. curl -d @file strips newlines from the file, which breaks Markdown/HTML/XML parsing (blank lines/whitespace are often significant). --data-binary sends the file exactly as-is.
For queries that need modules, args, or aggregate, use POST /api/v1/query instead.
GET /api/v1/query
| Parameter |
Required |
Description |
query |
Yes |
mq query string |
input |
No |
Input content |
input_format |
No |
markdown (default), mdx, text, html, raw, null, csv, tsv, psv, json, yaml, toml, xml, hcl, toon |
POST /api/v1/query
{
"query": ".h",
"input": "## Markdown Content\n\nBody text.",
"input_format": "markdown",
"output_format": "markdown",
"modules": ["json"],
"args": { "key": "value" },
"aggregate": false
}
| Field |
Type |
Description |
query |
string |
mq query string |
input |
string? |
Input content |
input_format |
string? |
markdown (default), mdx, text, html, raw, null, csv, tsv, psv, json, yaml, toml, xml, hcl, toon |
output_format |
string? |
markdown (default), html, text, json, none |
modules |
string[]? |
Builtin module names to load (e.g. "json", "csv") |
args |
object? |
String variables passed to the engine |
aggregate |
bool? |
Aggregate all input nodes before querying (equivalent to CLI -A) |
POST /api/v1/batch
Runs one query against multiple documents in a single request, avoiding an HTTP round trip per document. Each document is processed independently — one failing document doesn't fail the others.
{
"query": ".h1",
"inputs": ["# Doc One\n\nBody.", "# Doc Two\n\nBody."],
"input_format": "markdown",
"output_format": "markdown"
}
| Field |
Type |
Description |
query |
string |
mq query string |
inputs |
string[] |
Documents to run the query against, one per entry (max 100) |
input_format |
string? |
Same as POST /api/v1/query |
output_format |
string? |
Same as POST /api/v1/query |
modules |
string[]? |
Same as POST /api/v1/query |
args |
object? |
Same as POST /api/v1/query |
aggregate |
bool? |
Same as POST /api/v1/query |
Response, items ordered like inputs:
{
"items": [
{ "results": ["# Doc One"], "error": null },
{ "results": ["# Doc Two"], "error": null }
]
}
Returns HTTP 400 if inputs has more than 100 entries. The whole batch shares a wall-clock budget (BATCH_TIMEOUT_SECONDS); documents not processed by then come back with an error instead of results.
Errors
Every error response is an RFC 9457 application/problem+json body, including 404/405, malformed JSON bodies, 413, 408, 429 and 5xx:
{ "status": 400, "type": "about:blank", "title": "Invalid query parameters", "error": "Invalid input_format 'csvv', expected one of: markdown, mdx, ..." }
Every response carries an X-Request-Id header (a client-supplied one is kept), also present in the server logs. 429 and 503 responses include Retry-After. Unknown input_format / output_format values are rejected with 400 rather than ignored.
A panic while handling a request is caught and answered with 500; the server keeps running and the panic is logged and counted in mq_panics_total.
POST /api/v1/check
{ "query": "upcase | downcase" }
Returns an array of errors (empty means no issues). Always returns HTTP 200.
GET /api/v1/functions
Returns all builtin mq functions with their description, params, param types, return type,
runnable examples, and required capability (a Cargo feature name, e.g. file-io; functions
requiring a capability this deployment wasn't built with are simply absent). Sourced from the
same catalog as mq help, so the two can't drift apart.
GET /api/v1/functions/{name}
Docs for a single builtin function, same shape as one entry from /api/v1/functions. 404 if
name isn't a builtin function.
GET /api/v1/selectors
Returns all builtin mq selectors with their description, params, param types, matched/produced
type, examples, and required capability.
GET /api/v1/selectors/{name}
Docs for a single builtin selector, with or without a leading . (h1 and .h1 both work).
404 if name isn't a builtin selector.
POST /api/v1/lint
{ "query": "let x = .h1 | .text" }
Returns an array of style/correctness diagnostics (empty means no issues). Always returns HTTP 200.
POST /api/v1/format
{
"query": "if(a):1 elif(b):2 else:3",
"indent_width": 2,
"sort_imports": false,
"sort_functions": false,
"sort_fields": false
}
Configuration
All settings are controlled through environment variables.
Server
| Variable |
Default |
Description |
HOST |
0.0.0.0 |
Bind address |
PORT |
8080 |
Bind port |
RUST_LOG |
mq_web_api=debug,tower_http=debug |
Log level filter |
LOG_FORMAT |
json |
Log format: json or text |
CORS_ORIGINS |
* |
Comma-separated allowed origins |
QUERY_TIMEOUT_SECONDS |
10 |
Max seconds a single query may run before it's aborted |
MAX_REQUEST_BODY_SIZE |
10485760 |
Max accepted HTTP request body size, in bytes. Larger requests get 413 Payload Too Large |
REQUEST_TIMEOUT_SECONDS |
30 |
Max seconds a single HTTP request may take end-to-end (routing, auth, rate limiting, handler). Exceeding it returns 408 Request Timeout |
MAX_CONCURRENT_QUERIES |
2 x CPU cores |
Queries evaluated at the same time. Others wait up to QUERY_TIMEOUT_SECONDS for a slot, then get 503 with Retry-After |
BATCH_TIMEOUT_SECONDS |
20 |
Wall-clock budget of one POST /api/v1/batch request. Keep it below REQUEST_TIMEOUT_SECONDS |
METRICS_ENABLED |
false |
Serve Prometheus metrics at GET /metrics (requires the read scope when auth is enabled) |
SHUTDOWN_DRAIN_SECONDS |
0 |
After SIGTERM/SIGINT, /ready answers 503 for this long before the listener closes, so a load balancer can drain the instance |
SHUTDOWN_TIMEOUT_SECONDS |
30 |
Max seconds to wait for in-flight requests after the listener closed; the process exits afterwards regardless |
/health, /ready and /metrics are exempt from rate limiting.
Query Cache
Repeated requests to GET/POST /api/v1/query and POST /{query} with the same
query, input, input_format, modules, args, output_format, and
aggregate are served from a short-lived in-memory cache instead of
re-evaluating the query. Queries that call a nondeterministic builtin (now,
uuid, uuid_v4, uuid_v7, rand, rand_int, random_string) are never
cached.
| Variable |
Default |
Description |
QUERY_CACHE_ENABLED |
true |
Enable the query result cache |
QUERY_CACHE_TTL_SECONDS |
30 |
How long a cached result stays fresh |
QUERY_CACHE_MAX_ENTRIES |
1000 |
Max number of cached query results |
QUERY_CACHE_MAX_ENTRY_BYTES |
1048576 |
Max bytes retained by one cached response |
QUERY_CACHE_MAX_TOTAL_BYTES |
33554432 |
Max bytes retained by all cached responses |