python-jwt is a Python module for generating and verifying JSON Web Tokens (JWTs). It leverages the `cryptography` library for cryptographic operations and provides a straightforward API for encoding and decoding tokens. The current version is 4.1.0, with an intermittent, feature-driven release cadence.
pip install python-jwtVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to encode a JWT with a payload and secret key, and then decode it, including basic error handling for common JWT exceptions. Remember to use a strong, securely stored secret key in production.
Ensure your `cryptography` dependency is updated to `cryptography>=3.0.0`. If using `pip`, `pip install --upgrade python-jwt cryptography` should resolve it.
Always pass `algorithms` as a list: `jwt.decode(token, key, algorithms=['HS256'])`.
Wrap your `jwt.decode()` calls in `try...except jwt.exceptions.ExpiredSignatureError` and `try...except jwt.exceptions.InvalidTokenError` blocks to gracefully handle invalid or expired tokens.