hook
, their plugin in these phases of the API request/response lifecycle based on their specific use case.
Hook Type (in their execution order) | Plugin Type | HTTP Request/Response phase | Executed before/after reverse proxy to the upstream API | Details | Common Use Cases | |
---|---|---|---|---|---|---|
Pre (Request) | Request Plugin | HTTP request | Before | The first thing to be executed, before any middleware | IP Rate Limit plugins, API Request enrichment | |
Authentication | Authentication Plugin | HTTP request | Before | Replaces Tyk’s authentication & authorization middleware with your own business logic | When you need your a custom flow, for example, interfacing with legacy Auth database | |
Post-Auth (Request) | Authentication Plugin | HTTP request | Before | Executed immediately after authentication middleware | Additional special custom authentication is needed | |
Post (Request) | Request Plugin | HTTP request | Before | The final middleware to be executed during the HTTP request phase (see Note below) | Update the request before it gets to the upstream, for example, adding a header that might override another header, so we add it at the end to ensure it doesn’t get overridden | |
Response Plugin | Response Plugin | HTTP Response | After | Executed after the reverse proxy to the upstream API | Executed straight after the reverse proxy returns from the upstream API to Tyk | Change the response before the user gets it, for example, change Location header from internal to an external URL |
Analytics Plugin (Request+Response) | Analytics Plugin | HTTP request | After | The final middleware to be executed during the HTTP response phase | Change analytics records, for example, obfuscating sensitive data such as the Authorization header |
Functionality | Pre | Auth | Post-Auth | Post |
---|---|---|---|---|
Can modify the Header | ✅ | ✅ | ✅ | ✅ |
Can modify the Body | ✅ | ✅ | ✅ | ✅ |
Can modify Query Params | ✅ | ✅ | ✅ | ✅ |
Can view Session1 Details (metadata, quota, context-vars, tags, etc) | ❌ | ✅ | ✅ | ✅ |
Can modify Session1 2 | ❌ | ✅ | ❌ | ❌ |
Can Add More Than One3 | ✅ | ❌ | ✅ | ✅ |
ID Extractor
.
token
metadata when using custom authentication middleware, in order to give Tyk a unique ID with which to track each session.
For backwards compatibility, even when using an ID Extractor, we need to continue to set the token
metadata. For example, when building a session object in GoLang custom middleware:
token
field in the Metadata (see above) that Tyk will use to cache.
driver
: set this to the appropriate value for the plugin type (e.g. python
, goplugin
)response
: this is the hook name. You use middleware with the response
hook type because you want this custom middleware to process the request on its return leg of a round trip.response.name
: is your function name from the plugin file.response.path
: is the full or relative (to the Tyk binary) path to the plugin source file. Ensure Tyk has read access to this file.multivalue_headers
field to facilitate more flexible and efficient management of headers, particularly for scenarios involving a single header key associated with multiple values. The multivalue_headers
field, similar to its predecessor, the headers
field, is a key-value store. However, it can accommodate an array or list of string values for each key, instead of a single string value. This feature empowers you to represent multiple values for a single header key. Here’s an example of how you might use multivalue_headers
, using the Set-Cookie header which often has multiple values:
"sessionToken=abc123; HttpOnly; Secure"
and "language=en-US; Secure"
. To help you understand this further, let’s see how multivalue_headers
can be used in a Tyk response plugin written in Python:
Set-Cookie
header and then log both: the traditional headers
and the new multivalue_headers
. This is a great way to monitor your transition to multivalue_headers
and ensure that everything is functioning as expected.
Please note, while the headers
field will continue to be available and maintained for backward compatibility, we highly encourage the adoption of multivalue_headers
for the added flexibility in handling multiple header values.
analytics_plugin
configuration block within an API Definition. This contains the following configuration parameters:
enable
: Set to true
to enable the pluginfunc_name
: The name of the function representing the pluginplugin_path
: The path to the source code file containing the function that implements the pluginoperationId
defined in the OpenAPI Document that declares both the path and method for which the middleware should be added. The path
can contain wildcards in the form of any string bracketed by curly braces, for example {user_id}
. These wildcards are so they are human readable and do not translate to variable names. Under the hood, a wildcard translates to the “match everything” regex of: (.*)
.
The endpoint plugin middleware (postPlugins
) can be added to the operations
section of the Tyk OAS Extension (x-tyk-api-gateway
) in your Tyk OAS API Definition for the appropriate operationId
(as configured in the paths
section of your OpenAPI Document).
The postPlugins
object has the following configuration:
enabled
: enable the middleware for the endpointfunctionName
: this is the name of the Go function that will be executed when the middleware is triggeredpath
: the relative path to the source file containing the compiled Go codeGET
requests to the /anything
endpoint. For any call made to this endpoint, Tyk will invoke the function myUniqueFunctionName
in the file located at /middleware/myPlugin.so
.
The configuration above is a complete and valid Tyk OAS API Definition that you can import into Tyk to try out the per-endpoint custom plugin middleware.
go_plugin
object to the extended_paths
section of your API definition.
The go_plugin
object has the following configuration:
path
: the endpoint pathmethod
: the endpoint HTTP methodfunc_name
: this is the “symbol” or function name you are calling in your Go plugin once loaded - a function can be called by one or more APIsplugin_path
: the relative path of the shared object containing the function you wish to call, one or many .so
files can be calledGET
requests to the /anything
endpoint. For any call made to this endpoint, Tyk will invoke the function myUniqueFunctionName
in the file located at /middleware/myPlugin.so
.
go_plugin
section (you can search within the text editor window).
plugin_path
and func_name
to locate your compiled plugin function.
ID
to describe any key that’s used for authentication purposes.
When a custom authentication mechanism is used, every API call triggers a call to the associated middleware function, if you’re using a gRPC-based plugin this translates into a gRPC call. If you’re using a native plugin -like a Python plugin-, this involves a Python interpreter call.
The ID extractor works the following rich plugins: gRPC-based plugins, Python and Lua.
id_extractor_deadline
. See Plugin Data Structures for more details.
The value of this field should be a UNIX timestamp on which the cached ID will expire, like 1507268142958
. It’s an integer.
For example, this snippet is used in a NodeJS plugin, inside a custom authentication function:
id_extractor_deadline
and setting its value is enough to activate this feature.
In the above sample, Tyk will cache the key for 60 seconds. During that time any requests that use the cached ID won’t call your plugin.
enable_coprocess_auth
flag set to true, like the following definition:
id_extractor
key:
extract_from
specifies the source of the ID to extract.extract_with
specifies how to extract and parse the extracted ID.extractor_config
specifies additional parameters like the header name or the regular expression to use, this is different for every choice, see below for more details.param_name
represents the key of the submitted parameter:
regex_expression
, which represents the regular expression itself and regex_match_index
which is the item index:
prefix-d28e17f7
, given the regular expression we’re using, the extracted ID value will be d28e17f7
.
LastUpdated
or else token updates will not be applied. If you don’t set an ID Extractor, Tyk will store session information in the cache based off the token
field that is set in the metadata.