Receive a webhook
Copy page
In this tutorial you post a webhook to a QueueBox inbox source, post it a second time and see QueueBox reject the duplicate. Then you consume the stored message with a pull client in Go, TypeScript or C#. The client writes an order row and completes the message in one transaction.
Before you start
Section titled “Before you start”You need Docker with the Compose plugin, and curl. There is no checkout and no build. You also need one of these toolchains:
- Go 1.24 or later.
- Node 22.18 or later, which runs a TypeScript file directly.
- The .NET 8 SDK or later.
The stack uses the ports 8080 and 5432 on your machine. If you did the first tutorial, run docker compose -f docker-compose.image.yml down -v in its directory first to start from an empty database.
Get the stack
Section titled “Get the stack”Run every command of this tutorial in one empty directory. These two downloads give you the stack file and the configuration that it mounts.
curl -O https://raw.githubusercontent.com/alternayte/queuebox/main/docker-compose.image.ymlcurl -O https://raw.githubusercontent.com/alternayte/queuebox/main/examples/queuebox.ymldocker-compose.image.yml runs QueueBox 0.9.1, the image that the project publishes to ghcr.io/alternayte/queuebox. It starts PostgreSQL beside it and mounts the queuebox.yml that sits next to it.
Add a pull source
Section titled “Add a pull source”An inbox source is one configured ingress. This tutorial adds an HTTP source named orders. Its messages stay in the inbox until your worker takes them, because the source sets consumption: pull.
sources is the last block of the queuebox.yml that you downloaded. Append the new source to it:
cat >> queuebox.yml <<'YAML'
orders: type: http path: /orders idempotencyKeyPath: $.id consumption: pullYAMLThe source now reads:
sources: orders: type: http path: /orders idempotencyKeyPath: $.id consumption: pullpath: /ordersputs the source atPOST /inbox/orders, becauseinbox.basePathis/inbox.idempotencyKeyPath: $.idreads the idempotency key from theidfield of the body.consumption: pulltells QueueBox not to forward the message. A pull source needs no topic, no route and no destination.
Start the stack:
docker compose -f docker-compose.image.yml up -dDocker pulls the images on the first start. Ask QueueBox whether it is ready:
curl http://localhost:8080/healthA ready instance answers with status 200 and a body that starts with {"status":"healthy". If curl cannot connect, wait a moment and send the request again.
Post the webhook
Section titled “Post the webhook”Send an order to the source:
curl -i -X POST http://localhost:8080/inbox/orders \ -H 'Content-Type: application/json' \ -d '{"id":"order-1","total":42}'QueueBox stores the message and answers 202 Accepted. The body holds the identifier of the new inbox row:
HTTP/1.1 202 Accepted...{"messageId":"8d5c3f0e-2f7b-4c0e-9d51-6f0c1b3a7e21"}Your identifier differs.
Post it again
Section titled “Post it again”A webhook sender retries when it does not see an answer, so the same message can arrive twice. Send the same request again:
curl -i -X POST http://localhost:8080/inbox/orders \ -H 'Content-Type: application/json' \ -d '{"id":"order-1","total":42}'QueueBox finds a stored row with the same source and the same idempotency key. It stores nothing and answers 200 OK:
HTTP/1.1 200 OK...{"status":"duplicate"}Look at the stored row
Section titled “Look at the stored row”Read the inbox table:
docker compose -f docker-compose.image.yml exec postgres psql -U queuebox -d queuebox \ -c "SELECT source, idempotency_key, consumption, state FROM inbox;"The inbox holds one row, in state pending:
source | idempotency_key | consumption | state--------+-----------------+-------------+--------- orders | order-1 | pull | pending(1 row)Create the business table
Section titled “Create the business table”The worker writes each order into a table that your application owns. Create it in the same database:
docker compose -f docker-compose.image.yml exec postgres psql -U queuebox -d queuebox \ -c "CREATE TABLE IF NOT EXISTS orders (id TEXT PRIMARY KEY, total INTEGER NOT NULL);"Write the worker
Section titled “Write the worker”The pull client claims the messages of one source. It gives each message to your handler with an open transaction. When the handler returns, the client marks the message processed in that same transaction and commits. The order row and the completion commit together, or neither commits.
Choose a language, create the project and write the worker.
mkdir orders-worker && cd orders-workergo mod init example.com/orders-workerWrite main.go:
package main
import ( "context" "database/sql" "log" "os" "os/signal" "syscall"
_ "github.com/jackc/pgx/v5/stdlib"
queuebox "github.com/alternayte/queuebox/clients/go")
func main() { db, err := sql.Open("pgx", "postgres://queuebox:queuebox@localhost:5432/queuebox?sslmode=disable") if err != nil { log.Fatal(err) } defer db.Close()
worker, err := queuebox.NewInboxWorker(db, queuebox.Options{Source: "orders"}) if err != nil { log.Fatal(err) }
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) defer stop()
log.Println("Waiting for messages of the source 'orders'. Press Ctrl+C to stop.")
err = worker.Run(ctx, func(ctx context.Context, message queuebox.Message, tx *sql.Tx) error { var order struct { ID string `json:"id"` Total int `json:"total"` } if err := message.UnmarshalPayload(&order); err != nil { return err }
if _, err := tx.ExecContext(ctx, "INSERT INTO orders (id, total) VALUES ($1, $2)", order.ID, order.Total); err != nil { return err }
log.Printf("Stored the order %s.", message.IdempotencyKey) return nil }) if err != nil { log.Fatal(err) }}Fetch the dependencies and run it:
go mod tidygo run .mkdir orders-worker && cd orders-workernpm init -ynpm pkg set type=modulenpm install @alternayte/queuebox-inbox pgWrite worker.ts:
import pg from "pg";import { InboxWorker, fromPg } from "@alternayte/queuebox-inbox";
const stopping = new AbortController();process.on("SIGINT", () => stopping.abort());process.on("SIGTERM", () => stopping.abort());
const pool = new pg.Pool({ connectionString: "postgres://queuebox:queuebox@localhost:5432/queuebox" });
const worker = new InboxWorker(fromPg(pool), { source: "orders" });
console.log("Waiting for messages of the source 'orders'. Press Ctrl+C to stop.");
await worker.run(async (message, tx) => { const order = message.payload as { id: string; total: number };
await tx.query("INSERT INTO orders (id, total) VALUES ($1, $2)", [order.id, order.total]);
console.log(`Stored the order ${message.idempotencyKey}.`);}, stopping.signal);
await pool.end();Run it:
node worker.tsdotnet new console -o OrdersWorker && cd OrdersWorkerdotnet add package QueueBox.Inboxdotnet add package NpgsqlReplace Program.cs:
using Npgsql;using QueueBox.Inbox;
using var stopping = new CancellationTokenSource();Console.CancelKeyPress += (_, eventArgs) =>{ eventArgs.Cancel = true; stopping.Cancel();};
await using var dataSource = NpgsqlDataSource.Create( "Host=localhost;Database=queuebox;Username=queuebox;Password=queuebox");
var worker = new InboxWorker( InboxConnections.From(dataSource), new InboxOptions { Source = "orders" });
Console.WriteLine("Waiting for messages of the source 'orders'. Press Ctrl+C to stop.");
await worker.RunAsync(async (message, transaction, cancellationToken) =>{ await using var command = transaction.CreateCommand();
command.CommandText = "INSERT INTO orders (id, total) VALUES (@id, @total)"; command .WithParameter("@id", message.Payload.GetProperty("id").GetString()) .WithParameter("@total", message.Payload.GetProperty("total").GetInt32());
await command.ExecuteNonQueryAsync(cancellationToken);
Console.WriteLine($"Stored the order {message.IdempotencyKey}.");}, stopping.Token);Run it:
dotnet runThe worker claims the stored message at once and prints:
Stored the order order-1.Leave the worker running.
Check the result
Section titled “Check the result”Open a second terminal in the directory that holds the stack file. Read the order table and the inbox row:
docker compose -f docker-compose.image.yml exec postgres psql -U queuebox -d queuebox \ -c "SELECT id, total FROM orders;" \ -c "SELECT idempotency_key, state FROM inbox WHERE source = 'orders';"The order row exists, and the inbox row is in state processed:
id | total---------+------- order-1 | 42(1 row)
idempotency_key | state-----------------+----------- order-1 | processed(1 row)Post a new order while the worker runs, and watch the worker print it:
curl -X POST http://localhost:8080/inbox/orders \ -H 'Content-Type: application/json' \ -d '{"id":"order-2","total":17}'Clean up
Section titled “Clean up”Press Ctrl+C in the worker terminal. The worker stops claiming and lets a running handler finish. Then stop the stack, delete its volume, and restore the configuration file:
docker compose -f docker-compose.image.yml down -vcurl -O https://raw.githubusercontent.com/alternayte/queuebox/main/examples/queuebox.ymlThe second command overwrites queuebox.yml, so the appended orders source is gone.
What you did
Section titled “What you did”You posted a webhook to an inbox source, and QueueBox stored it once although it arrived twice. A pull worker then took the message and wrote an order row. The completion committed in the same transaction as the order row.
Receive GitHub webhooks
Section titled “Receive GitHub webhooks”The orders source reads its idempotency key from the body. A GitHub webhook has no body field that is unique for every delivery. GitHub puts the delivery ID in the X-GitHub-Delivery header, and the event name in the X-GitHub-Event header. GitHub also sends the same delivery again after a timeout or a manual redelivery.
An HTTP source reads a key from a header when you set the header key:
sources: github: type: http path: /github idempotencyKeyHeader: X-GitHub-Delivery eventTypeHeader: X-GitHub-Event aggregateIdPath: $.repository.full_name consumption: pull auth: type: hmac secret: ${GITHUB_WEBHOOK_SECRET} headerName: X-Hub-Signature-256 signaturePrefix: "sha256="idempotencyKeyHeader: X-GitHub-Deliverymakes the delivery ID the idempotency key. A redelivery of the same delivery is a duplicate. Two deliveries with the same body are two rows.eventTypeHeader: X-GitHub-Eventstores the event name, for examplepull_request, as the event type.aggregateIdPath: $.repository.full_namekeeps the events of one repository in order.- The
hmacauth checks theX-Hub-Signature-256header against the secret of the GitHub webhook. A request with a wrong signature gets401.
A header name matches in any letter case. When a source also sets idempotencyKeyPath, the header comes first and the path is the fallback. A request without the key gets 400.
Do not name the signature header as a key header. QueueBox never stores the auth.headerName header, so that configuration stops the start.
To send a delivery by hand, sign the body with the same secret:
body='{"action":"opened","repository":{"full_name":"acme/api"}}'signature="sha256=$(printf '%s' "$body" | openssl dgst -sha256 -hmac "$GITHUB_WEBHOOK_SECRET" | sed 's/^.* //')"curl -i -X POST http://localhost:8080/inbox/github \ -H "X-Hub-Signature-256: $signature" \ -H 'X-GitHub-Delivery: 72d3162e-cc78-11e3-81ab-4c9367dc0958' \ -H 'X-GitHub-Event: pull_request' \ -H 'Content-Type: application/json' \ -d "$body"The first request gets 202 Accepted. The same request again gets 200 OK with {"status":"duplicate"}.
Next steps
Section titled “Next steps”- Consume the inbox compares the push relay with pull workers, and covers retries and idempotent handlers.
- Authenticate requests protects an inbox source with a token, an API key or an HMAC signature.
- Pull clients lists every setting of the three libraries.