Policies¶
ConceptExplains how it works and why.
A policy is a rule the gateway applies to a request, a response, or both. Every policy has typed settings with defaults, so most work without configuration, and every setting is documented in the policy reference, generated from the code.
The catalogue¶
| Phase | Policies | Answers the question |
|---|---|---|
| Access | ip-filter, cors, allow-methods |
Who may reach this at all |
| Protection | forward, request-size-limit, threat-protection, validate, require-headers, deny |
Is the request safe to read, does it honour the contract, what may reach the backend |
| Authentication | api-key, oauth2, jwt, basic |
Who is calling (at a glance) |
| Authorization | subscription (managed by products), lifecycle (managed by versions) |
Are they allowed, is this version still served |
| Traffic | rate-limit, concurrency-limit |
How much |
| Cache | cache |
Can this be answered without the backend |
| Errors | normalize-errors |
What error bodies look like |
| Composition | mock-response, service-callout, soap |
Which other backends are called, and what their answers fill; what the gateway answers itself |
| Transformation | transform, set-header, upstream-auth, compress |
What the backend and the caller see, and how large the answer is on the wire |
Phases fix the execution order. Within a phase, the order is the one you wrote.
Binding a policy¶
policies:
- type: rate-limit
config:
limit: 100
window: 1m
- type: jwt
name: partners # optional: several bindings of one type, told apart by name
config:
issuer: https://login.example.com/
jwks_url: https://login.example.com/.well-known/jwks.json
At a narrower level (an API under the environment, an operation under its API):
| Setting | Effect |
|---|---|
strategy: override (default) |
Replaces the inherited binding's configuration |
strategy: merge |
Deep-merges into the inherited configuration |
strategy: append |
Runs in addition to the inherited one |
enabled: false |
Switches the inherited policy off here |
Example: a default for the API, an exception for one operation¶
A limit of 100 calls a minute per user on an HR API, except for login, which has no user yet and must be tighter:
apiVersion: apinx/v1
kind: API
name: hr-mobile
policies:
- type: rate-limit
config: { limit: 100, window: 1m, key: consumer }
operations:
- name: login
path: /api/auth/login
policies:
- type: rate-limit # override is the default
config: { limit: 10, window: 1m, key: ip }
- name: employees
path: /api/employees # inherits 100 a minute
The same works one level up: a limit bound on the environment is the default for every API, and
an API overrides it the same way. Effective policies on any operation (and
GET /api/v1/routes/{id}/effective-policies) show the result with the level each entry came
from.
A rate-limit or concurrency-limit counts as wide as the level it is bound at. Bound on an
API, one counter covers every operation of that API: 100 calls a minute per user means 100
across all of them, not 100 on each. Bound on the environment, one counter covers every API.
Bound on an operation, as login is above, it counts that operation alone, and the rest of the
API keeps counting together. In a gateway file written by hand the same rule is the binding's
scope field (route, api or environment); the console sets it for you from where you
bound the policy.
Rate-limit counters and concurrency slots on an instance survive a deploy when the binding is unchanged: a caller at its limit stays at its limit, and a request still in flight on the old configuration releases its slot into the new count. A changed limit, window or key starts counting afresh, as the person who changed it expects. Counts shared through Redis never depended on this; they live outside the policy instance.
Two levels are deliberately not overridable from below:
- Plan limits and quotas (the
subscriptionpolicy a product adds) are the contract with the consumer: no API or operation can override or merge its settings. An operation may switch the whole policy off (enabled: false), which makes that operation public and unmetered, and may add a tighterrate-limitof its own, which runs in addition. - Environment flow hooks run on every API and are locked; no API or operation can change or switch them off. Use them for rules the platform team must guarantee. Shared flows and flow hooks.
References in values¶
Header, path and query values in transform, set-header and upstream-auth may reference the
request:
| Reference | Value |
|---|---|
${path.<param>} |
A path parameter captured by the operation, e.g. /orders/{id} |
${header.<Name>}, ${query.<name>} |
A request header or query parameter |
${principal.id}, ${principal.name} |
The authenticated caller |
${claim.<name>} |
A claim of the caller's validated JWT; dotted for nested claims (${claim.profile.dept}) |
${body.<field>} |
A field of a JSON request body, dotted for nested fields (transform only; buffers the body) |
${client_ip}, ${host}, ${method}, ${route} |
The connection and the match |
${callout.<name>.body.<field>}, ${callout.<name>.status}, ${callout.<name>.header.<Name>} |
A service callout's answer (composition) |
${response.body.<field>}, ${response.status}, ${response.header.<Name>} |
The backend's answer, for callouts that run after it |
${secret.<name>} |
A sealed secret of the service-callout binding, inside that binding only |
A header whose reference is empty on a request is left out rather than sent empty.
What the caller sees on refusal¶
Every refusal carries a stable code in X-Apinx-Error and a uniform JSON body with a message
and a remediation. The error reference lists them all; the console
links every error to its explanation.
Managed policies¶
Two policies are bound by the control plane, not by you:
subscriptionon every API in a product, from the product's plans. Switch it off on an operation to make that operation public.lifecycleon the operations of a deprecated or retired version.
Where policies run in the console¶
The Develop tab of an API draws the request flow and the response flow with every step,
including inherited and hooked ones, and lets you add, edit, reorder and switch off policies.
Develop → Policies → Usage answers "where is rate-limit bound?". The Catalogue tab lists every
policy with its settings and an example.