Installation and setup | laravel-webhook-client-go | Spatie       

 SPATIE  

laravel-webhook-client-go
=========================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-webhook-client-go](https://spatie.be/docs/laravel-webhook-client-go/v1)  Installation and setup

 Version   v1      

 Other versions for crawler [v1](https://spatie.be/docs/laravel-webhook-client-go/v1) 

  Installation and setup    
- [ Introduction ](https://spatie.be/docs/laravel-webhook-client-go/v1/introduction)
- [ Installation and setup ](https://spatie.be/docs/laravel-webhook-client-go/v1/installation-and-setup)
- [ Usage ](https://spatie.be/docs/laravel-webhook-client-go/v1/usage)
- [ Customizing the package ](https://spatie.be/docs/laravel-webhook-client-go/v1/customizing-the-package)
- [ Storing attachments ](https://spatie.be/docs/laravel-webhook-client-go/v1/attachments)
- [ Testing ](https://spatie.be/docs/laravel-webhook-client-go/v1/testing)
- [ Releasing ](https://spatie.be/docs/laravel-webhook-client-go/v1/releasing)
- [ Changelog ](https://spatie.be/docs/laravel-webhook-client-go/v1/changelog)
- [ Support us ](https://spatie.be/docs/laravel-webhook-client-go/v1/support-us)

 Installation and setup
======================

###  On this page 

1. [ Configuring the package ](#content-configuring-the-package)
2. [ Preparing the database ](#content-preparing-the-database)
3. [ Taking care of routing ](#content-taking-care-of-routing)

Requires Go 1.27 or newer and `go.laravel.com v0.1.0`. You can install the package with:

```
go get github.com/spatie/laravel-webhook-client-go@v0.1.0
```

Configuring the package
-----------------------------------------------------------------------------------------------------------------------------

Go packages are configured in code. There is no config publishing command. Start with `NewWebhookConfig` to keep the default validator, profile, response and attachment settings:

```
import (
    "context"

    webhookclient "github.com/spatie/laravel-webhook-client-go"
    laravel "go.laravel.com"
)

type ProcessWebhook struct {
    Call webhookclient.WebhookCall `json:"call" queue:"model"`
}

func (j ProcessWebhook) Handle(ctx context.Context) error {
    // Process j.Call.Payload.V here.
    return nil
}

func Webhooks() laravel.Package {
    processor := webhookclient.NewProcessor(
        webhookclient.NewWebhookConfig("default", ""),
        webhookclient.DatabaseWebhookCallModel{},
        webhookclient.NewQueuedJob(func(call webhookclient.WebhookCall) ProcessWebhook {
            return ProcessWebhook{Call: call}
        }),
    )

    return webhookclient.New(func(cfg *webhookclient.Config) {
        cfg.Endpoints = []webhookclient.Endpoint{
            processor.Endpoint("/webhook-receiving-url"),
        }
    })
}
```

Add `Webhooks()` to `laravel.Options.Packages` in `bootstrap/app.go`. Installing the module with `go get` does not activate it. The package registers the actual `ProcessWebhook` job type, including when you replace it with your own type.

Set `WEBHOOK_CLIENT_SECRET` to a valid webhook secret provided by the sending app. An empty endpoint secret uses this package default. Set a nonempty endpoint secret when receiving calls from multiple apps. `WEBHOOK_CLIENT_DELETE_AFTER_DAYS` defaults to 30. Invalid typed environment settings stop startup. The callbacks passed to `New` run after environment defaults, so `cfg.SigningSecret` and `cfg.DeleteAfterDays` can override them in code.

The package tries to store and respond to the webhook as fast as possible. Processing the payload is done via a queued job. Use a persistent queue driver in production instead of the `sync` driver. Your job controls Laravel's retry, timeout, middleware and queue options. `processor.Job.Connection` and `processor.Job.Queue` override the job's destination when set.

Preparing the database
--------------------------------------------------------------------------------------------------------------------------

By default, accepted webhook calls are saved in the database. The package registers a migration for the `webhook_calls` table, including payload, selected headers, attachments and exception data. Register your application's database driver explicitly through `laravel.Options.Drivers`. The package does not choose a database driver for consumers.

Run the migrations:

```
laravel migrate
```

A database-backed queue needs its own migration with `queue.CreateTables`. That is the application's responsibility. With a custom model, set `cfg.WithoutMigration = true` and register the custom model's migrations instead. The exported `Migration` value can also be registered directly when composing a package.

Taking care of routing
--------------------------------------------------------------------------------------------------------------------------

At the app that sends webhooks, configure the receiving URL. `processor.Endpoint("/webhook-receiving-url")` creates a POST route in Laravel's bare route group. It has no API prefix, browser session or CSRF middleware. Global middleware still applies, including request limits.

You can register several endpoints or several routes using the same processor. Route names do not select configuration in Go. Each route closes over its processor, so PHP's random route-name token setting is not needed.

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
