Skip to content
Last updated

The Sinch Python SDK allows you to quickly interact with the from inside your Python applications. When using the Python SDK, the code representing requests and queries sent to and responses received from the are structured similarly to those that are sent and received using the .

The fastest way to get started with the SDK is to check out our getting started guides. There you'll find all the instructions necessary to download, install, set up, and start using the SDK.

Syntax

Note:

This guide describes the syntactical structure of the Python SDK for the Voice API, including any differences that may exist between the API itself and the SDK. For a full reference on Voice API calls and responses, see the Voice API Reference.

The code sample below is an example of how to use the Python SDK to to make a text to speech phone call to a phone number. We've also provided an example that accomplishes the same task using the REST API.

app.py
from sinch import SinchClient

sinch_client = SinchClient(
    application_key="YOUR_application_key",
    application_secret="YOUR_application_secret"
)

response = sinch_client.voice.callouts.text_to_speech(
    destination={
        "type":"number",
        "endpoint":"YOUR_phone_number"
    }, 
    text="Hello, this is a call from Sinch. Congratulations! You made your first call.")

print(response)

This example highlights the following required to successfully make a Voice API call using the Sinch Python SDK:

Client

When using the Sinch Python SDK, you initialize communication with the Sinch backend by initializing the Python SDK's main client class. This client allows you to access the the functionality of the Sinch Python SDK.

Initialization

To start using the SDK, you need to initialize the main client class with your credentials from your Sinch dashboard and additionally add your Voice app credentials.

from sinch import SinchClient

sinch_client = SinchClient(
    application_key="YOUR_application_key",
    application_secret="YOUR_application_secret"
)
Note

For testing purposes on your local environment it's fine to use hardcoded values, but before deploying to production we strongly recommend using environment variables to store the credentials, as in the following example:

import os
from sinch import SinchClient

sinch_client = SinchClient(
    application_key=os.getenv("APPLICATION_KEY"),
    application_secret=os.getenv("APPLICATION_SECRET")
)

Voice domain

The Sinch Python SDK organizes different functionalities in the Sinch product suite into domains. These domains are accessible through the client. For example, sinch_client.voice.[endpoint_category].[method]. You can also create a domain-specific client from a general client. For example:


from sinch import Client
from sinch.domains.voice import Voice

sinch_client = Client(
    application_key="YOUR_application_key",
    application_secret="YOUR_application_secret"
)

voice_client = Voice(sinch_client)

In the Sinch Python SDK, Voice API endpoints are accessible through the client (either a general client or a Voice-specific client). The naming convention of the endpoint's representation in the SDK matches the API:

  • callouts
  • calls
  • conferences
  • applications

For example:


voice_response = sinch_client.voice.callouts.text_to_speech(
    destination={
        "type":"number",
        "endpoint":"YOUR_phone_number"
    },
    text="This is a text to speech message."
) 

The callouts category of the Python SDK corresponds corresponds to the callouts endpoint. The mapping between the API operations and corresponding Python methods are described below:

API operationSDK method
Makes a Text-to-speech callouttext_to_speech()
Makes a Conference calloutconference()
Makes a Custom calloutcustom()

The calls category of the Python SDK corresponds corresponds to the calls endpoint. The mapping between the API operations and corresponding Python methods are described below:

API operationSDK method
Get information about a callget()
Manage call with callLegmanage_with_call_leg()
Updates a call in progressupdate()

The conferences category of the Python SDK corresponds corresponds to the conferences endpoint. The mapping between the API operations and corresponding Python methods are described below:

The applications category of the Python SDK corresponds corresponds to the configuration endpoint. The mapping between the API operations and corresponding Python methods are described below:

Requests and queries made using the Python SDK are similar to those made using the Voice API. Many of the fields are named and structured similarly.

There are some important distinctions to note. For example, consider SVAML actions. In the REST API, SVAML actions must be written in JSON notation, as in the following example:

"action": {
    "name": "hangup"
}

But in the Python SDK, this is an object sent as a simple dict, as demonstrated by the following example:

"action": HangupAction().as_dict()

When translating field names from the Verification API to the Python SDK, remember that many of the API field names are in camelCase, whereas the Python SDK field names are in snake_case. This pattern change manages almost all field name translations between the API and the SDK.

When making calls directly to the API, we use JSON objects, including (in some cases) nested JSON objects. When using the Python SDK, we use dictionaries instead of nested JSON objects. For example, consider the callout destination configuration objects below. One is represented in JSON, the other as a Python dictionary:

destination= {
    "type": "number",
    "endpoint": "YOUR_phone_number"
}

Note that, in both cases, the destination object is structured in exactly the same way as they would be in a normal Python call to the Voice API. When using the Python SDK, any argument that represents a nested JSON object will be represented as a Python dictionary at the top level, but the contents of that dictionary must be represented as JSON objects.

Response fields match the API responses. They are delivered as Python objects, with each top-level field represented as a property. Note that any nested objects normally returned by the Voice API are returned as dictionaries by the Python SDK. Additionally, if there are any responses that differ significantly from the API responses, we note them in the endpoint category documentation.