SR
Usage

Caching

Learn about caching your queries with Craft Cuery API


The API endpoints are blazingly fast. 🚀

Caching Strategy

The plugin utilizes Craft's built-in caching mechanism, similar to the {% cache %} tag in Twig.

Caches are automatically invalidated when any element in the request changes. The cache will persist based on the cacheDuration setting defined in your configuration. You can adjust this in your Craft CMS configuration.

You can learn how to override that in the Settings Page.

Note

A new cache is created when a previously unqueried element is requested. However, simply changing the order of your GET parameters will NOT generate a new cache.

Eager Loading

To optimize the initial cache generation, eager loading is used to reduce the number of database queries. This is handled automatically through an internal eager loading map, so no additional configuration is required. You should however try to reuse relational fields to keep things scalable.

Frontend Cache Invalidation

If your frontend caches pages or API responses itself (e.g. Nitro route rules, Next.js data cache, a CDN), it needs to know when content changes. Since version 3.8.0 the Query API can notify your frontend via webhooks, whenever its caches get invalidated.

Webhooks

Define one or more endpoints in your settings:

config/query-api.php
return [
  '*' => [
    'webhooks' => [
      [
        'url' => '$FRONTEND_WEBHOOK_URL',
        'secret' => '$FRONTEND_WEBHOOK_SECRET',
      ],
    ],
  ],
];

A webhook is sent when:

  • an element gets saved or deleted (drafts and revisions are ignored)
  • the Query API caches get cleared (Clear Caches utility, clear-caches command, schema changes)
  • an entry goes live or expires and the invalidate-scheduled command runs

Webhooks are sent through the Craft queue. All changes within a few seconds are batched into a single request, so saving an entry with many nested elements, or resaving thousands of entries, doesn't flood your frontend. Failed requests are retried, with up to 5 attempts per webhook.

Note

Make sure your queue is running, e.g. via a queue daemon. Otherwise webhooks are only sent when the queue gets triggered by a control panel request.

Payload

Each webhook is a POST request with a JSON body:

{
  "event": "invalidate",
  "all": false,
  "tags": ["element::123", "element::craft\\elements\\Entry::section:2"],
  "timestamp": 1794558454
}
  • all: true if every cache should be cleared (e.g. after clearing all caches in Craft).
  • tags: The Craft cache tags that got invalidated. Only relevant if you use the cache tags header.

The type is available as CacheWebhookPayload in @query-api/js (also re-exported by @query-api/vue and @query-api/nuxt).

Verifying the Signature

If a secret is set, every request contains an X-Query-Api-Signature header: sha256= followed by the HMAC-SHA256 of the raw request body. Always verify it before clearing any caches:

import { createHmac, timingSafeEqual } from 'node:crypto'

function isValidSignature(rawBody: string, signature: string, secret: string) {
  const expected = 'sha256=' + createHmac('sha256', secret).update(rawBody).digest('hex')
  return signature.length === expected.length && timingSafeEqual(Buffer.from(signature), Buffer.from(expected))
}

For a complete Nuxt example, see Cache Invalidation in Nuxt.

Scheduled Entries

Entries that go live because of their post date, or expire because of their expiry date, don't trigger a save. To notify your frontend anyway, run the invalidate-scheduled command via cron, e.g. every minute:

* * * * * php craft query-api/cache/invalidate-scheduled

Cache Tags Header

For precise invalidation, the API can expose the cache tags of every response in a header (see cacheTagsHeader). Tag your frontend cache entries with them, and only clear the entries whose tags intersect with the tags of the webhook.

Warning

Queries returning many elements produce long headers. Make sure your proxies allow large response headers (e.g. proxy_buffer_size in nginx).


Copyright © 2026 Samuel Reichör