Webhooks and callbacks
Both describe requests sent by your API, with path items and operations like the rest of the document.
Webhooks
Webhooks are requests the API sends on its own, outside any call. They exist from OpenAPI 3.1; the objectId of each path item is the webhook name.
use Ark4ne\OpenApiWriter\Objects\MediaType;
use Ark4ne\OpenApiWriter\Objects\Operation;
use Ark4ne\OpenApiWriter\Objects\PathItem;
use Ark4ne\OpenApiWriter\Objects\RequestBody;
use Ark4ne\OpenApiWriter\Objects\Response;
use Ark4ne\OpenApiWriter\Objects\Schema;
$event = RequestBody::create()
->required()
->content(MediaType::json()->schema(Schema::ref('Book')));
$document = $document->webhooks(
PathItem::create('bookPublished')->operations(
Operation::post()
->operationId('webhooks.bookPublished')
->requestBody($event)
->responses(Response::noContent()),
),
);
Written for OpenAPI 3.0, webhooks throw an UnsupportedFeatureException (or are dropped in lenient mode). Redoc reads
them from an x-webhooks extension, which the library can write for you; see
Redoc extensions.
Callbacks
Callbacks are requests sent in reaction to a call, to a URL that call provides. The objectId of the callback is its name; the objectId of each path item is a runtime expression.
use Ark4ne\OpenApiWriter\Objects\Callback;
$subscribe = Operation::post()
->operationId('subscriptions.store')
->requestBody(
RequestBody::create()->required()->content(
MediaType::json()->schema(
Schema::object()
->properties(
Schema::string('callbackUrl')->format('uri'),
)
->required('callbackUrl'),
),
),
)
->callbacks(
Callback::create('bookPublished')->pathItems(
PathItem::create('{$request.body#/callbackUrl}')->operations(
Operation::post()
->requestBody($event)
->responses(Response::noContent()),
),
),
)
->responses(Response::created());
Callbacks exist in every version. Reusable ones go in Components::callbacks(), referred to with
Callback::ref('BookPublished', 'bookPublished').