Vonage Python SDK

repository·main·Indexed 42 days ago

https://github.com/vonage/vonage-python-sdk

A comprehensive server-side library for integrating Vonage APIs, including SMS, Voice, Video, and Messages, into Python applications. The SDK provides a central client for API access and specialized packages for account management, application configuration, JWT generation via JwtClient, and Identity Insights. It includes the vonage-http-client for authenticated requests and supports multiple messaging channels such as WhatsApp, RCS, Messenger, and Viber.

Tokens
106.7K
Snippets
397
Records
483
Agent score
53%

What's inside Vonage Python SDK

  1. Use the Vonage Legacy Verify Package

    main

    The vonage_verify_legacy package provides methods for interacting with Vonage's legacy Verify API, specifically for 2-factor authentication (2FA) via SMS or TTS.

    Note: This is a legacy package. For most modern use cases, it is recommended to use the current Vonage Verify API, which supports additional channels like Email, WhatsApp, and Messenger, as well as Silent Authentication.

  2. Manage Vonage Account via the Account Package

    main

    The account package provides methods for managing Vonage accounts, including querying account balances, performing top-ups, checking service pricing, managing account settings (like SMS webhooks), and handling account secrets.

    It is recommended to use this package as part of the main vonage package. The following examples assume you have already instantiated the main client as vonage_client.

  3. Use the Vonage Video API with Python

    main

    The vonage_video module provides methods for managing video sessions, streams, signals, and advanced features like archiving, broadcasting, and SIP calls.

    It is recommended to use this module as part of the main vonage package. Most API calls require an instance of the vonage.Vonage class (referred to here as vonage_client). The API uses custom Pydantic data models for request parameters to ensure type safety and validation.

    import vonage
    
    # Assuming you have initialized your client
    vonage_client = vonage.Vonage(api_key='...', api_secret='...')
    
    # Access video features via the .video property
    # vonage_client.video.<method_name>(...)
  4. Use Vonage Number Insight API

    main

    The Vonage Number Insight package allows you to retrieve information about phone numbers using three levels of insight: basic, standard, and advanced.

    • Basic: Provides minimal information.
    • Standard: Provides more detailed information. You can optionally request caller name information (CNAM) by setting the cnam parameter to True (note: this incurs an additional charge).
    • Advanced: Provides the highest level of detail. This can be requested synchronously or asynchronously.

    Asynchronous Advanced Insight is recommended to avoid timeouts. In an asynchronous request, the API returns basic request information immediately and sends the full dataset to a specified callback webhook URL.

    It is recommended to use this package as part of the main vonage package. The following examples assume you have already instantiated the vonage.Vonage class as vonage_client.

  5. Access API Methods and Data Models in v4

    main

    Accessing API Methods

    API methods are accessed as named attributes on the vonage.Vonage instance. For example, to use the Video API, use vonage_client.video.method_name().

    Accessing Data Models

    Data models and errors are not accessed through the main vonage package. Instead, they are imported from their specific API packages:

    1. Standard APIs: Import from the top level of the API package (e.g., from vonage_verify import VerifyRequest).
    2. Complex APIs: For APIs with many models (like vonage-messages, vonage-voice, or vonage-video), import from the .models submodule (e.g., from vonage_messages.models import MessengerImage).
    # Example: Using Verify API
    from vonage_verify import VerifyRequest, SmsChannel
    
    sms_channel = SmsChannel(to='1234567890')
    verify_request = VerifyRequest(brand='Vonage', workflow=[sms_channel])
    
    # Accessing the method via the client
    response = vonage_client.verify.start_verification(verify_request)
  6. Identify Vonage SDK errors with VonageError

    main
    The VonageError type is the base exception class for all exceptions related to the Vonage SDK. You can access it directly from the vonage_utils package or via the main SDK module using vonage.VonageError to implement robust error handling for your integration.
  7. How the Vonage Python SDK monorepo structure works

    main

    The Vonage Python SDK is organized as a monorepo. While the top-level vonage package provides access to most API methods, specialized data models and request/response objects are located in specific API packages (e.g., vonage-sms, vonage-account).

    To use an API, you typically use the vonage.Vonage client instance for the method call, but import the specific message or request objects from their corresponding sub-package.

    Example: Sending an SMS

    1. Use vonage to access the .sms.send() method.
    2. Use vonage_sms to import SmsMessage and SmsResponse.
    from vonage_sms import SmsMessage, SmsResponse
    
    # message is created using the specialized package
    message = SmsMessage(to='1234567890', from_='Acme Inc.', text='Hello, World!')
    
    # the call is made via the top-level vonage client
    response: SmsResponse = vonage_client.sms.send(message) # vonage_client is an instance of `vonage.Vonage`
  8. Configure HttpClientOptions

    main

    The HttpClientOptions class allows you to customize the behavior of the HTTP client. You can configure:

    • api_host: The API host.
    • rest_host: The REST host.
    • timeout: Request timeout.
    • pool_connections: Connection pool settings.
    • pool_max_size: Maximum pool size.
    • max_retries: Maximum number of retries.
  9. How the Number Verification workflow works

    main

    The Vonage Number Verification API uses OAuth2 authentication to verify a mobile device's phone number without user input. The process consists of three stages:

    1. Get an OIDC URL: Generate a URL using the SDK to be used in your front-end application.
    2. Authorization: Use that URL in your application to obtain an authorization code (the user's device must be connected to a mobile network).
    3. Verification Request: Use the obtained authorization code to make a final Number Verification Request to verify the number.

    This package provides helper methods for stages 1 and 3.