Webhooks & callbacks
A webhook is a request your application sends. Declare it by its payload: a JsonResource marked #[Webhook].
The generator finds the class without any route and writes it under webhooks, never under paths. The same class
is what your application sends, so the document describes exactly what goes out. #[Callback] documents a request
sent back to a URL the client gave in its own request.
Declare a webhook Since 1.3
Write the payload as a resource
use Ark4ne\OpenApi\Attributes\Header;
use Ark4ne\OpenApi\Attributes\Webhook;
use App\Models\Article;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
/**
* An article was published.
*
* Sent once, when the article goes live.
*
* @mixin Article
*/
#[Webhook('article.published')]
#[Header('X-Signature', required: true, description: 'HMAC of the body')]
#[Header('X-Attempt', type: 'positive-int', example: 1)]
final class ArticlePublished extends JsonResource
{
public function toArray(Request $request): array
{
return [
'id' => $this->id,
'title' => $this->title,
'author' => $this->whenLoaded('author'),
];
}
}
The name of #[Webhook] is the key of the webhook and its operationId. It is required.
Send it
The resource resolves to an array, so any client sends it:
$payload = ArticlePublished::make($article)->resolve();
Http::withHeaders(['X-Signature' => $signature])->post($url, $payload);
// spatie/laravel-webhook-server, or any other library
WebhookCall::create()->url($url)->payload($payload)->dispatch();
The result, under webhooks:
{
"webhooks": {
"article.published": {
"post": {
"tags": ["Webhooks"],
"summary": "An article was published.",
"description": "Sent once, when the article goes live.",
"operationId": "article.published",
"parameters": [
{
"name": "X-Signature",
"in": "header",
"description": "HMAC of the body",
"required": true,
"schema": { "type": "string" }
},
{
"name": "X-Attempt",
"in": "header",
"schema": { "type": "integer", "minimum": 1, "example": 1 }
}
],
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": { "$ref": "#/components/schemas/ArticlePublished" }
}
}
},
"responses": { "200": { "description": "OK" } }
}
}
}
}
The body is described like a resource an action returns: the resource runs on a sampled model, author is optional
because it is loaded conditionally, and the schema is a component. See
Sampling.
{"data": …}, as
application/vnd.api+json. Send that document, not resolve(), which gives the resource object alone:
ArticlePublished::make($article)->response()->getData(true).What the class carries
| Need | Declaration | Default |
|---|---|---|
| key and operationId | #[Webhook('article.published')] | required |
| HTTP method | #[Webhook('article.published', method: 'PUT')] | POST |
| summary, description | the PHPDoc of the class; @deprecated | none |
| operationId, tags, deprecation | #[Operation] on the class | the name; tag Webhooks |
| tags with details | #[Tag] on the class (replaces the Webhooks tag) | none |
| headers sent | #[Header] on the class | none |
| what the receiver answers | #[Response] on the class | 200 OK |
Only the attributes of the class itself are read. An abstract class marked #[Webhook] is never a webhook, and a
class without a name is left out with a warning.
#[Webhook('article.archived', method: 'PUT')]
#[Operation(operationId: 'articleArchived', tags: ['Articles'])]
#[Response(204, description: 'Received')]
#[Response(410, description: 'The subscription is cancelled')]
final class ArticleArchived extends JsonResource
Where the classes are found
By default, every class under app/ is a candidate: only the PHP files that mention Webhook are read, so the scan
loads no other class. Narrow it, or list classes from a package or a module, in config/openapi.php:
'webhooks' => [
'paths' => [app_path('Webhooks')],
'classes' => [Billing\Webhooks\InvoiceIssued::class],
],
A file whose class cannot be loaded is skipped with a warning. A listed class that is not a concrete JsonResource
marked #[Webhook] fails the command, before any document is generated.
Check one webhook with php artisan openapi:inspect article.published.
Choose the webhooks of each document
Every document gets every webhook found. Leave a webhook out of all of them with #[Exclude] on its class:
#[Webhook('article.drafted')]
#[Exclude]
final class ArticleDrafted extends JsonResource
Choose the webhooks of one document by their name, with shell masks in its own webhooks. They apply to the
webhooks declared on routes too:
'documents' => [
'public' => [
'webhooks' => [
'include' => ['article.*'],
'exclude' => ['article.archived'],
],
],
'partners' => [
'webhooks' => ['include' => ['invoice.*']],
],
],
include keeps the names it matches (every name when empty), exclude leaves out those it matches, over include.
A document overrides the global webhooks key by key: 'webhooks' => ['paths' => []] gives it no webhook found by
scanning.
OpenAPI versions
webhooks exists from OpenAPI 3.1. In a 3.0 document:
- with
vendor_extensions: redoc, the webhooks are written asx-webhooks; - otherwise a strict document fails, and a lenient one (
strict: false) drops them with a warning.
Webhooks declared on routes
Before 1.3, a webhook was an action marked #[Webhook] behind a route registered only to be documented and kept
from answering by a middleware. It still works, and is deprecated: move the payload to a resource class. When a
payload class and a route webhook have the same name and method, the payload class is kept, with a warning.
Callbacks
A callback is a request sent to a URL the client gave, such as a callback_url field of its request. Declare it on
the operation that receives that URL, with a runtime expression:
use Ark4ne\OpenApi\Attributes\Callback;
final class ArticleController
{
/**
* Submit an article; `callback_url` is called once it is published.
*/
#[Callback(
'articlePublished',
url: '{$request.body#/callback_url}',
webhook: 'article.published',
)]
public function store(StoreArticleRequest $request): ArticleResource
{
// …
}
}
webhook reuses the request and the responses of a webhook, declared by a payload class or on a route.
Without it, describe the payload on the attribute:
#[Callback(
'articlePublished',
url: '{$request.body#/callback_url}',
type: 'array{id: int, status: string}',
description: 'Sent once the article is published',
status: 204,
)]
| Parameter | Gives |
|---|---|
name | the key of the callback in the operation |
url | the runtime expression of the URL |
method | the HTTP method, POST by default |
webhook | a webhook whose request and responses are reused |
resource, type | the payload, from a resource or a PHPDoc type |
mediaType | the media type of the payload |
description | the description of the request |
status | the response expected from the receiver, 200 by default |
A callback that reuses a missing webhook is left out with a warning.