Vonage Users

1.2.1 · active · verified Thu Apr 16

The `vonage-users` library provides a Pythonic interface to the Vonage Users API, enabling management of users, devices, and associated properties within the Vonage platform. It is built on top of the main `vonage` Python SDK for authentication and HTTP communication. As of version 1.2.1, it requires Python 3.9+ and the `vonage` SDK version 3.x. The release cadence is irregular, typically tied to Vonage API updates.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the `VonageUsersClient`, list existing users, create a new user, retrieve a user by ID, and then delete that user for cleanup. It uses environment variables for secure API key management and includes basic error handling for Vonage API specific errors. Ensure your Vonage API keys and application ID are set as environment variables or replaced directly in the code for testing.

import vonage
import os
from vonage_users import VonageUsersClient

# Initialize Vonage client using environment variables for sensitive data
# This library requires a vonage.Client instance for its operations.
vonage_client = vonage.Client(
    key=os.environ.get('VONAGE_API_KEY', 'YOUR_API_KEY'),
    secret=os.environ.get('VONAGE_API_SECRET', 'YOUR_API_SECRET'),
    private_key=os.environ.get('VONAGE_PRIVATE_KEY', 'YOUR_PRIVATE_KEY').replace('\\n', '\n'), # Handle multiline key
    application_id=os.environ.get('VONAGE_APPLICATION_ID', 'YOUR_APPLICATION_ID')
)

# Initialize Vonage Users client with the base client
users_client = VonageUsersClient(vonage_client)

try:
    # Example: List users
    print("\n--- Listing Users ---")
    users = users_client.get_users()
    if users:
        for user in users:
            print(f"User ID: {user.id}, Name: {user.name}, Display Name: {user.display_name}")
    else:
        print("No users found.")

    # Example: Create a user (requires appropriate permissions and valid data)
    print("\n--- Creating a Test User ---")
    test_user_name = "my_unique_test_user"
    try:
        new_user = users_client.create_user(
            name=test_user_name,
            display_name="My Test User",
            image_url="https://example.com/default.png",
            properties={"custom_tag": "dev"}
        )
        print(f"Successfully created user: ID={new_user.id}, Name={new_user.name}")

        # Example: Get a user by ID
        print(f"\n--- Retrieving User by ID: {new_user.id} ---")
        retrieved_user = users_client.get_user(new_user.id)
        print(f"Retrieved user: Name={retrieved_user.name}, Display Name={retrieved_user.display_name}")

        # Example: Delete the created user (cleanup)
        print(f"\n--- Deleting Test User: {new_user.id} ---")
        users_client.delete_user(new_user.id)
        print(f"User {new_user.id} deleted successfully.")
    except vonage.errors.ClientError as e:
        if 'name is not unique' in str(e):
            print(f"Skipping user creation: User '{test_user_name}' already exists. Error: {e}")
        else:
            print(f"Error creating/deleting user: {e}")
    except Exception as e:
        print(f"An unexpected error occurred during user creation/deletion: {e}")

except vonage.errors.ClientError as e:
    print(f"Vonage API Error: {e}")
    print("Please ensure your VONAGE_API_KEY, VONAGE_API_SECRET, VONAGE_PRIVATE_KEY, and VONAGE_APPLICATION_ID are correctly set and have the necessary permissions.")
except Exception as e:
    print(f"An unexpected error occurred: {e}")

view raw JSON →