# Sinch Python SDK for Voice 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](/docs/voice/getting-started/python-sdk/make-call) 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](/docs/voice/api-reference/voice). 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. SDK 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) REST API ```python 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) ``` This example highlights the following required to successfully make a Voice API call using the Sinch Python SDK: - [Client initialization](#client) - [Voice domain access](#voice-domain) - [Endpoint usage](#endpoint-categories) - [Field population](#request-and-query-parameters) ## 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](https://dashboard.sinch.com) and additionally add your [Voice app credentials](https://dashboard.sinch.com/voice/apps). ```python 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: ```python 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: ```Python 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: ```Python 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](/docs/voice/api-reference/voice/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](/docs/voice/api-reference/voice/callouts/callouts#callouts/callouts/request) | `text_to_speech()` | | [Makes a Conference callout](/docs/voice/api-reference/voice/callouts/callouts#callouts/callouts/request) | `conference()` | | [Makes a Custom callout](/docs/voice/api-reference/voice/callouts/callouts#callouts/callouts/request) | `custom()` | The `calls` category of the Python SDK corresponds corresponds to the [calls](/docs/voice/api-reference/voice/calls/) endpoint. The mapping between the API operations and corresponding Python methods are described below: | API operation | SDK method | | --- | --- | | [Get information about a call](/docs/voice/api-reference/voice/calls/calling_getcallresult) | `get()` | | [Manage call with `callLeg`](/docs/voice/api-reference/voice/calls/calling_managecallwithcallleg) | `manage_with_call_leg()` | | [Updates a call in progress](/docs/voice/api-reference/voice/calls/calling_updatecall) | `update()` | The `conferences` category of the Python SDK corresponds corresponds to the [conferences](/docs/voice/api-reference/voice/conferences/) endpoint. The mapping between the API operations and corresponding Python methods are described below: | API operation | SDK method | | --- | --- | | [Get information about a conference](/docs/voice/api-reference/voice/conferences/calling_getconferenceinfo) | `get()` | | [Makes a Conference callout](/docs/voice/api-reference/voice/callouts/callouts#callouts/callouts/t=request&path=&d=0/conferencecallout) | `call()` | | [Manage a conference participant](/docs/voice/api-reference/voice/conferences/calling_manageconferenceparticipant) | `manage_participant()` | | [Remove a participant from a conference](/docs/voice/api-reference/voice/conferences/calling_kickconferenceparticipant) | `kick_participant()` | | [Remove all participants from a conference](/docs/voice/api-reference/voice/conferences/calling_kickconferenceall) | `kick_all()` | The `applications` category of the Python SDK corresponds corresponds to the [configuration](/docs/voice/api-reference/voice/applications/) 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](/docs/voice/api-reference/voice/applications/configuration_getnumbers) | `get_numbers()` | | [Assign a number or list of numbers to an application](/docs/voice/api-reference/voice/applications/configuration_updatenumbers) | `assign_numbers()` | | [Unassign a number from an application](/docs/voice/api-reference/voice/applications/configuration_unassignnumber) | `unassign_number()` | | [Return the callback URLs for an application](/docs/voice/api-reference/voice/applications/configuration_getcallbackurls) | `get_callback_urls()` | | [Update the callback URL for an application](/docs/voice/api-reference/voice/applications/configuration_updatecallbackurls) | `update_callback_urls()` | | [Returns information about a number](/docs/voice/api-reference/voice/applications/calling_querynumber) | `query_number()` | 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: ```json "action": { "name": "hangup" } ``` But in the Python SDK, this is an object sent as a simple dict, as demonstrated by the following example: ```python "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: SDK ```python destination= { "type": "number", "endpoint": "YOUR_phone_number" } ``` JSON ```JSON "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.