PHP API, Hooks & CLI
Resolve Headless via ProcessWire\Headless::requireInstance($wire) (or $this->wire()->modules->get('Headless')).
Core ProcessWire\Headless Methods
Section titled “Core ProcessWire\Headless Methods”| Group | Key Methods |
|---|---|
| Mode & Routing | doHeadless(), isFullHeadless(), apiPrefix(), passthroughMatcher(), shouldPassThrough(), configure() |
| Registration | batchRegistration(), expose(), action(), registerScope(), registerScopes(), getAvailableScopes(), registerWebhookEvent(), registerWebhookEvents(), getAvailableWebhookEvents(), registerProvider(), registerTransformer(), registerGuard(), discoverProviderModules(), ensureProvidersPulled() |
| Auth & Security | issueJwtToken(), issuePreviewToken(), verifyPreviewToken(), verifyPreviewRequest(), verifyAccess(), ensurePersistentSecret(), secretFor(), withInternalCredentialWrite(), prepareHeadlessItemForSave(), cloneHeadlessItem() |
| Serialization & Dispatch | serialize(), resolveLanguageByKey(), pageAccessPolicy(), pageSerializer(), pagePaginator(), schemaProvider(), buildOpenApiSchema(), handle(), createRequestFromEnvironment() |
| Storage & Webhooks | keys(), tokens(), webhooks(), webhookDispatcher(), settings(), saveSettings(), clearCache() |
ProcessWire Hooks (Headless::___*)
Section titled “ProcessWire Hooks (Headless::___*)”| Hook | Arguments & Return | Timing | What It Does |
|---|---|---|---|
Headless::registerProviders |
(): void |
After | Invoked during ready() and resetState() so site code or modules register ResourceBlueprints, HeadlessActions, HeadlessProviderInterfaces, FieldTransformerInterfaces, scopes, and webhook events. |
Headless::clearCache |
(): void |
After | Fires when Headless API and provider caches are flushed via the Overview UI or $headless->clearCache(). |
ProcessHeadless::translations |
(): array |
After | Returns the localized UI string map for the Setup -> Headless React workspace. |
Register a Custom REST Provider & React to Cache Flushes
Section titled “Register a Custom REST Provider & React to Cache Flushes”<?php
declare(strict_types=1);
namespace ProcessWire;
use Totoglu\Headless\Blueprint\ResourceBlueprint;
$wire->addHookAfter('Headless::registerProviders', function (HookEvent $event): void { /** @var Headless $headless */ $headless = $event->object; $headless->expose( ResourceBlueprint::forTemplate('yacht') ->fields(['title', 'summary', 'cabins', 'weekly_rate']) ->publicRead() );});
$wire->addHookAfter('Headless::clearCache', function (HookEvent $event): void { $event->wire()->log->save('headless', 'Headless API schema and route caches were flushed.');});All 4 CLI Commands & Flags (vendor/bin/wire)
Section titled “All 4 CLI Commands & Flags (vendor/bin/wire)”1. headless:key:create
Section titled “1. headless:key:create”Creates a scoped whk_* Personal Access Token bound to a ProcessWire user:
php vendor/bin/wire headless:key:create "Aegean Frontend Storefront" \ --scopes=pages:read,entries:read \ --user=api-bot \ --rate-limit=240 \ --dry-run \ --jsonFlags: --scopes, --user, --rate-limit, --dry-run, --json.
2. headless:key:list
Section titled “2. headless:key:list”Lists all active and expired headless-keys records with prefixes, bound users, scopes, and last-used timestamps:
php vendor/bin/wire headless:key:list --jsonFlags: --json.
3. headless:routes
Section titled “3. headless:routes”Inspects all registered REST providers, resources, actions, and URL prefixes:
php vendor/bin/wire headless:routes --public --jsonFlags: --public, --json.
4. headless:token:prune
Section titled “4. headless:token:prune”Deletes expired and revoked whr_* refresh tokens from headless-tokens:
php vendor/bin/wire headless:token:prune --dry-run --force --jsonFlags: --dry-run, --force, --json.
Background Queue (DeliverHeadlessWebhookQueue) & Webhook Verification
Section titled “Background Queue (DeliverHeadlessWebhookQueue) & Webhook Verification”ProcessWire\DeliverHeadlessWebhookQueue (queue/DeliverHeadlessWebhookQueue.php) invokes WebhookDispatcher::deliverQueued($payload) for asynchronous webhook dispatch (webhook_id, event, timestamp, data). Run php vendor/bin/wire queue:work --tries=3 --sleep=3 in production so queued webhook deliveries process automatically.
Every outbound webhook includes X-Headless-Event, X-Headless-Timestamp, X-Headless-Nonce, and X-Headless-Signature (sha256=<hex>):
{ "event": "entries.saved", "timestamp": "2026-06-15T13:15:00Z", "data": { "type": "charter-yachts", "id": "0196f8d2-3c1a-7b40-9e12-8a4c5d6e7f01" }}<?php
declare(strict_types=1);
$rawBody = file_get_contents('php://input') ?: '';$timestamp = $_SERVER['HTTP_X_HEADLESS_TIMESTAMP'] ?? '';$received = $_SERVER['HTTP_X_HEADLESS_SIGNATURE'] ?? '';$expected = 'sha256=' . hash_hmac('sha256', $timestamp . '.' . $rawBody, (string) getenv('HEADLESS_WEBHOOK_SECRET'));$isValid = hash_equals($expected, $received);Troubleshooting
Section titled “Troubleshooting”401 Unauthorized With Authorization: Bearer on Apache/FastCGI
Section titled “401 Unauthorized With Authorization: Bearer on Apache/FastCGI”Symptom: Valid whk_* or JWT tokens are ignored by the API.
Fix: Ensure your web server forwards the Authorization header to PHP (RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] on Apache), or pass X-Api-Key: whk_... for API keys.
Frontend Sitemaps or Custom URL Hooks Return JSON in Full Headless Mode
Section titled “Frontend Sitemaps or Custom URL Hooks Return JSON in Full Headless Mode”Symptom: /sitemap.xml or a custom module endpoint is intercepted by Full Headless mode.
Fix: Add the path pattern to Settings -> Passthrough Paths (or $headless->doHeadless(..., passthroughPaths: [...])).
403 Forbidden Updating Settings or Issuing full_access Keys
Section titled “403 Forbidden Updating Settings or Issuing full_access Keys”Symptom: A user with the headless permission cannot modify global Settings or create full_access keys.
Fix: Global headless Settings writes, full_access keys, and cross-user key assignment are intentionally restricted to superuser accounts (and blocked when $config->demo is active).