Skip to content
Last updated

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

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 SMS API, including any differences that may exist between the API itself and the SDK. For a full reference on SMS API calls and responses, see the SMS API Reference.

The code sample below is an example of how to use the Python SDK to send a text message using the SMS API. We've also provided an example that accomplishes the same task using the REST API.

send-message.py

from sinch import SinchClient

sinch_client = SinchClient(
    key_id="YOUR_key_id",
    key_secret="YOUR_key_secret",
    project_id="YOUR_project_id"
)

send_batch_response = sinch_client.sms.batches.send(
    body="Hello from Sinch!",
    to=["YOUR_to_number"],
    from_="YOUR_Sinch_number",
    delivery_report="none"
)

print(send_batch_response)

This example highlights the following required to successfully make a SMS 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 successfully initialize the Sinch client class, you must provide a valid access key ID and access key secret combination. You must also provide your Project ID. For example:


from sinch import SinchClient

sinch_client = SinchClient(
    key_id="YOUR_key_id",
    key_secret="YOUR_key_secret",
    project_id="YOUR_project_id"
)

Configuration

After intializing the client, you can modify the following SMS properties using the configuration class:

PropertyDescription
sms_regionThe geographical location in which the server is located. Must set to either us or eu.
sms_domainThe URL (excluding the region) of the server. Do not change unless advised by your account manager.

You will mostly use the configuration class to detail the location of the server you'd like to interact with. For example:


sinch_client.configuration.sms_region="us"

SMS 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.sms.[endpoint_category].[method]. You can also create a domain-specific client from a general client. For example:


from sinch import SinchClient

sinch_client = SinchClient(key_id="YOUR_key_id", key_secret="YOUR_key_secret",
project_id="YOUR_project_id")

from sinch.domains.sms import SMS

SMS_client = SMS(sinch_client)

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

  • groups
  • batches
  • inbounds
  • delivery_reports

For example:

send_batch_response = sinch_client.sms.batches.send(
    body = "Hello from Sinch!",
    to = ["YOUR_to_number"],
    from_ = "YOUR_Sinch_number",
    delivery_report = "none"
)

batches endpoint category

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

inbounds endpoint category

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

groups endpoint category

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

API operationSDK method
List groupslist
Create a groupcreate
Retrieve a groupget
Update a groupupdate
Replace a groupreplace
Delete a groupdelete
Get phone numbers for a groupget_group_phone_numbers

delivery_reports endpoint category

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

Request and query parameters

Note:

Note that the service_plan_id path parameter does not need to be included in any requests created by the Python SDK.

Requests and queries made using the Python SDK are similar to those made using the SMS API. Many of the fields are named and structured similarly. In most cases, they are the same. For example, consider the representations of a SMS API app ID below. One field is represented in JSON, and the other is using our Python SDK:

batch_id = "{BATCH_ID}"

Note that the fields have the same name. Additionally, path parameters, request body parameters, and query parameters that are used in the API are all passed as arguments to the corresponding python method. For example, consider this example in which the get method of the batch class is invoked:


SMS_response = sinch_client.sms.batches.get("01GR4H81QVX78E06F8ETGQ1CZK")

When using the SMS API, service_plan_id and batch_id would be included as path parameters in the JSON payload. With the Python SDK, the batch_id parameter is included as an argument in the get method.

Field name differences

Below is a table detailing field names present in the SMS API and their modified counterparts in the SMS API Python SDK:

API field nameSDK field name
typetype_
fromfrom_
recipient_msisdnrecipient_number

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.

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 SMS API are returned as dictionaries by the Python SDK.

Note:

Any field labelled from in the API is labelled as from_ in the Python SDK.