Event Hooks Reference
Event hooks are outbound calls from Kong Gateway. With event hooks, the Kong Gateway can communicate with target services or resources, letting the target know that an event was triggered. When an event is triggered in Kong, it calls a URL with information about that event. Event hooks add a layer of configuration for subscribing to worker events using the admin interface. Worker events are integrated into Kong Gateway to communicate within the gateway context. For example, when an entity is created, the Kong Gateway fires an event with information about the entity. Parts of the Kong Gateway codebase can subscribe to these events, then process the events using callbacks.
In Kong Gateway, these callbacks can be defined using one of the following “handlers”:
webhook: Makes a JSON POST request to a provided URL with the event data as a payload. Useful for building a middle tier integration (your own webhook that receives Kong hooks). Specific headers can be configured for the request.
webhook-custom: Fully configurable request. Useful for building a direct integration with a service (for example, a Slack webhook). Because it’s fully configurable, it’s more complex to configure. It supports templating on a configurable body, a configurable form payload, and headers.
log: This handler, which requires no configuration, logs the event and the content of the payload into the Kong Gateway logs. If using hybrid mode, the and
dao:crud
sources will log on the control plane logs and thebalancer
andrate-limiting-advanced
sources will log on the data plane logs.lambda: This handler runs specified Lua code after an event is triggered.
Endpoint
/event-hooks
Response
List all sources
Sources are the actions that trigger the event hook. The /sources
JSON output follows the following pattern:
- 1st level = The source, which is the action that triggers the event hook.
- 2nd level = The event, which is the Kong entity the event hook will listen to for events.
- 3rd level = The available template parameters for use in webhook-custom payloads.
For instance, in the example below balancer
is the source, health
is the event, and upstream_id
, ip
, port
, hostname
, and health
are the available template parameters.
Endpoint
/event-hooks/sources/
{
"data": {
"balancer": {
"health": {
"fields": [
"upstream_id",
"ip",
"port",
"hostname",
"health"
]
}
},
"crud": {
"acls": {
"fields": [
"operation",
"entity",
"old_entity",
"schema"
]
},
. . .
"rate-limiting-advanced": {
"rate-limit-exceeded": {
"description": "Run an event when a rate limit has been exceeded",
"fields": [
"consumer",
"ip",
"service",
"rate",
"limit",
"window"
],
"unique": [
"consumer",
"ip",
"service"
}
}
}
}
List all events for a source
Events are the Kong entities the event hook will listen to for events. With this endpoint you can list all of the events associated with a particular source.
/event-hooks/sources/{source}/
The following response lists all of the events for the dao:crud
source.
Response
{
"data": {
"create": {
"fields": [
"operation",
"entity",
"old_entity",
"schema"
]
},
"delete": {
"fields": [
"operation",
"entity",
"old_entity",
"schema"
]
"update": {
"fields": [
"operation",
"entity",
"old_entity",
"schema"
]
}
}
}
Endpoint
/event-hooks
Request Body
Response
Add a custom webhook
Endpoint
/event-hooks
Request Body
Response
{
"config": {
"body": null,
"body_format": true,
"headers": {
"content-type": "application/json"
},
"headers_format": false,
"method": "POST",
"payload": {
"text": "Admin account `` d; email address set to ``"
},
"payload_format": true,
"secret": null,
"ssl_verify": false,
"url": "https://hooks.slack.com/services/foo/bar/baz"
},
"event": "admins",
"handler": "webhook-custom",
"id": "937df175-3db2-4e6d-8aa1-d95c94a76089",
"on_change": null,
"snooze": null,
}
Add a log event hook
Endpoint
Request Body
Response
{
"config": {},
"on_change": null,
"created_at": 1627346155,
"snooze": null,
"id": "13a16f91-68b6-4384-97f7-d02763a551ac",
"handler": "log",
"source": "crud",
"event": "routes"
}
Endpoint
/event-hooks
Request Body
Response
Test an event hook
It’s useful to manually trigger an event hook without provoking the event to be triggered. For instance, you might want to test the integration, or see if your hook’s service is receiving a payload from Kong.
POST any data to /event-hooks/:id-of-hook/test
, and the /test
endpoint executes the with the provided data as the event payload.
Endpoint
/event-hooks/{event-hook-id}/test
Response
{
"data": {
"consumer": {
"username": "Jane Austen"
},
"event": "consumers",
"source": "crud"
},
"result": {
"body": "",
"headers": {
"Cache-Control": "no-cache, private",
"Content-Type": "text/plain; charset=UTF-8",
"Date": "Fri, 30 Jul 2021 16:07:09 GMT",
"Server": "nginx/1.14.2",
"Transfer-Encoding": "chunked",
"Vary": "Accept-Encoding",
"X-Request-Id": "f1e703a5-d22c-435c-8d5d-bc9c561ead4a",
"X-Token-Id": "1cc1c53b-f613-467f-a5c9-20d276405104"
},
"status": 200
}
}
Ping a webhook event hook
Endpoint
/event-hooks/{event-hook-id}/ping
Response
{
"source": "kong:event_hooks",
"event_hooks": {
"source": "crud",
"id": "c57340ab-9fed-40fd-bb7e-1cef8d37c2df",
"on_change": null,
"event": "consumers",
"handler": "webhook",
"created_at": 1627581575,
"config": {
"headers": {
"content-type": "application/json"
},
"ssl_verify": false,
"url": "https://webhook.site/a1b2c3-d4e5-g6h7-i8j9-k1l2m3n4o5p6",
"secret": null
},
"snooze": null
},
"event": "ping"