Skip to main content
Context variables are extracted from the request at the start of the middleware chain. These values can be very useful for later transformation of request data, for example, in converting a form POST request into a JSON PUT request or to capture an IP address as a header.
When using Tyk Classic APIs, you must enable context variables for the API to be able to access them. When using Tyk OAS APIs, the context variables are always available to the context-aware middleware.

Available context variables

From Tyk 5.13.0 the current Session’s rate limit and quota data are available from the following context variables:

Middleware that can use context variables:

Context variables are exposed in three middleware plugins but are accessed differently depending on the caller as follows:
  1. URL Rewriter - Syntax is $tyk_context.CONTEXTVARIABLES. See Path Modification for more details.
  2. Modify Headers - Syntax is $tyk_context.CONTEXTVARIABLES. See Request Headers for more details.
  3. Body Transforms - Syntax is {{ ._tyk_context.CONTEXTVARIABLES }}. See Body Transforms for more details.
The Body Transform can fully iterate through list indices within context data so, for example, calling {{ index ._tyk_context.path_parts 0 }} in the Go Template in a Body Transform will expose the first entry in the path_parts list.URL Rewriter and Header Transform middleware cannot iterate through list indices.

Example use of context variables

Examples of the syntax to use with all the available context variables:

Example of the syntax in the UI

The context variable values in the response:

Enabling Context Variables for use with Tyk Classic APIs

  1. In the your Tyk Dashboard, select APIs from the System Management menu
  2. Open the API you want to add Context Variable to
  3. Click the Advanced Options tab and then select the Enable context variables option
Context Variables If not using a Tyk Dashboard, add the field enable_context_vars to your API definition file at root level and set it to true. If you are using Tyk Operator, set the field spec.enable_context_vars to true. The example API Definition below enabled context variable: