Guide

GitLab webhooks: test project hooks locally and check the secret token

GitLab webhooks call a URL when something happens in a project: a push, a merge request, a pipeline or a comment. To build a handler you need a public URL that GitLab can reach and a way to see exactly what it sends. Here is how to set up a project hook, check the token header and fire test events.

All docs

Get a public URL for GitLab webhooks#

GitLab can't deliver to localhost. An OtterKit webhook endpoint gives you a public HTTPS URL that answers GitLab around the clock and keeps every request, so nothing is lost while your laptop is closed or your handler is half written:

Terminal
npx otterkit login
npx otterkit webhook --daemon --subdomain gitlab-dev
#   https://gitlab-dev.otterkit.app

The name is reserved to your account, so the URL stays the same every time you start it. An endpoint costs $0.01 per hour while it is live and never more than $3 in any 30 days, and new accounts get 10 free credits. To look at a payload before you sign up, the try page gives you a live URL in the browser with no account.

Register the URL in GitLab#

  1. In the project, open Settings, then Webhooks, and add a new webhook. Group webhooks are under the group's settings on paid tiers.
  2. Paste the endpoint URL.
  3. Type a long random string as the Secret token, tick the triggers you want, such as push events and merge request events, and save.

Use https://gitlab-dev.otterkit.app as the URL. Add a path if your handler expects one, such as https://gitlab-dev.otterkit.app/hooks/gitlab: the endpoint accepts any path and records it.

Check the secret token header#

GitLab does not sign the body. It sends the secret token as it is in the X-Gitlab-Token header, and your handler compares it with the value you saved. Use a constant-time comparison, and only ever use HTTPS URLs, since the token travels in the request.

The X-Gitlab-Event header names the event, for example Push Hook or Merge Request Hook. The same name is in the body as object_kind.

OtterKit can run the same check on every request as it arrives. Start the endpoint again with the secret, and each capture is marked valid, invalid or missing:

Terminal
npx otterkit stop gitlab-dev
npx otterkit webhook --daemon --subdomain gitlab-dev --verify gitlab:my-secret-token

A failed check comes with its reason, such as signature_mismatch, so a wrong secret and a changed body look different. See signature verification for all 16 presets and the generic HMAC mode.

Send a test event#

On the webhook's row, open the Test menu and pick an event type. GitLab sends a sample built from the project's real data, so the project needs at least one commit for a push test.

Then watch it land, or wait for it in a script:

Terminal
# Follow requests as they arrive, with headers and bodies
npx otterkit inspect gitlab-dev --follow

# Or block until the next POST, then print it as JSON
npx otterkit await gitlab-dev --method POST --count 1 --timeout 120s --json

Replay GitLab events into your local handler#

Once an event is captured you can send it to your handler as often as you like, with the original headers and body, without asking GitLab to fire it again:

Terminal
# Send the latest captured event to a server on port 3000
npx otterkit replay gitlab-dev --target 127.0.0.1:3000

# Or a specific one (1 is the oldest, -1 the latest)
npx otterkit replay gitlab-dev --index 3 --target 127.0.0.1:3000

Replays cost nothing and are added to the capture log with your handler's response next to them. If you would rather have events delivered live while you work, expose the handler with a tunnel instead and register the tunnel URL.

Retries, timeouts and duplicates#

  • GitLab expects a fast answer and treats a slow or non-2xx response as a failure. A webhook that keeps failing is disabled, first temporarily and then until you re-enable it.
  • Edit the webhook to see its recent events, with the request and response for each, and resend one from there.

To see how your code copes when a delivery fails, the HTTP status codes guide has URLs that return any status on demand.

Questions about GitLab webhooks#

Does GitLab sign webhook payloads?#

Not with an HMAC. The check is the shared secret token in X-Gitlab-Token.

Can GitLab send webhooks to localhost?#

GitLab.com cannot reach your machine, and self-managed instances block local network addresses by default. Give it a public URL and replay or tunnel from there.

Why was my GitLab webhook disabled?#

It failed several times in a row. Fix the endpoint, then send a test event from the webhook settings to turn it back on.

Other providers#