Sending webhooks | 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)  Sending webhooks

 Version   v1      

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

  Sending webhooks    
- [ 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)

 Sending webhooks
================

###  On this page 

1. [ Send a JSON payload ](#content-send-a-json-payload)
2. [ Send synchronously or conditionally ](#content-send-synchronously-or-conditionally)
3. [ Change the HTTP verb and headers ](#content-change-the-http-verb-and-headers)
4. [ Send a raw body ](#content-send-a-raw-body)
5. [ Delivery IDs ](#content-delivery-ids)
6. [ Timestamps ](#content-timestamps)
7. [ Metadata and Horizon tags ](#content-metadata-and-horizon-tags)

Send a JSON payload
-----------------------------------------------------------------------------------------------------------------

```
id, err := webhookserver.From(ctx).Call("https://other-app.com/webhooks").
    Payload(map[string]any{"key": "value"}).
    Secret("your-shared-secret").Dispatch(ctx)
```

Import `webhookserver "github.com/spatie/laravel-webhook-server-go"`. In a handler, pass `r` as the context. In a job, listener or command, pass its `ctx`. Always handle the returned error.

The payload is encoded once before dispatch using deterministic `encoding/json/v2`. The package signs and sends those exact bytes on every attempt. Invalid JSON payloads return an error at dispatch, including when signing is disabled. A custom serializer can replace body encoding independently of the JSON snapshot retained for listeners.

Send synchronously or conditionally
-----------------------------------------------------------------------------------------------------------------------------------------------------------------

Use `DispatchSync(ctx)` to make the request immediately instead of putting it on the queue. Synchronous calls do not wait or loop through retries.

`DispatchIf(ctx, condition)`, `DispatchUnless(ctx, condition)`, `DispatchSyncIf(ctx, condition)` and `DispatchSyncUnless(ctx, condition)` conditionally send a webhook. A skipped call is not validated or prepared. Queued variants return an empty job ID when skipped.

Change the HTTP verb and headers
--------------------------------------------------------------------------------------------------------------------------------------------------------

The default verb is `POST`. Override it with `Configure` and add headers with `Header`:

```
id, err := webhookserver.From(ctx).Call("https://other-app.com/webhooks").
    Payload(map[string]string{"key": "value"}).Secret("your-shared-secret").
    Configure(func(o *webhookserver.Options) { o.Method = "PUT" }).
    Header("X-Source", "billing").Dispatch(ctx)
```

Headers are case-insensitive. `Header` replaces a value while retaining other defaults. Assign `o.Headers` for multiple values or a complete replacement. Laravel HTTP global hooks and default headers apply. Explicit webhook headers override matching global headers. Middleware that changes signed bytes must update the signature.

For GET, give explicit `net/url.Values`. Repeated keys and nested bracket keys are supported without guessing how a Go struct should become a query:

```
err := webhookserver.From(ctx).Call("https://other-app.com/webhooks").
    Query(url.Values{"order_id": {"123"}}).
    Payload(map[string]int{"order_id": 123}).
    Secret("your-shared-secret").DispatchSync(ctx)
```

GET requests send query parameters and sign the JSON representation of `Payload`, not an empty body. `Query` sets the payload to its values by default, whose JSON values are string arrays. Call `Payload` afterwards when the receiver expects another representation. Raw GET calls are rejected. Existing URL query parameters are retained.

Send a raw body
-----------------------------------------------------------------------------------------------------

Use `RawBody` instead of `Payload` to send XML, pre-encoded JSON, or arbitrary binary bytes. Set a matching Content-Type:

```
err := webhookserver.From(ctx).Call("https://other-app.com/webhooks").
    RawBody([]byte("<root>someXMLContent</root>")).
    Header("Content-Type", "application/xml").
    Secret("your-shared-secret").DispatchSync(ctx)
```

Raw bodies are not JSON-encoded. Result events identify them with `Delivery.Raw`. `Delivery.Payload` contains the original raw bytes or the original JSON snapshot. `Delivery.Body` holds the signed bytes, including custom body encoding.

Delivery IDs
--------------------------------------------------------------------------------------------

Every request includes a `Webhook-Id` header containing a delivery UUID. It stays the same across retries. Retrieve it before dispatch with `call.ID()`, or set an application-specific ID with `WithID("your-delivery-id")`. `call.URL()` returns the destination.

Set `cfg.IDHeader` to rename the header globally, or to an empty string to disable it. Change `o.IDHeader` per call. An explicitly supplied header with that name takes precedence, regardless of casing.

The default signer authenticates the body only, not the ID or other headers. A delivery ID is not replay protection. Authenticate the request and make processing idempotent on the receiver. The queue job ID returned by `Dispatch` is separate from the webhook ID.

Timestamps
--------------------------------------------------------------------------------------

Set `o.UseTimestamp = true` to include a `Timestamp` header containing the Unix timestamp for the attempt. Rename it with `cfg.TimestampHeader`. Time comes from Laravel's application clock, including in workers and time-travel tests.

The default signer does not authenticate this header. For replay protection, put a timestamp in the signed payload and have the receiver validate its age.

Metadata and Horizon tags
-----------------------------------------------------------------------------------------------------------------------------------

Add local context with `Meta(map[string]any{"order_id": 123})`. Metadata is JSON-encoded and available to [event listeners](events), but is not sent to the receiver.

Set `cfg.Tags` or `o.Tags` to attach labels to jobs and events. Laravel Go has no PHP Horizon integration. These tags remain application data, not Horizon UI labels.

 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)
