Signing requests | laravel-webhook-server-go | Spatie       

 SPATIE  

laravel-webhook-server-go
=========================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-webhook-server-go](https://spatie.be/docs/laravel-webhook-server-go/v1)  Signing requests

 Version   v1      

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

  Signing requests    
- [ Introduction ](https://spatie.be/docs/laravel-webhook-server-go/v1/introduction)
- [ Installation and setup ](https://spatie.be/docs/laravel-webhook-server-go/v1/installation-and-setup)
- [ Sending webhooks ](https://spatie.be/docs/laravel-webhook-server-go/v1/sending-webhooks)
- [ Signing requests ](https://spatie.be/docs/laravel-webhook-server-go/v1/signing-requests)
- [ Retrying failed webhooks ](https://spatie.be/docs/laravel-webhook-server-go/v1/retrying-failed-webhooks)
- [ Transport options ](https://spatie.be/docs/laravel-webhook-server-go/v1/transport-options)
- [ Events ](https://spatie.be/docs/laravel-webhook-server-go/v1/events)
- [ Testing webhooks ](https://spatie.be/docs/laravel-webhook-server-go/v1/testing-webhooks)
- [ Differences from PHP ](https://spatie.be/docs/laravel-webhook-server-go/v1/upgrading)
- [ Testing ](https://spatie.be/docs/laravel-webhook-server-go/v1/testing)
- [ Releasing ](https://spatie.be/docs/laravel-webhook-server-go/v1/releasing)
- [ Changelog ](https://spatie.be/docs/laravel-webhook-server-go/v1/changelog)
- [ Support us ](https://spatie.be/docs/laravel-webhook-server-go/v1/support-us)

 Signing requests
================

###  On this page 

1. [ Change or disable the signature header ](#content-change-or-disable-the-signature-header)
2. [ Custom signers ](#content-custom-signers)
3. [ Customize body serialization ](#content-customize-body-serialization)

Share a secret between your application and the receiving application. Pass it to `Secret` when creating a webhook. The package adds a `Signature` header calculated over the prepared body using hexadecimal HMAC-SHA256.

JSON requests are encoded once before dispatch. Raw bodies are not JSON-encoded. Every retry sends the same bytes and signature. The secret is consumed during preparation and is not stored on the queue job.

The receiver should calculate the expected signature over the raw received body and compare decoded signatures with `crypto/hmac.Equal`. The default signer does not authenticate the URL, delivery ID, timestamp header, or other headers.

GET webhooks are an exception to body signing. The payload is sent as explicit query parameters and its JSON representation is signed. Agree on the signed representation with the receiver.

Change or disable the signature header
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Use `cfg.Signer = webhookserver.DefaultSigner{Header: "X-Signature"}` to rename the header, or set `WEBHOOK_SERVER_SIGNATURE_HEADER`. Call `DoNotSign` to send an unsigned webhook. No secret is required and the package does not add a signature header.

Custom signers
--------------------------------------------------------------------------------------------------

Implement `webhookserver.Signer`:

```
type CustomSigner struct{}

func (CustomSigner) HeaderName() string { return "X-Custom-Signature" }

func (CustomSigner) Sign(destination string, body []byte, secret string) (string, error) {
    mac := hmac.New(sha256.New, []byte(secret))
    mac.Write([]byte(destination + "\n"))
    mac.Write(body)
    return hex.EncodeToString(mac.Sum(nil)), nil
}
```

Set `cfg.Signer = CustomSigner{}` globally or select it per call with `SignUsing(CustomSigner{})`. The body is already serialized. Do not JSON-encode it again.

Customize body serialization
--------------------------------------------------------------------------------------------------------------------------------------------

Set `Config.Serialize` instead of overriding the PHP job's `generateBody` method:

```
webhookserver.New(func(cfg *webhookserver.Config) {
    cfg.Serialize = func(payload any) ([]byte, error) {
        return json.MarshalIndent(payload, "", "  ")
    }
})
```

This example uses the standard `encoding/json` package. A nil serializer uses the default deterministic JSON snapshot. Raw bodies bypass the serializer.

The package prepares the body before dispatch, signs the result, and sends it unchanged across attempts. The serializer must not depend on worker-only state or the current attempt. HTTP middleware that later changes the body must also update its signature. Go JSON escaping and map order need not match PHP byte for byte. Always verify received bytes rather than re-encoding JSON.

 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)
