While it's not necessary for every Saleor app to receive domain events from Saleor it is possible, as described in Saleor's docs.
To configure your app to listen to HTTP webhooks issued from Saleor you need to register your handlers similarly as you would register your FastAPI endpoints.
The framework ensures that the webhook comes from a trusted source but to achieve that it needs to be provided with a way of retrieving the webhook_secret your app stored when the save_app_data was invoked (upon app installation). To do that you need to provide the SaleorApp with an async function doing just that.
The framework provides a special webhook router that allows you to use many different endpoints under the /webhook route. That router needs to be enabled with the get_webhook_details function:
An HTTP webhook handler is a function that is exactly like one that one would use as a FastAPI endpoint. The difference is that we register those with a special router.
The difference between subscriptions and the basic approach for webhook handlers is that we add an optional argument for the subscription query, and also a more general payload parameter type in the endpoint. This is because the structure of the subscription payload sent by Saelor is different in this case.
fromsaleor_app.appimportSaleorAppfromsaleor_app.depsimportsaleor_domain_header# (1)fromsaleor_app.schemas.handlersimportSaleorEventTypefromsaleor_app.schemas.webhookimportWebhookfromsaleor_app.schemas.coreimportDomainName,WebhookDataasyncdefget_webhook_details(saleor_domain:DomainName)->WebhookData:returnWebhookData(webhook_id="webhook-id",webhook_secret_key="webhook-secret-key",)app=SaleorApp(#[...])app.include_webhook_router(get_webhook_details=get_webhook_details)SUBSCRIPTION_ORDER_CREATED="subscription { event { ... on DraftOrderCreated { order { id status created } } } }"@app.webhook_router.http_event_route(SaleorEventType.ORDER_CREATED,subscription_query=SUBSCRIPTION_ORDER_CREATED)asyncdeforder_created(payload:Request,saleor_domain=Depends(saleor_domain_header)# (2)):awaitdo_something(payload,saleor_domain)