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:
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:
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:
Property | Description |
---|---|
sms_region | The geographical location in which the server is located. Must set to either us or eu . |
sms_domain | The 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)
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:
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 operation | SDK method |
---|---|
Send | send |
List batches | list |
Dry run | send_dry_run |
Get a batch message | get |
Update a batch message | update |
Replace a batch | replace |
Cancel a batch message | cancel |
Send delivery feedback for a message | send_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 operation | SDK method |
---|---|
List incoming messages | list |
Retrieve inbound message | get |
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 operation | SDK method |
---|---|
List groups | list |
Create a group | create |
Retrieve a group | get |
Update a group | update |
Replace a group | replace |
Delete a group | delete |
Get phone numbers for a group | get_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 operation | SDK method |
---|---|
Retrieve a delivery report | get_for_batch |
Retrieve a recipient delivery report | get_for_number |
Retrieve a list of delivery reports | list |
Request and query parameters
Note:
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}"
"batch_id": "{BATCH_ID}"
get
method of the batch
class is invoked:SMS_response = sinch_client.sms.batches.get("01GR4H81QVX78E06F8ETGQ1CZK")
url = "https://us.SMS.api.sinch.com/v1/" + service_plan_id + "/batches/" + batch_id
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 name | SDK field name |
---|---|
type | type_ |
from | from_ |
recipient_msisdn | recipient_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:
from
in the API is labelled as from_
in the Python SDK.