Skip to content

Lifecycle Hooks

Lifecycle hooks let you observe what the client is doing without replacing the client internals.

Hooks are useful for logging, metrics, tracing, and status notifications.

Register Hooks

php
use PhpN8n\Client\Hooks\HookContext;
use PhpN8n\Client\Hooks\HookRegistry;
use PhpN8n\Client\Hooks\LifecycleHook;
use PhpN8n\Client\N8nClient;

$hooks = (new HookRegistry())
    ->on(LifecycleHook::BeforeWebhookRequest, function (HookContext $event): void {
        $webhook = $event->context()['webhook'];
    })
    ->on(LifecycleHook::ExecutionStatusChanged, function (HookContext $event): void {
        $previous = $event->context()['previous_status'];
        $current = $event->context()['status'];
    })
    ->on(LifecycleHook::ExceptionThrown, function (HookContext $event): void {
        $exception = $event->exception();
    });

$client = new N8nClient(
    httpClient: $httpClient,
    requestFactory: $requestFactory,
    streamFactory: $streamFactory,
    hooks: $hooks,
);

HookRegistry is immutable. Each on() call returns a new registry instance.

Available Hooks

HookWhen it runs
BeforeWebhookRequestBefore sending a webhook HTTP request.
AfterWebhookResponseAfter a successful webhook HTTP response is received.
ExecutionReferenceCreatedAfter a webhook response exposes an execution reference.
BeforeExecutionRefreshBefore fetching an execution from the n8n API.
AfterExecutionRefreshAfter an execution is fetched and mapped.
BeforePollingStartsBefore execution polling begins.
PollingAttemptBefore each polling attempt.
ExecutionStatusChangedWhen polling observes a status change.
ExecutionSucceededWhen polling reaches a successful terminal status.
ExecutionFailedWhen polling reaches a failed or canceled terminal status.
ExecutionTimedOutWhen polling times out.
ExceptionThrownWhen the client catches and rethrows an exception during supported operations.

Hook Context

Each callback receives HookContext.

php
$hook = $event->hook();
$context = $event->context();
$exception = $event->exception();

Common context keys:

HookContext keys
BeforeWebhookRequestwebhook, webhook_request, http_request
AfterWebhookResponsewebhook, webhook_request, http_response
ExecutionReferenceCreatedreference, webhook_response
BeforeExecutionRefreshreference, options
AfterExecutionRefreshreference, result
BeforePollingStartsreference, polling
PollingAttemptreference, attempt
ExecutionStatusChangedreference, previous_status, status, result
ExecutionSucceededreference, result
ExecutionFailedreference, result
ExecutionTimedOutreference, last_result

Logging Example

php
$hooks = (new HookRegistry())
    ->on(LifecycleHook::PollingAttempt, function (HookContext $event): void {
        $reference = $event->context()['reference'];
        $attempt = $event->context()['attempt'];

        error_log("Polling n8n execution {$reference->id()}, attempt {$attempt}");
    })
    ->on(LifecycleHook::ExecutionSucceeded, function (HookContext $event): void {
        $reference = $event->context()['reference'];

        error_log("n8n execution {$reference->id()} succeeded");
    });

Metrics Example

php
$hooks = (new HookRegistry())
    ->on(LifecycleHook::AfterWebhookResponse, function (HookContext $event): void {
        $response = $event->context()['http_response'];

        // Example only. Send this to the metrics system used by your app.
        error_log('n8n_webhook_status=' . $response->getStatusCode());
    });

Important Behavior

Hook callbacks run inside the client process. Keep them fast and safe.

If a hook callback throws, the operation can stop. Treat hooks as observability code unless you intentionally want to abort the request or polling flow.

Released under the MIT License.