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.
This guide describes the syntactical structure of the Python SDK for the Voice v2 API, including any differences that may exist between the API itself and the SDK. For a full reference on Voice v2 API calls and responses, see the Voice v2 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.
"""
Sinch Python Snippet
This snippet is available at https://github.com/sinch/sinch-sdk-python/tree/main/examples/snippets
"""
import os
from dotenv import load_dotenv
from sinch import SinchClient
load_dotenv()
sinch_client = SinchClient(
project_id=os.environ.get("SINCH_PROJECT_ID") or "MY_PROJECT_ID",
key_id=os.environ.get("SINCH_KEY_ID") or "MY_KEY_ID",
key_secret=os.environ.get("SINCH_KEY_SECRET") or "MY_KEY_SECRET",
)
# The Sinch phone number to call from
from_phone_number = (
os.environ.get("SINCH_PHONE_NUMBER") or "MY_SINCH_PHONE_NUMBER"
)
# The phone number of the recipient to call
to_phone_number = "RECIPIENT_PHONE_NUMBER"
# The command dialing out to the recipient
dial_command = {
"command": "dial",
"call_name": "Python_SDK_Snippet_Call",
"from_": {"type": "PHONE", "phone": {"number": from_phone_number}},
"to": {"type": "PHONE", "phone": {"number": to_phone_number}},
"events": {
"on_answer": [
{
"command": "messages",
"messages": [
{
"type": "SAY",
"say": {
"text": "Hello, your call is now connected.",
"voice_name": "Emma",
},
}
],
}
],
"on_hangup": [{"command": "hangup"}],
},
}
# The SVAML commands describing the call flow
commands = [dial_command]
response = sinch_client.voice.v2.calls.start(commands=commands)
print(f"Call Successfully started.\n{response}")This example highlights the following required to successfully make a Voice v2 API call using the Sinch Python SDK:
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 functionality of the Sinch Python SDK.
To start using the SDK, you need to initialize the main client class with your credentials from your Sinch dashboard.
from sinch import SinchClient
sinch_client = SinchClient(key_id="key_id", key_secret="key_secret", project_id="YOUR_project_id")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(
key_id=os.getenv("KEY_ID"),
key_secret=os.getenv("KEY_SECRET"),
project_id=os.getenv("PROJECT_ID")
)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.v2.[endpoint_category].[method]. You can also create a domain-specific client from a general client. For example:
from sinch import SinchClient
from sinch.domains.voice.v2 import Voice
sinch_client = SinchClient(key_id="YOUR_key_id", key_secret="YOUR_key_secret",
project_id="YOUR_project_id")
voice_client = Voice(sinch_client)In the Sinch Python SDK, Voice v2 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:
For example:
dial_command = {
"command": "dial",
"call_name": "Python_SDK_Snippet_Call",
"from_": {"type": "PHONE", "phone": {"number": from_phone_number}},
"to": {"type": "PHONE", "phone": {"number": to_phone_number}},
"events": {
"on_answer": [
{
"command": "messages",
"messages": [
{
"type": "SAY",
"say": {
"text": "Hello, your call is now connected.",
"voice_name": "Emma",
},
}
],
}
],
"on_hangup": [{"command": "hangup"}],
},
}
commands = [dial_command]
response = sinch_client.voice.v2.calls.start(commands=commands)
Requests and queries made using the Python SDK are similar to those made using the Voice v2 API. Many of the fields are named and structured similarly.
When translating field names from the Voice v2 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 from configuration objects below. One is represented in JSON, the other as a Python dictionary:
"from_": {"type": "PHONE", "phone": {"number": from_phone_number}}Note that, in both cases, the from object is structured in exactly the same way as they would be in a normal Python call to the Voice v2 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 v2 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.



