Sinch Python SDK for Voice
The Sinch Voice Python SDK allows you to quickly interact with the Voice API from inside your Python applications. 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
When using the Python SDK, the code representing requests and queries sent to and responses received from the Voice API are structured similarly to those that are sent and received using the Voice API itself.
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 on the side of this page is an example of how to use the Python SDK to initiatate an SMS PIN verification request. The Voice API call that accomplishes the same task is displayed below for reference:
import requests
key = ""
secret = ""
from_number = ""
to = ""
locale = ""
url = "https://calling.api.sinch.com/calling/v1/callouts"
payload = {
"method": "ttsCallout",
"ttsCallout": {
"cli": from_number,
"destination": {
"type": "number",
"endpoint": to
},
"locale": locale,
"text": "Hello, this is a call from Sinch. Congratulations! You made your first call."
}
}
headers = {"Content-Type": "application/json"}
response = requests.post(url, json=payload, headers=headers, auth=(key, secret))
data = response.json()
print(data)
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)
Endpoint categories
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."
)
callouts
endpoint category
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 operation | SDK method |
---|---|
Makes a Text-to-speech callout | text_to_speech() |
Makes a Conference callout | conference() |
Makes a Custom callout | custom() |
calls
endpoint category
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 operation | SDK method |
---|---|
Get information about a call | get() |
Manage call with callLeg | manage_with_call_leg() |
Updates a call in progress | update() |
conferences
endpoint category
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:API operation | SDK method |
---|---|
Get information about a conference | get() |
Makes a Conference callout | call() |
Manage a conference participant | manage_participant() |
Remove a participant from a conference | kick_participant() |
Remove all participants from a conference | kick_all() |
applications
endpoint category
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:API operation | SDK method |
---|---|
Return all the numbers assigned to an application | get_numbers() |
Assign a number or list of numbers to an application | assign_numbers() |
Unassign a number from an application | unassign_number() |
Return the callback URLs for an application | get_callback_urls() |
Update the callback URL for an application | update_callback_urls() |
Returns information about a number | query_number() |
Request and query parameters
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()
Field name differences
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.
Nested objects
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 calloutdestination
configuration objects below. One is represented in JSON, the other as a Python dictionary:destination= {
"type": "number",
"endpoint": "YOUR_phone_number"
}
"destination": {
"type": "number",
"endpoint": "YOUR_phone_number"
}
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.Responses
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.