Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
L

laravel-webhook-server

> 后端框架
Open source

Send webhooks from Laravel apps

1.1K stars0 likes1 views
WebsiteGitHub

About

Send webhooks from Laravel apps

A webhook is a way for an app to provide information to another app about a particular event. The way the two apps communicate is with a simple HTTP request.

This package allows you to configure and send webhooks in a Laravel app easily. It has support for signing calls, retrying calls and backoff strategies.

If you need to receive and process webhooks take a look at our laravel-webhook-client package.

Support us

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.

Installation

You can install the package via composer:

bash
composer require spatie/laravel-webhook-server

You can publish the config file with:

bash
php artisan vendor:publish --provider="Spatie\WebhookServer\WebhookServerServiceProvider"

This is the contents of the file that will be published at config/webhook-server.php:

…

By default, the package uses queues to retry failed webhook requests. Be sure to set up a real queue other than sync in non-local environments.

Usage

This is the simplest way to call a webhook:

php
WebhookCall::create()
   ->url('https://other-app.com/webhooks')
   ->payload(['key' => 'value'])
   ->useSecret('sign-using-this-secret')
   ->dispatch();

This will send a post request to https://other-app.com/webhooks. The body of the request will be JSON encoded version of the array passed to payload. The request will have a header called Signature that will contain a signature the receiving app can use to verify the payload hasn't been tampered with. Dispatching a webhook call will also fire a DispatchingWebhookCallEvent.

If the receiving app doesn't respond with a response code starting with 2, the package will retry calling the webhook after 10 seconds. If that second attempt fails, the package will attempt to call the webhook a final time after 100 seconds. Should that attempt fail, the FinalWebhookCallFailedEvent will be raised.

Send webhook synchronously

If you would like to call the webhook immediately (synchronously), you may use the dispatchSync method. When using this method, the webhook will not be queued and will be run immediately. This can be helpful in situations where sending the webhook is part of a bigger job that already has been queued.

php
WebhookCall::create()
   ...
   ->dispatchSync();

Conditionally sending webhooks

If you would like to conditionally dispatch a webhook, you may use the dispatchIf, dispatchUnless, dispatchSyncIf, and dispatchSyncUnless methods:

php
WebhookCall::create()
   ...
   ->dispatchIf($condition);

WebhookCall::create()
   ...
   ->dispatchUnless($condition);

WebhookCall::create()
   ...
   ->dispatchSyncIf($condition);

WebhookCall::create()
   ...
   ->dispatchSyncUnless($condition);

How signing requests work

When setting up, it's common to generate, store, and share a secret between your app and the app that wants to receive webhooks. Generating the secret could be done with Illuminate\Support\Str::random(), but it's entirely up to you. The package will use the secret to sign a webhook call.

By default, the package will add a header called Signature that will contain a signature the receiving app can use if the payload hasn't been tampered with. This is how that signature is calculated:

php
// payload is the array passed to the `payload` method of the webhook
// secret is the string given to the `signUsingSecret` method on the webhook.

$payloadJson = json_encode($payload); 

$signature = hash_hmac('sha256', $payloadJson, $secret);

Skip signing request

We don't recommend this, but if you don't want the webhook request to be signed call the doNotSign method.

php
WebhookCall::create()
   ->doNotSign()
    ...

By calling this method, the Signature header will not be set.

Customizing signing requests

If you want to customize the signing process, you can create your own custom signer. A signer is any class that implements Spatie\WebhookServer\Signer.

This is what that interface looks like.

php
namespace Spatie\WebhookServer\Signer;

interface Signer
{
    public function signatureHeaderName(): string;

    public function calculateSignature(array $payload, string $secret): string;
}

After creating your signer, you can specify its class name in the signer key of the webhook-server config file. Your signer will then be used by default in all webhook calls.

You can also specify a signer for a specific webhook call:

php
WebhookCall::create()
    ->signUsing(YourCustomSigner::class)
    ...
    ->dispatch();

If you want to customize the name of the header, you don't need to use a custom signer, but you can change the value in the signature_header_name in the webhook-server config file.

Use Timestamp

This is highly recommended to help prevent replay attacks.

Timestamping is disabled by default, but you can enable it by calling useTimestamp() method.

php
WebhookCall::create()
    ->useTimestamp()
    ...
    ->dispatch();

By calling this method, the Timestamp header will be set.

Retrying failed webhooks

When the app to which we're sending the webhook fails to send a response with a 2xx status code the package will consider the call as failed. The call will also be considered failed if the remote app doesn't respond within 3 seconds.

You can configure that default timeout in the timeout_in_seconds key of the webhook-server config file. Alternatively, you can override the timeout for a specific webhook like this:

