Vonage Utilities

1.1.4 · active · verified Thu Apr 16

The `vonage-utils` library provides foundational utility objects for interacting with Vonage APIs, including a base HTTP client, error handling, and signature verification. It is currently at version `1.1.4` and serves as a low-level dependency for the `vonage-python-sdk`, which is more frequently updated. While usable independently, most users interact with the higher-level SDK for full API functionality.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use the `SignatureVerification` class from `vonage-utils` to validate incoming Vonage webhook requests. This is crucial for securing your webhooks by ensuring the payload originated from Vonage and has not been altered.

import os
from vonage_utils.signature import SignatureVerification

# Replace with your actual signature secret from Vonage API dashboard
# For example, store it in an environment variable named VONAGE_SIGNATURE_SECRET
VONAGE_SIGNATURE_SECRET = os.environ.get("VONAGE_SIGNATURE_SECRET", "YOUR_SIGNATURE_SECRET")

if VONAGE_SIGNATURE_SECRET == "YOUR_SIGNATURE_SECRET":
    print("WARNING: Please set the VONAGE_SIGNATURE_SECRET environment variable or replace 'YOUR_SIGNATURE_SECRET' with your actual secret.")

# Example webhook payload (as a raw string, as received by your server)
webhook_payload = '{"msisdn":"1234567890","to":"19876543210","messageId":"00000000-0000-0000-0000-000000000000","text":"Hello World","type":"text","keyword":"HELLO","api-key":"YOUR_API_KEY","message-timestamp":"2023-10-27 10:00:00"}'

# Example X-Vonage-Signature header value received with the webhook
# This value is a hash calculated by Vonage based on the payload and your secret.
# (The example below is a placeholder; a real one would be dynamically generated.)
vonage_signature = "e10adc3949ba59abbe56e057f20f883e" # Placeholder: MD5 hash of "Hello World" for illustration

verifier = SignatureVerification(VONAGE_SIGNATURE_SECRET)

# To verify an incoming webhook signature
is_valid = verifier.check_signature(webhook_payload, vonage_signature)

if is_valid:
    print("Signature is valid! The webhook can be trusted.")
else:
    print("Signature is invalid! The webhook might be tampered with or is not from Vonage.")

# For actual production use:
# - Ensure VONAGE_SIGNATURE_SECRET is your correct secret.
# - `webhook_payload` must be the exact raw request body.
# - `vonage_signature` must be the exact value from the 'X-Vonage-Signature' HTTP header.

view raw JSON →