Consuming a JWT-protected API
JWT Auth is APEX’s recommended authentication method for sensitive APIs. Instead of sending a static key with every request, you generate a short-lived signed token each time. APEX validates the token against your public key before forwarding the request.
Before continuing, ensure you have:
Prefer to follow along visually? The JWT Auth interactive walkthrough covers the whole setup, from generating your JWKS to signing requests, in three short demos.
How it works
For every API call, you must:
- Generate a JWT — signed with your ES256 private key, containing the required claims below.
- Attach it to the request — in the
x-apex-jwtheader.
APEX fetches your public key from your JWKS endpoint, verifies the JWT signature, and forwards the request if valid.
Each JWT can only be used once. Generate a new JWT with a unique jti value for every request.
JWT claims
The JWT must be signed using ES256 only. All claim names are case-sensitive.
| Claim | Required | Description |
|---|---|---|
alg |
Yes | Must be ES256. |
typ |
Yes | Must be JWT. |
kid |
Yes | Key ID of the signing key in your JWKS. Used to identify which key to validate against. |
iat |
Yes | Unix timestamp (seconds, UTC) of when the JWT was issued. APEX allows a time skew of up to 5 seconds behind, but tokens with a future timestamp are rejected. |
exp |
Yes | Expiry timestamp. Must be no more than 180 seconds after iat. |
jti |
Yes | A unique random string (nonce) of at least 36 characters. A UUIDv4 is recommended. |
iss |
Yes | Your API key. For bridging APIs, provide both keys of your applications in each respective environment (Internet,Intranet) separated by a comma: key1,key2. |
aud |
Yes | The full API endpoint URL, excluding query parameters. Example: https://public.api.gov.sg/agency/api. |
sub |
Yes | The HTTP method of the request, in uppercase. Example: GET, POST. |
data |
POST / PUT / PATCH only | SHA-256 hex digest of the request payload. See Payload hashing below. |
Making the API call
Attach the serialized JWT to the x-apex-jwt header:
POST /agency/api
Host: public.api.gov.sg
x-apex-jwt: eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6ImFwZXgtZXhhbXBsZSJ9...
Content-Type: application/json
{"payload":"data"}Payload hashing
For POST, PUT, and PATCH requests, hash the request body with SHA-256 and include the hex digest in the data claim.
Before hashing, serialize your JSON payload into a compact string with no whitespace, tabs, or newlines between structural characters. This ensures a consistent hash across different operating systems.
Example:
Input: {"key":"value"}
SHA-256: <hex digest of the compact JSON string>
Include the result as the data claim in your JWT.
The
dataclaim is not required forGETrequests.
For detailed standardization rules and examples (including SOAP payloads), see API Payload Hash.
Key rotation
APEX caches your JWKS for up to 1 hour. When rotating keys:
- Add the new key (with a new
kid) to your JWKS endpoint. - Wait at least 1 hour before switching to sign with the new key.
- Remove the old key from your JWKS endpoint once it is no longer in use.
This ensures APEX has time to pick up the new key before you start using it.
Testing your JWT
Use the APEX Hello World! APIs to verify your JWT is correctly formed before calling a live API. The Hello World! API returns a success response if your x-apex-jwt header is valid.