Product-scoped modifications of requests and responses.
Transformations allow you to dynamically modify requests and responses. That includes headers, query string parameters and body.
Product-scoped transformations are applied to requests/responses only if the customer is subscribed to this product.
Transformations defined on an API level are always applied first.
Read about API-scoped transformations.
To configure transformations, navigate to Products → select product → Transformations.
The transformation will add/replace/remove headers in a request or response.
Header names are case-insensitive.
Adds a new header, even if there is already one with the same name.
Example
Original request:
Transformation:
Transformed request:
Replaces header(s) with a new value. If there are several headers with the given name, they’re removed and replaced with the new value.
Example
Original request:
Transformation:
Transformed request:
Removes all headers with the given name.
Example
Original request:
Transformation:
Transformed request:
The transformation will add/replace/remove query string parameters in a request.
Query string parameter names are case-sensitive.
Adds a new query string parameter, even if there is already one or more with the same name.
Example
Original request:
Transformation:
Transformed request:
Replaces query string parameter(s) with a new value. If there are several query string parameters with the given name, they’re removed and replaced with the new value.
Example
Original request:
Transformation:
Transformed request:
Removes all query string parameters with the given name.
Example
Original request:
Transformation:
Transformed request:
The transformation will add/replace/remove request/response body.
Adds a new body. If a request or response already has a body, it’s left unchanged.
Example
Original request:
Transformation:
Transformed request:
Replaces the body with a new value. If there is no body yet, it’s added.
Example
Original request:
Transformation:
Transformed request:
Removes the body.
Example
Original request:
Transformation:
Transformed request:
You can use JavaScript expressions in your transformations to make them even more dynamic.
Expressions must evaluate to a new string value for the header/query string parameter/body.
Example — News API
Say, you provide an API endpoint that returns contents of a news article. E.g.,
For your “Free” product, you’d like to return only the first 120 characters of article body.
With the following transformation added to the “Free” product, you configure Nadles API Gateway to do that:
Note that the expression evaluation result is used as the new value for the response body.
There are several variables you can use in your expressions.
path.params.*
— placeholder values specified in the endpoint URL.
Example
If an endpoint URL is /resource/{resourceId}
and the HTTP request URL is /resource/801d49c2-ca05-42b1-97af-baf0ddf36ba3
,
then there will be a variable path.params.resourceId
with value "801d49c2-ca05-42b1-97af-baf0ddf36ba3"
.
Path parameter names are case-sensitive.
request.remote_addr
— Client IP address.
request.headers['header-name']
— Request header values.
Example
Header names must be in lower case.
request.query['query_string_parameter_name']
— Request query string parameters.
Example
Query string parameter names are case-sensitive.
request.body
— Request body.
Example
Response variables are only available in response transformations.
response.statusCode
— HTTP status code of the response from the upstream.
Example
response.headers['header-name']
— Response header values.
Example
Header names must be in lower case.
response.body
— Raw response body.
Example
You can also use jq expressions for transformations.
jq is like sed for JSON data - you can use it to slice and filter and map and transform JSON with the same ease that sed, awk, grep and friends let you play with text.
Unlike JavaScript expressions, the result of jq transformation does not necessarily have to be a string. If the expression result is not a string, it’s encoded as JSON.
Example — News API
Say, you provide an API endpoint that returns contents of a news article. E.g.,
For your “Free” product, you’d like to return only the first 120 characters of article body.
The following jq expression will do the job:
Note that the expression evaluation result is used as the new value for the response body.
The followind is passed as input (.
) to your jq expressions: