|
| 1 | +--- |
| 2 | +sidebar_position: 11 |
| 3 | +--- |
| 4 | + |
| 5 | +# Webhooks |
| 6 | + |
| 7 | +Laravel Workflow provides webhooks that allow external systems to start workflows and send signals dynamically. This feature enables seamless integration with external services, APIs, and automation tools. |
| 8 | + |
| 9 | +## Enabling Webhooks |
| 10 | +To enable webhooks in Laravel Workflow, register the webhook routes in your application’s routes file (`routes/web.php` or `routes/api.php`): |
| 11 | + |
| 12 | +```php |
| 13 | +use Workflow\Webhooks; |
| 14 | + |
| 15 | +Webhooks::routes(); |
| 16 | +``` |
| 17 | + |
| 18 | +By default, webhooks will: |
| 19 | +- Auto-discover workflows in the `app/Workflows` folder. |
| 20 | +- Expose webhooks to workflows marked with `#[Webhook]` at the class level. |
| 21 | +- Expose webhooks to signal methods marked with `#[Webhook]`. |
| 22 | + |
| 23 | +## Starting a Workflow via Webhook |
| 24 | +To expose a workflow as a webhook, add the `#[Webhook]` attribute on the class itself. |
| 25 | + |
| 26 | +```php |
| 27 | +use Workflow\Webhook; |
| 28 | +use Workflow\Workflow; |
| 29 | + |
| 30 | +#[Webhook] |
| 31 | +class OrderWorkflow extends Workflow |
| 32 | +{ |
| 33 | + public function execute($orderId) |
| 34 | + { |
| 35 | + // your code here |
| 36 | + } |
| 37 | +} |
| 38 | +``` |
| 39 | + |
| 40 | +### Webhook URL |
| 41 | +``` |
| 42 | +POST /webhooks/start/order-workflow |
| 43 | +``` |
| 44 | + |
| 45 | +### Example Request |
| 46 | +```bash |
| 47 | +curl -X POST "https://example.com/webhooks/start/order-workflow" \ |
| 48 | + -H "Content-Type: application/json" \ |
| 49 | + -d '{"orderId": 123}' |
| 50 | +``` |
| 51 | + |
| 52 | +## Sending a Signal via Webhook |
| 53 | +To allow external systems to send signals to a workflow, add the `#[Webhook]` attribute on the method. |
| 54 | + |
| 55 | +```php |
| 56 | +use Workflow\SignalMethod; |
| 57 | +use Workflow\Webhook; |
| 58 | +use Workflow\Workflow; |
| 59 | + |
| 60 | +class OrderWorkflow extends Workflow |
| 61 | +{ |
| 62 | + protected bool $shipped = false; |
| 63 | + |
| 64 | + #[SignalMethod] |
| 65 | + #[Webhook] |
| 66 | + public function markAsShipped() |
| 67 | + { |
| 68 | + $this->shipped = true; |
| 69 | + } |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +### Webhook URL |
| 74 | +``` |
| 75 | +POST /webhooks/signal/order-workflow/{workflowId}/mark-as-shipped |
| 76 | +``` |
| 77 | + |
| 78 | +### Example Request |
| 79 | +```bash |
| 80 | +curl -X POST "https://example.com/webhooks/signal/order-workflow/1/mark-as-shipped" \ |
| 81 | + -H "Content-Type: application/json" |
| 82 | +``` |
| 83 | + |
| 84 | +## Webhook Authentication |
| 85 | +By default, webhooks don't require authentication but this can be configured in `config/workflows.php`. |
| 86 | + |
| 87 | +### Authentication Methods |
| 88 | +Laravel Workflow supports: |
| 89 | +1. No Authentication (none) |
| 90 | +2. Token-based Authentication (token) |
| 91 | +3. HMAC Signature Verification (signature) |
| 92 | + |
| 93 | +### Token Authentication |
| 94 | +For token authentication, webhooks require a valid API token in the request headers. The default header is `Authorization` but you can change this in the configuration settings. |
| 95 | + |
| 96 | +#### Example Request |
| 97 | +```bash |
| 98 | +curl -X POST "https://example.com/webhooks/start/order-workflow" \ |
| 99 | + -H "Content-Type: application/json" \ |
| 100 | + -H "Authorization: your-api-token" \ |
| 101 | + -d '{"orderId": 123}' |
| 102 | +``` |
| 103 | + |
| 104 | +### HMAC Signature Authentication |
| 105 | +For HMAC authentication, Laravel Workflow verifies requests using a secret key. The default header is `X-Signature` but this can also be changed. |
| 106 | + |
| 107 | +#### Example Request |
| 108 | +```bash |
| 109 | +BODY='{"orderId": 123}' |
| 110 | +SIGNATURE=$(echo -n "$BODY" | openssl dgst -sha256 -hmac "your-secret-key" | awk '{print $2}') |
| 111 | + |
| 112 | +curl -X POST "https://example.com/webhooks/start/order-workflow" \ |
| 113 | + -H "Content-Type: application/json" \ |
| 114 | + -H "X-Signature: $SIGNATURE" \ |
| 115 | + -d "$BODY" |
| 116 | +``` |
| 117 | + |
| 118 | +## Configuring Webhook Routes |
| 119 | +By default, webhooks are accessible under `/webhooks`. You can customize the route path in `config/workflows.php`: |
| 120 | + |
| 121 | +```php |
| 122 | +'webhooks_route' => 'api/webhooks', |
| 123 | +``` |
| 124 | + |
| 125 | +After this change, webhooks will be accessible under: |
| 126 | +``` |
| 127 | +POST /api/webhooks/start/order-workflow |
| 128 | +POST /api/webhooks/signal/order-workflow/{workflowId}/mark-as-shipped |
| 129 | +``` |
0 commit comments