KQL (Kirby Query Language)
Kirby Headless provides an enhanced KQL endpoint at /api/kql with bearer token authentication, caching through Kirby's pages cache, and multi-language support. This extends the official KQL plugin with features commonly needed in headless setups, so that plugin has to be installed as well: composer require getkirby/kql.
By default, the endpoint uses Kirby's native API authentication – switch it to a bearer token as described under Authentication.
Making Requests
Send KQL queries to /api/kql with the bearer token in the Authorization header:
const response = await fetch("https://example.com/api/kql", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${process.env.KIRBY_API_TOKEN}`,
},
body: JSON.stringify({
query: "site",
select: {
title: true,
children: {
query: "site.children",
select: ["title", "url"],
},
},
}),
});
const data = await response.json();
Multi-Language Support
For multi-language sites, name the language with the X-Language header, such as X-Language: de, or a ?language= query parameter. The query parameter wins where a request carries both.
Cache Control
Query responses are cached only once Kirby's pages cache is enabled:
return [
'cache' => [
'pages' => [
'active' => true
]
]
];
Once the cache is on, send X-Cacheable: false to bypass it for a single request.
Each language, and each Kirby user a request is authenticated as, is cached separately. Pick one way to name the language and keep to it: the same query sent once with ?language=de and once with X-Language: de is cached twice.