Tyk v2.2 Documentation Components

  1. Home
  2. Tyk v2.2 Documentation Components
  3. Add events to your API Definition

Add events to your API Definition

Event handlers are assigned by event type, multiple handlers can be fired off per event. Event Handlers are fired as go routines and so will not interfere with request processing. The webhook event handler also features a ‘Flooding’ check so that the same requests are not sent over and over again to an endpoint – this is detailed in the webhook event handler documentation.

Event handlers are added to your API definition file (or database document) and have their own unique configuration parameters for each instance, here is an example of a multi-event, multi-handler configuration:

...
"event_handlers": {
        "events": {
            "QuotaExceeded": [
                {
                    "handler_name":"eh_log_handler",
                    "handler_meta": {
                        "prefix": "LOG-HANDLER-PREFIX"
                    }
                },
                {
                    "handler_name":"eh_web_hook_handler",
                    "handler_meta": {
                        "method": "POST",
                        "target_path": "http://posttestserver.com/post.php?dir=tyk-event-test",
                        "template_path": "templates/default_webhook.json",
                        "header_map": {"X-Tyk-Test-Header": "Tyk v1.BANANA"},
                        "event_timeout": 10
                    }
                }
            ]
        }
},
...

Each event handler that is added to the event_handlers.events section, is mapped by the event type (in the above case it’s QuotaExceeded), and then a list of each handler configuration, defined by the handler name and the handler metadata (usually some kind of configurable options for the specific handler).

So, in a less verbose example, event handlers are added by adding an event_handlers section to you API definition:

"event_handlers": {
    "events": {
        "EVENT_NAME": [
            {
                "handler_name": "HANDLER TO USE",
                "handler_meta": {
                    ...
                }
            }
        ],
        "EVENT_NAME": [
            {
                "handler_name": "HANDLER TO USE",
                "handler_meta": {
                    ...
                }
            }
        ],
    }
}