Vonage Verify Python Library

2.1.0 · active · verified Thu Apr 16

The `vonage-verify` package provides Python bindings for the Vonage Verify API, enabling developers to implement two-factor authentication (2FA) via various channels like SMS, Voice, WhatsApp, and Email. It also supports Silent Authentication for a seamless user experience. This library is designed to be used in conjunction with the main `vonage` Python SDK. The current version is 2.1.0, and it follows the release cadence of the broader Vonage Python SDK.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the Vonage client and send a verification request via SMS using `vonage-verify`. It uses environment variables for API credentials for security. The process involves creating a `VerifyRequest` with a specified `brand` and `workflow` (e.g., `SmsChannel`), and then calling `client.verify.start_verification`.

import os
from vonage import Vonage, Auth
from vonage_verify import VerifyRequest, SmsChannel

# Ensure VONAGE_API_KEY and VONAGE_API_SECRET are set as environment variables
client = Vonage(Auth(key=os.environ.get('VONAGE_API_KEY', ''), secret=os.environ.get('VONAGE_API_SECRET', '')))

# Define the recipient's phone number and brand name
recipient_number = os.environ.get('VONAGE_TEST_NUMBER', '1234567890') # Use E.164 format
brand_name = 'MyTestApp'

# Create an SMS channel for verification
sms_channel = SmsChannel(to=recipient_number)

# Construct the verification request with a workflow (SMS in this case)
params = {
    'brand': brand_name,
    'workflow': [sms_channel],
}
verify_request = VerifyRequest(**params)

try:
    # Start the verification process
    response = client.verify.start_verification(verify_request)
    print(f"Verification initiated. Request ID: {response['request_id']}")

    # In a real application, you would now prompt the user for the code
    # and then call client.verify.check_code(request_id=response['request_id'], code=user_entered_code)

    # Example of checking a code (assuming '1234' was sent and entered)
    # user_entered_code = '1234'
    # check_response = client.verify.check_code(request_id=response['request_id'], code=user_entered_code)
    # if check_response['status'] == '0':
    #     print('Verification successful!')
    # else:
    #     print(f"Verification failed: {check_response['error_text']}")

except Exception as e:
    print(f"An error occurred: {e}")

view raw JSON →