JWT key mappings
When applications sign in to the gateway with a JWT from your identity provider, the gateway normally works out the team and user from the token's claims. A JWT key mapping goes one step further. It says that a token whose claim, for example azp, has a given value should run as one specific virtual key, with that key's models, budget, rate limits, guardrails and spend attribution. The application never holds the key. It presents its JWT, and the gateway looks the key up. Mappings are managed by proxy admins and need a license that includes JWT auth, because they only apply when JWT auth is on.
When you would use this
Your billing service authenticates with a client credentials token from your identity provider. You want its requests to count against the billing-service key, which has a 50 dollar monthly budget and two allowed models, without storing that key in the service's configuration. You create a mapping from azp equal to billing-service to that key. Every request the service makes with its token is now authorized as that key, shows up under the key in the logs and stops when the key's budget is spent.
Before you start
JWT auth has to be configured on the gateway, see JWT authentication. The claim the gateway reads for the lookup is virtual_key_claim_field under general_settings.spanway_jwtauth, and unregistered_jwt_client_behavior says what happens to a token with no mapping.
general_settings:
spanway_jwtauth:
virtual_key_claim_field: azp
unregistered_jwt_client_behavior: reject
virtual_key_mapping_cache_ttl: 300
You also need the virtual key the client should run as, either its plaintext value or its SHA-256 hash from /key/list.
Step by step
-
Create the mapping. In the Admin UI open Settings, then Admin Settings, open the JWT Key Mappings tab and click + Create Mapping. Fill in Claim Name (
azp), Claim Value (billing-service), choose under Identify the Key By whether you paste the key or its hash, fill in Mapped Key, optionally an Issuer and a Description, and save. Over the API callPOST /jwt/key/mapping/newwith exactly one ofkeyortoken.curl -X POST http://localhost:4000/jwt/key/mapping/new \-H "Authorization: Bearer $SPANWAY_API_KEY" \-H "Content-Type: application/json" \-d '{"jwt_claim_name": "azp","jwt_claim_value": "billing-service","key": "sk-9f3a2c7e41b8d605","jwt_issuer": "https://login.example.com","description": "Billing service"}'{"id": "jkm-01J8Z","jwt_issuer": "https://login.example.com","jwt_claim_name": "azp","jwt_claim_value": "billing-service","description": "Billing service","is_active": true,"created_at": "2026-11-01T09:00:00Z","updated_at": "2026-11-01T09:00:00Z","created_by": "admin","updated_by": "admin"}The response never includes the key or its hash. Leave
jwt_issuerout to match the claim from every configured issuer. -
Try it. Call the gateway with the client's JWT instead of a key.
curl -X POST http://localhost:4000/v1/chat/completions \-H "Authorization: Bearer $JWT" \-H "Content-Type: application/json" \-d '{"model": "claude-sonnet-5-5", "messages": [{"role": "user", "content": "Hello"}]}' -
Review the mappings. The JWT Key Mappings tab lists every mapping with its claim, issuer (Any issuer when none is set), status and last update. Over the API use
GET /jwt/key/mapping/listwithpageandsize, orGET /jwt/key/mapping/info?id=...for one.curl "http://localhost:4000/jwt/key/mapping/list?page=1&size=50" -H "Authorization: Bearer $SPANWAY_API_KEY" -
Change a mapping. Open its row menu, click edit and change the Mapped Key, Issuer, Description or the Active switch, then save. Over the API send the fields to change to
POST /jwt/key/mapping/update. The claim name and value cannot change, so delete and recreate the mapping to move it to another claim.is_active: falsepauses the mapping without deleting it.curl -X POST http://localhost:4000/jwt/key/mapping/update \-H "Authorization: Bearer $SPANWAY_API_KEY" \-H "Content-Type: application/json" \-d '{"id": "jkm-01J8Z", "key": "sk-9876wxyz5432stuv", "description": "Billing service, rotated key"}' -
Delete a mapping. Click Delete in the row menu and confirm Delete JWT key mapping?, or call
POST /jwt/key/mapping/delete. Tokens with that claim stop resolving to the key right away.curl -X POST http://localhost:4000/jwt/key/mapping/delete \-H "Authorization: Bearer $SPANWAY_API_KEY" \-H "Content-Type: application/json" \-d '{"id": "jkm-01J8Z"}'
What you should see
A request with a mapped token succeeds and its spend log row carries the mapped key's hash, alias, team and user, exactly as if the key itself had been used. /key/info on the key shows the spend growing. A token whose claim has no mapping follows unregistered_jwt_client_behavior, which continues with team based JWT auth by default, answers 403 with reject, or creates a key and a mapping on first contact with auto_register. Lookups are cached for virtual_key_mapping_cache_ttl seconds, and every create, update and delete drops the affected cache entries and writes an audit log row.
| Method | Path | Summary |
|---|---|---|
| POST | /jwt/key/mapping/delete | Delete a JWT key mapping |
| GET | /jwt/key/mapping/info | Get one JWT key mapping |
| GET | /jwt/key/mapping/list | List JWT key mappings |
| POST | /jwt/key/mapping/new | Map a JWT claim value to a virtual key |
| POST | /jwt/key/mapping/update | Change a JWT key mapping |
Errors
| Error | Cause | Fix |
|---|---|---|
403 JWT key mappings only apply when JWT auth is on, and JWT auth needs a Spanway enterprise license. | The gateway has no license and you tried to create or update a mapping. | Add a license. Listing, reading and deleting mappings keep working without one so old mappings can be cleaned up. |
403 Only proxy admins can create JWT key mappings | The caller is not a proxy admin. | Use a proxy admin key. Admin viewers can list and read mappings. |
400 Provide exactly one of key ... or token ... | The body has both key and token, or neither. | Send one of them. |
400 token must be the SHA-256 hash of a virtual key (64 lowercase hex characters). | A plaintext key was sent as token. | Send it as key, or send the hash from /key/list. |
400 The provided key does not match an existing virtual key. | The key or hash is not in the database. | Check the key with /key/info. |
409 A mapping for claim 'azp' = 'billing-service' already exists for issuer '...' | The same claim and issuer are already mapped. | Update the existing mapping instead. |
404 Mapping not found | The id does not exist. | List mappings with /jwt/key/mapping/list. |
Mapped token still answers 403 | The mapping is paused, the token's issuer does not match jwt_issuer, or virtual_key_claim_field names another claim. | Check is_active, the issuer and the claim name against the token. |
Related
- JWT authentication for the
spanway_jwtauthsettings and team based JWT auth. - Virtual keys for the key the mapping points at.
- Workload identity federation for trading an OIDC token for a short lived key instead.
- Audit logs for the rows each change writes.