jwt.decode in Python: verify_signature, algorithms and keys
In PyJWT, jwt.decode(token, key, algorithms=["HS256"]) verifies the signature by default, and verification needs an explicit algorithms list. Passing options={"verify_signature": False} only reads the payload, which is fine for debugging or logging but never for deciding who a user is, because anyone can change an unsigned read. If you want decoding, human-readable times and verification against a secret, PEM key or JWKS URL in one HTTP call, our JWT Decoder & Verifier API does it for $0.003.
Try it free JWT Decoder & Verifier API, HS/RS/PS/ES/EdDSA, $0.003 · $0.003 / call
The two modes in PyJWT
With verification off, PyJWT also skips the exp, nbf, aud and iss checks, so an expired token decodes without error. That surprises people who expect an ExpiredSignatureError.
import jwt # pip install pyjwt
# 1. Read only: no key, no checks. Use for debugging.
claims = jwt.decode(token, options={"verify_signature": False})
# 2. Verify: key plus an explicit list of allowed algorithms.
claims = jwt.decode(token, "my-test-secret", algorithms=["HS256"],
audience="my-api", leeway=30)
Mistakes that cause real bugs
- Leaving verify_signature off in production after debugging. The payload is only base64url, so anyone can edit it.
- Taking the algorithm from the token header. Always pass your own algorithms list; it blocks the old alg "none" and HMAC/RSA key-confusion tricks.
- Using a public key as an HMAC secret. An HS256 token must be checked with the shared secret, an RS256 token with the public key.
- Fetching keys from a jku or x5u URL inside the token. Only trust key URLs you configured.
- Forgetting the audience. A valid token for another service of the same issuer will pass.
Verifying RS256 tokens with a JWKS URL
from jwt import PyJWKClient
jwks = PyJWKClient("https://auth.example.com/.well-known/jwks.json")
key = jwks.get_signing_key_from_jwt(token)
claims = jwt.decode(token, key.key, algorithms=["RS256"], audience="my-api")
The same check as one API call
Useful for agents, no-code tools and quick support checks where installing a library is the hard part. Send the token plus a secret, a public_key (PEM, certificate or JWK) or a jwks_url. Decoding alone needs no key.
import requests
API = "https://iamai.markets/api/x402/services/Enz-BpY9bf19"
body = {"token": token, "secret": "my-test-secret"}
# pay_session: requests wrapped by an x402 v2 client, $0.003 per call
res = pay_session.post(API, json=body, timeout=30).json()["result"]
print(res["verification"]) # checked, valid, method, reason
print(res["time"]) # expired, not_yet_valid, expires_in_seconds
for c in res["claims"]:
print(c["claim"], c["meaning"], c.get("relative"))
What it refuses on purpose
The secret or key is used for that one call and not stored. For production auth, verify in your own code with PyJWT; use the API for inspection, agents and tooling.
- Encrypted tokens (JWE, five parts): they need the private key, which you should never send anywhere.
- A JWK that contains a private part ("d"): the call stops and asks for the public key.
- A public key for an HS256 token: reported as a failed check, which is the key-confusion defence.
The product
Questions
Why does PyJWT say algorithms is required?
Because verification without a fixed algorithm list was the cause of several JWT attacks. Pass the one algorithm your issuer uses.
Can I read a JWT without the secret?
Yes. The header and payload are encoded, not encrypted. Only the signature needs the key.
Does the API show if a token is expired?
Yes, with ISO times, "in 3 hours" or "2 days ago" text and expired and not_yet_valid flags.
Related
Updated 2026-12-02