php
WebhookCall::create()
    ->timeoutInSeconds(5)
    ...
    ->dispatch();

When a webhook call fails, we'll retry the call two more times. You can set the default amount of times we retry the webhook call in the tries key of the config file. Alternatively, you can specify the number of tries for a specific webhook like this:

php
WebhookCall::create()
    ->maximumTries(5)
    ...
    ->dispatch();

To not hammer the remote app we'll wait some time between each attempt. By default, we wait 10 seconds between the first and second attempts, 100 seconds between the third and the fourth, 1000 between the fourth and the fifth, and so on. The maximum amount of seconds that we'll wait is 100 000, which is about 27 hours. This behavior is implemented in the default ExponentialBackoffStrategy.

You can define your own backoff strategy by creating a class that implements Spatie\WebhookServer\BackoffStrategy\BackoffStrategy. This is what that interface looks like:

php
namespace Spatie\WebhookServer\BackoffStrategy;

interface BackoffStrategy
{
    public function waitInSecondsAfterAttempt(int $attempt): int;
}

You can make your custom strategy the default strategy by specifying its fully qualified class name in the backoff_strategy of the webhook-server config file. Alternatively, you can specify a strategy for a specific webhook like this.

php
WebhookCall::create()
    ->useBackoffStrategy(YourBackoffStrategy::class)
    ...
    ->dispatch();

Under the hood, the retrying of the webhook calls is implemented using delayed dispatching. Amazon SQS only has support for a small maximum delay. If you're using Amazon SQS for your queues, make sure you do not configure the package in a way so there are more than 15 minutes between each attempt.

Customizing the HTTP verb

By default, all webhooks will use the post method. You can customize that by specifying the HTTP verb you want in the http_verb key of the webhook-server config file.

You can also override the default for a specific call by using the useHttpVerb method.

php
WebhookCall::create()
    ->useHttpVerb('get')
    ...
    ->dispatch();

Adding extra headers

You can use extra headers by adding them to the headers key in the webhook-server config file. If you want to add additional headers for a specific webhook, you can use the withHeaders call.

php
WebhookCall::create()
    ->withHeaders([
        'Another Header' => 'Value of Another Header'
    ])
    ...
    ->dispatch();

Using a proxy

You can direct webhooks through a proxy by specifying the proxy key in the webhook-server config file. To set a proxy for a specific request, you can use the useProxy call.

php
WebhookCall::create()
    ->useProxy('http://proxy.server:3128')
    ...

Using mutual TLS authentication

To safeguard the integrity of webhook data transmission, it's critical to authenticate the intended recipient of your webhook payload. Mutual TLS authentication serves as a robust method for this purpose. Contrary to standard TLS, where only the client verifies the server, mutual TLS requires both the webhook endpoint (acting as the client) and the webhook provider (acting as the server) to authenticate each other. This is achieved through an exchange of certificates during the TLS handshake, ensuring that both parties confirm each other's identity.

Note: If you need to include your own certificate authority, pass the certificate path to the verifySsl() method.

php
WebhookCall::create()
    ->mutualTls(
        certPath: storage_path('path/to/cert.pem'), 
        certPassphrase: 'optional_cert_passphrase', 
        sslKeyPath: storage_path('path/to/key.pem'), 
        sslKeyPassphrase: 'optional_key_passphrase'
    )

The proxy specification follows the guzzlehttp proxy format

Verifying the SSL certificate of the receiving app

When using a URL that starts with https:// the package will verify if the SSL certificate of the receiving party is valid. If it is not, we will consider the webhook call failed. We don't recommend this, but you can turn off this verification by setting the verify_ssl key in the webhook-server config file to false.

You can also disable the verification per webhook call with the doNotVerifySsl method.

php
WebhookCall::create()
    ->doNotVerifySsl()
    ...
    ->dispatch();

Adding meta information

You can add extra meta information to the webhook. This meta information will not be transmitted, and it will only be used to pass to the events this package fires.

This is how you can add meta information:

php
WebhookCall::create()
    ->meta($arrayWithMetaInformation)
    ...
    ->dispatch();

Adding tags

If you're using Laravel Horizon for your queues, you'll be happy to know that we support tags.

To add tags to the underlying job that'll perform the webhook call, simply specify them in the tags key of the webhook-server config file or use the withTags method:

php
WebhookCall::create()
    ->withTags($tags)
    ...
    ->dispatch();

Exception handling

By default, the package will not log any exceptions that are thrown when sending a webhook.

To handle exceptions you need to create listeners for the Spatie\WebhookServer\Events\WebhookCallFailedEvent and/or `Spatie\WebhookServer\Events\FinalWebhookCallFailed

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

PHPhttplaravelwebhooks

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category后端框架
PricingOpen source

> Related tools

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架