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.
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.
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:
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.
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"
)
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"
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"
)
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 |
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 |
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 |
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 |
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.
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 |
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.
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.
Any field labelled from
in the API is labelled as from_
in the Python SDK.