Vonage JWT

1.1.5 · active · verified Thu Apr 16

The `vonage-jwt` package provides tooling for generating JSON Web Tokens (JWTs) for Vonage APIs in Python. It is primarily utilized by the Vonage Python SDK for authentication but can also be used as a standalone library. The current version is 1.1.5, with its latest release uploaded on November 29, 2024, indicating a moderate release cadence.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the `JwtClient` with your Vonage Application ID and private key, then generate an application-level JWT. It expects the application ID and private key path to be provided via environment variables for security. The private key content is read from the specified file path. Replace placeholders or set environment variables before running.

import os
from vonage_jwt import JwtClient

# It's recommended to load these from environment variables or a secure configuration.
VONAGE_APPLICATION_ID = os.environ.get('VONAGE_APPLICATION_ID', 'YOUR_APPLICATION_ID')
VONAGE_PRIVATE_KEY_PATH = os.environ.get('VONAGE_PRIVATE_KEY_PATH', './private.key') # Path to your private.key file

private_key_content = None
try:
    with open(VONAGE_PRIVATE_KEY_PATH, 'r') as f:
        private_key_content = f.read()
except FileNotFoundError:
    print(f"Error: Private key file not found at {VONAGE_PRIVATE_KEY_PATH}.")
    print("Please ensure the path is correct or the file exists.")
    # In a real application, you might want to raise an exception or handle this more robustly.

if VONAGE_APPLICATION_ID != 'YOUR_APPLICATION_ID' and private_key_content:
    try:
        jwt_client = JwtClient(VONAGE_APPLICATION_ID, private_key_content)
        jwt_token = jwt_client.generate_application_jwt()
        print(f"Successfully generated JWT: {jwt_token}")
    except Exception as e:
        print(f"Error generating JWT: {e}")
else:
    print("Please set VONAGE_APPLICATION_ID and ensure VONAGE_PRIVATE_KEY_PATH points to a valid private key file.")

view raw JSON →