DecodeJWT policy

This pageapplies toApigee andApigee hybrid.

View Apigee Edge documentation.

The DecodeJWT policy decodes a JWT without verifying the signature on the JWT. This is most useful when used in concert with theVerifyJWT policy, when the value of a claim from within the JWT must be known before verifying the signature of the JWT.

The JWT Decode policy works regardless of the algorithm that was used to sign the JWT. SeeJWS and JWT policies overview for a detailed introduction.

This policy is aStandard policy and can be deployed to any environment type. For information on policy types and availability with each environment type, seePolicy types.

Sample: Decode a JWT

The policy shown below decodes a JWT found in the flow variablevar.jwt. This variable must be present and contain a viable (decodable) JWT. The policy can obtain the JWT from any flow variable.

<DecodeJWTname="JWT-Decode-HS256"><DisplayName>JWTVerifyHS256</DisplayName><Source>var.jwt</Source></DecodeJWT>

The policy writes its output to context variables so that subsequent policies or conditions in the API proxy can examine those values. SeeFlow variables for a list of the variables set by this policy.

Element reference for Decode JWT

The policy reference describes the elements and attributes of the Decode JWT policy.

Attributes that apply to the top-level element

<DecodeJWT name="JWT" continueOnError="false" enabled="true" async="false">

The following attributes are common to all policy parent elements.

AttributeDescriptionDefaultPresence
name The internal name of the policy. Characters you can use in the name are restricted to:A-Z0-9._\-$ %. However, the Apigee UI enforces additional restrictions, such as automatically removing characters that are not alphanumeric.

Optionally, use the<displayname></displayname> element to label the policy in the Apigee UI proxy editor with a different, natural-language name.

N/ARequired
continueOnError Set tofalse to return an error when a policy fails. This is expected behavior for most policies.

Set totrue to have flow execution continue even after a policy fails.

falseOptional
enabled Set totrue to enforce the policy.

Set tofalse to "turn off" the policy. The policy will not be enforced even if it remains attached to a flow.

trueOptional
asyncThis attribute is deprecated.falseDeprecated

<DisplayName>

<DisplayName>Policy Display Name</DisplayName>

Use in addition to the name attribute to label the policy in the Apigee UI proxy editor with a different, natural-language name.

DefaultIf you omit this element, the value of the policy's name attribute is used.
PresenceOptional
TypeString

<Source>

<Source>jwt-variable</Source>

If present, specifies the flow variable in which the policy expects to find the JWT to decode.

Note: By default, the JWT is retrieved from the variablerequest.header.authorization. In this case, Apigee looks for the JWT in the request Authorization header. If you pass the JWT in the Authorization header, you do not need to include the Source element in the policy; however, youmust includeBearer in the auth header. For example, you would pass the JWT in the Authorization header like this:
curl -v http://52.200.92.187:9001/doctest-jwt/verify-rs256 -H "Authorization: Bearer <your JWT>"

You can configure to policy to retrieve the JWT from a form or query parameter variable or any other variable. If the variable does not exist or if the policy otherwise can't find the JWT, the policy returns an error.

Defaultrequest.header.authorization (See the note above for important information about the default).
PresenceOptional
TypeString
Valid valuesAn Apigee flow variable name

Flow variables

Upon success, theVerify JWT andDecode JWT policies set context variables according to this pattern:

jwt.{policy_name}.{variable_name}

For example, if the policy name isjwt-parse-token , then the policy will store the subject specified in the JWT to the context variable namedjwt.jwt-parse-token.decoded.claim.sub. (For backward compatibility, it will also be available injwt.jwt-parse-token.claim.subject)

Variable nameDescription
claim.audienceThe JWT audience claim. This value may be a string, or an array of strings.
claim.expiryThe expiration date/time, expressed in milliseconds since epoch.
claim.issuedatThe Date the token was issued, expressed in milliseconds since epoch.
claim.issuerThe JWT issuer claim.
claim.notbeforeIf the JWT includes a nbf claim, this variable will contain the value, expressed in milliseconds since epoch.
claim.subjectThe JWT subject claim.
claim.nameThe value of the named claim (standard or additional) in the payload. One of these will be set for every claim in the payload.
decoded.claim.nameThe JSON-parsable value of the named claim (standard or additional) in the payload. One variable is set for every claim in the payload. For example, you can usedecoded.claim.iat to retrieve the issued-at time of the JWT, expressed in seconds since epoch. While you can also use theclaim.name flow variables, this is the recommended variable to use to access a claim.
decoded.header.nameThe JSON-parsable value of a header in the payload. One variable is set for every header in the payload. While you can also use theheader.name flow variables, this is the recommended variable to use to access a header.
expiry_formattedThe expiration date/time, formatted as a human-readable string. Example: 2017-09-28T21:30:45.000+0000
header.algorithmThe signing algorithm used on the JWT. For example, RS256, HS384, and so on. See(Algorithm) Header Parameter for more.
header.kidThe Key ID, if added when the JWT was generated. See also "Using a JSON Web Key Set (JWKS)" atJWT policies overview to verify a JWT. See(Key ID) Header Parameter for more.
header.typeWill be set toJWT.
header.nameThe value of the named header (standard or additional). One of these will be set for every additional header in the header portion of the JWT.
header-jsonThe header in JSON format.
is_expiredtrue or false
payload-claim-namesAn array of claims supported by the JWT.
payload-json
The payload in JSON format.
seconds_remainingThe number of seconds before the token will expire. If the token is expired, this number will be negative.
time_remaining_formattedThe time remaining before the token will expire, formatted as a human-readable string. Example: 00:59:59.926
valid In the case of VerifyJWT, this variable will be true when the signature is verified, and the current time is before the token expiry, and after the token notBefore value, if they are present. Otherwise false.

In the case of DecodeJWT, this variable is not set.

Error reference

This section describes the fault codes and error messages that are returned and fault variables that are set by Apigee when this policy triggers an error. This information is important to know if you are developing fault rules to handle faults. To learn more, seeWhat you need to know about policy errors andHandling faults.

Runtime errors

These errors can occur when the policy executes.

Fault codeHTTP statusCauseFix
steps.jwt.FailedToDecode401Occurs when the policy is unable to decode the JWT. The JWT may be malformed, invalid or otherwise not decodable.
steps.jwt.FailedToResolveVariable401Occurs when the flow variable specified in the <Source> element of the policy does not exist.
steps.jwt.InvalidToken401Occurs when the flow variable specified in the <Source> element of the policy is out of scope or can't be resolved.

Deployment errors

These errors can occur when you deploy a proxy containing this policy.

Error nameCauseFix
InvalidEmptyElementOccurs when the flow variable containing the JWT to be decoded is not specified in the<Source> element of the policy.

Fault variables

These variables are set when a runtime error occurs. For more information, seeWhat you need to know about policy errors.

VariablesWhereExample
fault.name="fault_name"fault_name is the name of the fault, as listed in theRuntime errors table above. The fault name is the last part of the fault code.fault.name Matches "InvalidToken"
JWT.failedAll JWT policies set the same variable in the case of a failure.JWT.failed = true

Example error response

JWT Policy Fault Codes

For error handling, the best practice is to trap theerrorcode part of the error response. Do not rely on the text in thefaultstring, because it could change.

Example fault rule

    <FaultRules>        <FaultRule name="JWT Policy Errors">            <Step>                <Name>JavaScript-1</Name>                <Condition>(fault.name Matches "InvalidToken")</Condition>            </Step>            <Condition>JWT.failed=true</Condition>        </FaultRule>    </FaultRules>

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-17 UTC.