Skip to main content

Identity Headers

After API Management authenticates a request on a route linked to an API, it adds identity headers to the request that it sends to your upstream service. Middlewares on the same route can read these headers, for example to select an ai-quota budget. The response that goes back to the client does not include these headers.

Headers by Authentication Method​

Each authentication method sets only the headers in its column.

HeaderAPI keyJWTLDAPKeyless
Hub-App-IdThe spec.appId of the ManagedApplication that owns the keyThe value of the claim named in jwt.appIdClaimThe username used to authenticateNot set
Hub-App-NameThe metadata.name of the ManagedApplication that owns the keyNot set, unless jwt.forwardHeaders maps it to a claimNot setNot set
Hub-Token-NameThe title of the key in apiKeys[]. Traefik Hub sends an empty value when the key has no titleThe value of the claim named in jwt.tokenNameClaim. Traefik Hub does not set the header when jwt.tokenNameClaim is empty, unless jwt.forwardHeaders maps it to a claimNot setNot set

Traefik Hub forwards the key title unchanged in Hub-Token-Name. Upstream services must treat it as untrusted text. A title that contains a line break or another control character makes every request with that key fail with a 500 error.

For a JWT, jwt.forwardHeaders adds more headers. Each entry maps a header name to a claim name.

jwt:
forwardHeaders:
X-User-Email: email

See JWT Authentication for the full APIAuth options.

Set Hub-User-Id for a JWT​

The AI Gateway fills the user_id label of the GenAI metrics from the Hub-User-Id header. No authentication method sets this header by default. For a JWT, map the header to a claim in jwt.forwardHeaders, for example Hub-User-Id: sub.

Client-Sent Header Removal​

Starting in v3.21.5, Traefik Hub removes these headers from each request on a route linked to an API, before it authenticates the request:

  • Hub-App-Id, Hub-App-Name, and Hub-Token-Name.
  • Every header named in forwardHeaders of the APIAuth resources that apply to the API.

The upstream service receives only the values that the authentication method wrote after it accepted the request. This applies to every API, including an API with keyless authentication, so a client cannot supply its own Hub-App-Id.

Traefik Hub removes headers by exact name. It does not remove:

  • Other Hub-* headers, such as Hub-User-Id and Hub-User-Email, unless you list them in jwt.forwardHeaders. Treat a Hub-User-* value as client input.
  • A header with a different separator, such as Hub_App_Id. Read only the exact header names in the table.

Before v3.21.5, Traefik Hub keeps a client-sent value for every header that the authentication method does not set. For example, a keyless API forwards a client-sent Hub-App-Id.

Routes Not Linked to an API​

A route is linked to an API when its IngressRoute, Ingress, or HTTPRoute has the annotation hub.traefik.io/api: <API name>, or hub.traefik.io/api-version: <APIVersion name> for an APIVersion. On any other route, Traefik Hub does not authenticate the request, and it neither sets nor removes these headers. The upstream service and the middlewares receive whatever the client sends.

Before you use these headers as budget keys or as an identity, link the route to an API.

Limit Budgets per Application or API Key​

The ai-quota middleware keeps one budget for each value of the header that sourceCriterion.requestHeaderName names. Create one ai-quota middleware for each budget, and set sourceCriterion to the header that selects the budget:

# Budget for each application
sourceCriterion:
requestHeaderName: Hub-App-Id
---
# Budget for each API key
sourceCriterion:
requestHeaderName: Hub-Token-Name

Add both middlewares to the IngressRoute that is linked to the API, after the Chat Completion middleware. See Per-User Limits with Source Criterion for the other middleware options, such as store and totalTokenLimit.

When both middlewares are on the route:

  • Each middleware enforces its own budget. When one key spends its budget, the other keys of the application keep working until the application budget is spent.
  • A client cannot reset a budget by sending its own Hub-Token-Name or Hub-App-Id. Traefik Hub replaces the value before the middleware runs.
  • A key without a title sends an empty Hub-Token-Name. ai-quota counts every request with an empty or missing header in one shared budget, across all clients and applications. Set a title on every key that you use as a budget key.

Troubleshooting​

IssueCauseFix
The upstream service receives no Hub-* headerThe route is not linked to an API, or the API uses keyless authenticationAdd the hub.traefik.io/api annotation to the route, and use an APIAuth with apiKey, jwt, or ldap
Hub-Token-Name is missing on a JWT APIjwt.tokenNameClaim is not set, or the token lacks that claim. An ai-quota that uses Hub-Token-Name then counts these requests from all clients and applications in one budget, so one client can use up the budget for all of themSet jwt.tokenNameClaim to the claim that holds the token name, and issue tokens that include the claim
Hub-Token-Name is emptyThe API key has no title. An ai-quota that uses Hub-Token-Name then counts requests from all untitled keys, and from requests without the header, in one budget, so one client can use up the budget for all of themSet title on the key in apiKeys[] of the ManagedApplication
A client-sent Hub-App-Id reaches the upstream serviceThe route is not linked to an APIAdd the hub.traefik.io/api annotation to the route

Next steps​