The Sinch Python SDK allows you to quickly interact with the Conversation API from inside your Python applications. When using the Python SDK, the code representing requests and queries sent to and responses received from the Conversation API are structured similarly to those that are sent and received using the Conversation 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 Conversation API, including any differences that may exist between the API itself and the SDK. For a full reference on Conversation API calls and responses, see the Conversation API Reference.
The code sample below is an example of how to use the Python SDK to send an SMS text message. The Conversation API call that accomplishes the same task is displayed below for reference.
"""
Sinch Python Snippet
This snippet is available at https://github.com/sinch/sinch-sdk-python/tree/main/examples/snippets
"""
import os
from dotenv import load_dotenv
from sinch import SinchClient
load_dotenv()
sinch_client = SinchClient(
project_id=os.environ.get("SINCH_PROJECT_ID") or "MY_PROJECT_ID",
key_id=os.environ.get("SINCH_KEY_ID") or "MY_KEY_ID",
key_secret=os.environ.get("SINCH_KEY_SECRET") or "MY_KEY_SECRET",
conversation_region=os.environ.get("SINCH_CONVERSATION_REGION") or "MY_CONVERSATION_REGION"
)
# The ID of the Conversation App to send the message from
app_id = "CONVERSATION_APP_ID"
# The phone number of the recipient in E.164 format (e.g. +46701234567)
recipient_identities = [
{
"channel": "SMS",
"identity": "RECIPIENT_PHONE_NUMBER"
}
]
response = sinch_client.conversation.messages.send_text_message(
app_id=app_id,
text="[Python SDK: Conversation] Sample text message",
recipient_identities=recipient_identities
)
print(f"Successfully sent text message.\n{response}")This example highlights the following required to successfully make a Conversation 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 Conversation properties using the configuration class:
| Property | Description |
|---|---|
conversation_region | The geographical location in which the conversation server is located. Must set to either us or eu. |
conversation_domain | The URL (excluding the region) of the conversation server. Do not change unless advised by your account manager. |
templates_region | The geographical location in which the templates server is located. Must set to either us or eu. |
templates_domain | The URL (excluding the region) of the templates 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.conversation_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.conversation.[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.conversation import Conversation
conversation_client = Conversation(sinch_client)
In the Sinch Python SDK, Conversation API endpoints are accessible through the client (either a general client or a Conversation-specific client). The naming convention of the endpoint's representation in the SDK matches the API:
conversation.appsconversation.capabilityconversation.consentsconversation.contactsconversation.conversationsconversation.event_destinationsconversation.eventsconversation.messagesconversation.project_settingsconversation.templatesconversation.transcoding
To validate and parse incoming events, use conversation.sinch_events(). For more information, see Sinch Events.
For example:
app_id = "CONVERSATION_APP_ID"
recipient_identities = [
{
"channel": "SMS",
"identity": "RECIPIENT_PHONE_NUMBER"
}
]
response = sinch_client.conversation.messages.send_text_message(
app_id=app_id,
text="[Python SDK: Conversation] Sample text message",
recipient_identities=recipient_identities
)
The conversation.apps category of the Python SDK corresponds to the apps endpoint. The mapping between the API operations and corresponding Python methods are described below:
| API operation | SDK method |
|---|---|
| List all apps for a given project | list |
| Create an app | create |
| Get an app | get |
| Update an app | update |
| Delete an app | delete |
The conversation.capability category of the Python SDK corresponds to the capability endpoint. The mapping between the API operations and corresponding Python methods are described below:
| API operation | SDK method |
|---|---|
| Capability lookup | lookup |
The conversation.consents category of the Python SDK corresponds to the consent endpoint. The mapping between the API operations and corresponding Python methods are described below:
| API operation | SDK method |
|---|---|
| Get identities from consent list | list_identities |
| Get audit records from consent lists | list_audit_records |
The conversation.contacts category of the Python SDK corresponds to the contacts endpoint. The mapping between the API operations and corresponding Python methods are described below:
| API operation | SDK method |
|---|---|
| List contacts | list |
| Create a contact | create |
| Get a contact | get |
| Update a contact | update |
| Delete a contact | delete |
| Merge two contacts | merge_contact |
| Get channel profile | get_channel_profile |
| List contact identity conflicts | list_identity_conflicts |
Additionally, there are two helper methods for getting a channel profile:
| API operation | SDK method |
|---|---|
| Get channel profile by contact ID | get_channel_profile_by_contact_id |
| Get channel profile by channel identity | get_channel_profile_by_channel_identity |
The conversation.conversations category of the Python SDK corresponds to the conversations endpoint. The mapping between the API operations and corresponding Python methods are described below:
| API operation | SDK method |
|---|---|
| List conversations | list |
| Create a conversation | create |
| Get a conversation | get |
| Update a conversation | update |
| Delete a conversation | delete |
| List recent conversations | list_recent |
| Stop an active conversation | stop_active |
| Inject a message | inject_message |
| Inject an event | inject_event |
The conversation.event_destinations category of the Python SDK corresponds to the webhooks endpoint. The mapping between the API operations and corresponding Python methods are described below:
| API operation | SDK method |
|---|---|
| List webhooks | list |
| Create a new webhook | create |
| Get a webhook | get |
| Update an existing webhook | update |
| Delete an existing webhook | delete |
The conversation.events category of the Python SDK corresponds to the events endpoint. The mapping between the API operations and corresponding Python methods are described below:
| API operation | SDK method |
|---|---|
| Send an event | send |
The conversation.messages category of the Python SDK corresponds to the messages endpoint. The mapping between the API operations and corresponding Python methods are described below:
| API operation | SDK method |
|---|---|
| Send a message | send |
| Get a message | get |
| Delete a message | delete |
| List messages | list |
| Update message metadata | update |
| List messages by channel identity | list_last_messages_by_channel_identity |
Additionally, there are several helper methods for sending messages of the different media types:
| API operation | SDK method |
|---|---|
| Send a card message | send_card_message |
| Send a carousel message | send_carousel_message |
| Send a choice message | send_choice_message |
| Send a contact info message | send_contact_info_message |
| Send a list message | send_list_message |
| Send a location message | send_location_message |
| Send a media message | send_media_message |
| Send a template message | send_template_message |
| Send a text message | send_text_message |
The conversation.project_settings category of the Python SDK corresponds to the project settings endpoint. The mapping between the API operations and corresponding Python methods are described below:
| API operation | SDK method |
|---|---|
| Create project settings | create |
| Get project settings | get |
| Update project settings | update |
| Delete project settings | delete |
The conversation.templates category of the Python SDK corresponds to the Templates V2 endpoint. The mapping between the API operations and corresponding Python methods are described below:
| API operation | SDK method |
|---|---|
| List templates | list |
| Create a template | create |
| Get a template | get |
| Update a template | update |
| Delete a template | delete |
| List translations for a template | list_translations |
The conversation.transcoding category of the Python SDK corresponds to the transcoding endpoint. The mapping between the API operations and corresponding Python methods are described below:
| API operation | SDK method |
|---|---|
| Transcode a message | transcode_message |
The conversation.sinch_events() method returns a handler for the events that the Conversation API sends to your event destination. Pass the secret configured for your event destination to validate signatures:
| Operation | SDK method |
|---|---|
| Validate the event signature | validate_authentication_header |
| Parse an event into a typed model | parse_event |
For example:
sinch_events_service = sinch_client.conversation.sinch_events("YOUR_callback_secret")
if not sinch_events_service.validate_authentication_header(
headers=headers,
json_payload=raw_body,
):
# Reject the request
...
event = sinch_events_service.parse_event(raw_body, headers)
Requests and queries made using the Python SDK are similar to those made using the Conversation API. Many of the fields are named and structured similarly. In most cases, they are the same. For example, consider the representations of a Conversation API app ID below. One field is represented in JSON, and the other is using our Python SDK:
app_id = "{APP_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 messages category is invoked:
conversation_response = sinch_client.conversation.messages.get(
message_id="YOUR_message_id",
messages_source="CONVERSATION_SOURCE")
When using the Conversation API, message_id would be included as a path parameter, and messages_source would be included as a query parameter in the JSON payload. With the Python SDK, both parameters are included as arguments in the get method.
For update methods, the SDK builds the update_mask from the arguments you pass. Arguments you omit are left unchanged on the server. Passing None explicitly clears the field.
The SDK uses the same field names as the Conversation API, with one exception. from is a reserved word in Python, so the SDK uses from_:
| API field name | SDK field name |
|---|---|
from | from_ |
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 message objects below. One is represented in JSON, the other as a Python dictionary:
message = {
"text_message": {
"text": "Text message from Sinch Conversation API."
}
}Note that, in both cases, the text_message and text objects are structured in exactly the same way as they would be in a normal Python call to the Conversation 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. This is also highlighted in the below example:
recipient={
"identified_by" : {
"channel_identities" : [
{"identity":"RECIPIENT_number","channel" : "SMS"}
]
}
}
Response fields match the API responses. They are delivered as typed Python objects (Pydantic models), with each field, including nested objects, represented as a property.



