Sinch Python SDK for SMS

The Sinch SMS Python SDK allows you to quickly interact with the SMS 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 SMS API are structured similarly to those that are sent and received using the SMS API itself.

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 on the side of this page is an example of how to use the Python SDK to send a text message using the SMS API. The SMS API call that accomplishes the same task is displayed below for reference:

Copy
Copied
import requests

servicePlanId = "YOUR_service_plan_id"
apiToken = "YOUR_api_token"
sinchNumber = "YOUR_Sinch_number"
toNumber = "YOUR_recipient_number"
url = "https://us.sms.api.sinch.com/xms/v1/" + servicePlanId + "/batches"

payload = {
  "from": sinchNumber,
  "to": [
    toNumber
  ],
  "body": "Hello from Sinch!"
}

headers = {
  "Content-Type": "application/json",
  "Authorization": "Bearer " + apiToken
}

response = requests.post(url, json=payload, headers=headers)

data = response.json()
print(data)

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:

Copy
Copied
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:

Copy
Copied
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:
Copy
Copied
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)

Endpoint categories

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:

Copy
Copied
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:
API operationSDK method
Sendsend
List batcheslist
Dry runsend_dry_run
Get a batch messageget
Update a batch messageupdate
Replace a batchreplace
Cancel a batch messagecancel
Send delivery feedback for a messagesend_delivery_feedback

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:
API operationSDK method
List incoming messageslist
Retrieve inbound messageget

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:
API operationSDK method
Retrieve a delivery reportget_for_batch
Retrieve a recipient delivery reportget_for_number
Retrieve a list of delivery reportslist

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:

SDKJSON
Copy
Copied
batch_id = "{BATCH_ID}"
Copy
Copied
"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:
SDKJSON
Copy
Copied
SMS_response = sinch_client.sms.batches.get("01GR4H81QVX78E06F8ETGQ1CZK")
Copy
Copied
url = "https://us.SMS.api.sinch.com/v1/" + service_plan_id + "/batches/" + batch_id
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.
We'd love to hear from you!
Rate this content:
Still have a question?
 
Ask the community.