Create a webhook with Kong Gateway

Uses: Kong Gateway
TL;DR

The webhook handler can be configured with a URL. When configured, the Event Hook will listen for the event and push information to the configured URL.

Prerequisites

This tutorial requires Kong Gateway Enterprise. If you don’t have Kong Gateway set up yet, you can use the quickstart script with an enterprise license to get an instance of Kong Gateway running almost instantly.

  1. Export your license to an environment variable:

     export KONG_LICENSE_DATA='LICENSE-CONTENTS-GO-HERE'
    
  2. Run the quickstart script:

     curl -Ls https://get.konghq.com/quickstart | bash -s -- -e KONG_LICENSE_DATA 
    

    Once Kong Gateway is ready, you will see the following message:

     Kong Gateway Ready
    
  • You can generate a URL by navigating to https://webhook.site and copying the free URL.
  • Set that URL as an environment variable export WEBHOOK_URL=YOUR_URL.

cURL is used to send requests to Kong Gateway. curl is pre-installed on most systems.

Before you can use Event Hooks for the first time, Kong Gateway needs to be reloaded. To do this, run kong-reload. You can verify that this worked by issuing a GET request to the /event-hooks/ endpoint.

Create a webhook

The webhook handler is used to configure webhooks that make a POST request to the URL provided during configuration. The Event Hook will push information to this URL with the event data. In this guide you will configure an Event Hook that will issue a POST request every time an event type consumers has a CRUD event.

Using the Admin API, create an Event Hook on the Consumers event by issuing a POST request to the /event-hooks endpoint.

curl -i -X POST http://localhost:8001/event-hooks/ \
    --header "Accept: application/json" \
    --header "Content-Type: application/json" \
    --data '
    {
      "source": "crud",
      "event": "consumers",
      "handler": "webhook",
      "on_change": true,
      "config": {
        "url": "'$WEBHOOK_URL'"
      }
    }
    '

Issuing this POST request will send a request of type ping to the webhook URL verifying that the webhook is configured correctly.

Validate the webhook

Important: Before you can use Event Hooks for the first time, Kong Gateway needs to be reloaded.

Using the Admin API create a new Consumer:

curl -i -X POST http://localhost:8001/consumers/ \
    --header "Accept: application/json" \
    --header "Content-Type: application/json" \
    --data '
    {
      "username": "my-consumer"
    }
    '

Verify on https://webhook.site that you received a POST request. It will look like this:

{
  "entity": {
    "username_lower": "my-consumer",
    "id": "ea87c99f-36f1-41c9-8543-7b13ee2b5dfe",
    "updated_at": 1734547295,
    "type": 0,
    "username": "my-consumer",
    "created_at": 1734547295
  },
  "schema": "consumers",
  "source": "crud",
  "event": "consumers",
  "operation": "create"
}

This response body contains the operation, the source, and the event, confirming that a new Consumer was created.

Cleanup

curl -Ls https://get.konghq.com/quickstart | bash -s -- -d
Something wrong?

Help us make these docs great!

Kong Developer docs are open source. If you find these useful and want to make them better, contribute today!