style {` .panel-gray{ background-color: #CEE9F9; padding: 10px 10px 5px 10px; color:#000000; text-align: center;max-width: 1100px; border-radius:4px;font-family: DMSans, Helvetica, Arial, sans-serif; } `} div b Some features of this SDK are still in development. Consult with our [online support team](mailto:onlineteam@sinch.com) if you run into issues using this SDK in a production environment. br # Sinch Python SDK for Conversation The Sinch Python SDK allows you to quickly interact with the from inside your Python applications. When using the Python SDK, the code representing requests and queries sent to and responses received from the are structured similarly to those that are sent and received using the . The fastest way to get started with the SDK is to check out our [getting started](/docs/conversation/getting-started/python-sdk/send-message) 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 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](/docs/conversation/api-reference/). The code sample below is an example of how to use the Python SDK to send a text message on the SMS channel of a Conversation API app. The Conversation API call that accomplishes the same task is displayed below for reference. SDK 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" ) # conversation_region must be set to either us or eu sinch_client.configuration.conversation_region="us" send_conversation_api_message_response = sinch_client.conversation.message.send( app_id="YOUR_app_id", recipient={ "identified_by" : { "channel_identities" : [ {"identity":"RECIPIENT_number","channel" : "SMS"} ] } }, message={ "text_message" : { "text" : "Text message from Sinch Conversation API." } }, channel_properties={ "SMS_SENDER" : "YOUR_sms_sender" } ) print(send_conversation_api_message_response) REST API ```Json import requests import base64 appId = "YOUR_app_id" accessKey = "YOUR_key_id" accessSecret = "YOUR_key_secret" projectId = "YOUR_project_id" channel = "SMS" identity = "RECIPIENT_number" sender = "YOUR_sms_sender" url = "https://us.conversation.api.sinch.com/v1/projects/" + projectId + "/messages:send" data = accessKey + ":" + accessSecret encodedBytes = base64.b64encode(data.encode("utf-8")) accessToken = str(encodedBytes, "utf-8") payload = { "app_id": appId, "recipient": { "identified_by": { "channel_identities": [ { "channel": channel, "identity": identity } ] } }, "message": { "text_message": { "text": "Text message from Sinch Conversation API." } }, "channel_properties": { "SMS_SENDER": sender } } headers = { "Content-Type": "application/json", "Authorization": "Basic " + accessToken } response = requests.post(url, json=payload, headers=headers) data = response.json() print(data) ``` This example highlights the following required to successfully make a Conversation API call using the Sinch Python SDK: - [Client initialization](#client) - [Conversation domain access](#conversation-domain) - [Endpoint usage](#endpoint-categories) - [Field population](#request-and-query-parameters) ## 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: ```Python 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 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: ```Python sinch_client.configuration.conversation_region="us" ``` ## Conversation 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.conversation.[endpoint_category].[method]`. You can also create a domain-specific client from a general client. For example: ```Python 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: - `message` - `app` - `contact` - `event` - `transcoding` - `capability` - `template` - `webhook` - `conversation` For example: ```Python conversation_response = sinch_client.conversation.message.send( app_id="YOUR_app_ID", recipient={ "identified_by" : { "channel_identities" : [ {"identity":"RECIPIENT_number","channel" : "SMS"} ] } }, message={ "text_message" : { "text" : "Text message from Sinch Conversation API." } }, channel_properties={ "SMS_SENDER" : "YOUR_sms_sender" } ) ``` The `message` category of the Python SDK corresponds to the [messages](/docs/conversation/api-reference/conversation/messages/) endpoint. The mapping between the API operations and corresponding Python methods are described below: | API operation | SDK method | | --- | --- | | [Send a message](/docs/conversation/api-reference/conversation/messages/messages_sendmessage) | `send` | | [Get a message](/docs/conversation/api-reference/conversation/messages/messages_getmessage) | `get` | | [Delete a message](/docs/conversation/api-reference/conversation/messages/messages_deletemessage) | `delete` | | [List messages](/docs/conversation/api-reference/conversation/messages/messages_listmessages) | `list` | The `app` category of the Python SDK corresponds to the [apps](/docs/conversation/api-reference/conversation/app/) 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](/docs/conversation/api-reference/conversation/app/app_listapps) | `list` | | [Create an app](/docs/conversation/api-reference/conversation/app/app_createapp) | `create` | | [Get an app](/docs/conversation/api-reference/conversation/app/app_getapp) | `get` | | [Delete an app](/docs/conversation/api-reference/conversation/app/app_deleteapp) | `delete` | | [Update an app](/docs/conversation/api-reference/conversation/app/app_updateapp) | `update` | The `contact` category of the Python SDK corresponds to the [contacts](/docs/conversation/api-reference/conversation/contact/) endpoint. The mapping between the API operations and corresponding Python methods are described below: | API operation | SDK method | | --- | --- | | [List contacts](/docs/conversation/api-reference/conversation/contact/contact_listcontacts) | `list` | | [Create a contact](/docs/conversation/api-reference/conversation/contact/contact_createcontact) | `create` | | [Get a contact](/docs/conversation/api-reference/conversation/contact/contact_getcontact) | `get` | | [Delete a contact](/docs/conversation/api-reference/conversation/contact/contact_deletecontact) | `delete` | | [Update a contact](/docs/conversation/api-reference/conversation/contact/contact_updatecontact) | `update` | | [Merge two contacts](/docs/conversation/api-reference/conversation/contact/contact_mergecontact) | `merge` | | [Get channel profile](/docs/conversation/api-reference/conversation/contact/contact_getchannelprofile) | `get_channel_profile` | The `conversation` category of the Python SDK corresponds to the [conversations](docs/conversation/api-reference/conversation/conversation/) endpoint. The mapping between the API operations and corresponding Python methods are described below: | API operation | SDK method | | --- | --- | | [List conversations](/docs/conversation/api-reference/conversation/conversation/conversation_listconversations) | `list` | | [Create a conversation](/docs/conversation/api-reference/conversation/conversation/conversation_createconversation) | `create` | | [Get a conversation](/docs/conversation/api-reference/conversation/conversation/conversation_getconversation) | `get` | | [Delete a conversation](/docs/conversation/api-reference/conversation/conversation/conversation_deleteconversation) | `delete` | | [Update a conversation](/docs/conversation/api-reference/conversation/conversation/conversation_updateconversation) | `update` | | [Stop conversation](/docs/conversation/api-reference/conversation/conversation/conversation_stopactiveconversation) | `stop` | | [Inject messages](/docs/conversation/api-reference/conversation/conversation/conversation_injectmessage) | `inject_message_to_conversation` | The `event` category of the Python SDK corresponds to the [events](/docs/conversation/api-reference/conversation/events/) endpoint. The mapping between the API operations and corresponding Python methods are described below: | API operation | SDK method | | --- | --- | | [Send an event](/docs/conversation/api-reference/conversation/events/events_sendevent) | `send` | The `transcoding` category of the Python SDK corresponds to the [messages:transcode](/docs/conversation/api-reference/conversation/transcoding/) endpoint. The mapping between the API operations and corresponding Python methods are described below: | API operation | SDK method | | --- | --- | | [Transcode a message](/docs/conversation/api-reference/conversation/transcoding/transcoding_transcodemessage) | `transcode_message` | The `capability` category of the Python SDK corresponds to the [capability](/docs/conversation/api-reference/conversation/capability/) endpoint. The mapping between the API operations and corresponding Python methods are described below: | API operation | SDK method | | --- | --- | | [Capability lookup](/docs/conversation/api-reference/conversation/capability/capability_querycapability) | `query` | The `webhook` category of the Python SDK corresponds to the [webhooks](/docs/conversation/api-reference/conversation/webhooks/) endpoint. The mapping between the API operations and corresponding Python methods are described below: | API operation | SDK method | | --- | --- | | [List webhooks](/docs/conversation/api-reference/conversation/webhooks/webhooks_listwebhooks) | `list` | | [Create a new webhook](/docs/conversation/api-reference/conversation/webhooks/webhooks_createwebhook) | `create` | | [Get a webhook](/docs/conversation/api-reference/conversation/webhooks/webhooks_getwebhook) | `get` | | [Update an existing webhook](/docs/conversation/api-reference/conversation/webhooks/webhooks_updatewebhook) | `update` | | [Delete an existing webhook](/docs/conversation/api-reference/conversation/webhooks/webhooks_deletewebhook) | `delete` | The `template` category of the Python SDK corresponds to the [templates](/docs/conversation/api-reference/template/templates-v1/) endpoint. The mapping between the API operations and corresponding Python methods are described below: | API operation | SDK method | | --- | --- | | [List all templates belonging to a project ID](/docs/conversation/api-reference/template/templates-v1/templates_listtemplates) | `list` | | [Creates a template](/docs/conversation/api-reference/template/templates-v1/templates_createtemplate) | `create` | | [Updates a template](/docs/conversation/api-reference/template/templates-v1/templates_updatetemplate) | `update` | | [Get a template](/docs/conversation/api-reference/template/templates-v1/templates_gettemplate) | `get` | | [Delete a template](/docs/conversation/api-reference/template/templates-v1/templates_deletetemplate) | `delete` | 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: SDK ```Python app_id = "{APP_ID}" ``` REST API ```JSON "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 `message` class is invoked: SDK ```Python conversation_response = sinch_client.conversation.message.get( message_id="YOUR_message_id" messages_source="CONVERSATION_SOURCE") ``` REST API ```JSON url = "https://us.conversation.api.sinch.com/v1/projects/" + project_id + "/messages/" + YOUR_message_id payload = { "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. Below is a table detailing field names present in the Conversation API and their modified counterparts in the Conversation API Python SDK: | API field name | SDK field name | | --- | --- | | `metadata_json` | `conversation_metadata` | | `update_mask.paths` | `update_mask` | | `message.conversation_id` | `conversation_id` | | `from` | `from_` | | `default_translation` | `default_translations` | | `template.id` | `template_id` | 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: SDK ```Python message = { "text_message": { "text": "Text message from Sinch Conversation API." } } ``` REST API ```JSON "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: SDK ```Python recipient={ "identified_by" : { "channel_identities" : [ {"identity":"RECIPIENT_number","channel" : "SMS"} ] } } ``` REST API ```JSON "recipient": { "identified_by": { "channel_identities": [ {"identity":"RECIPIENT_number","channel" : "SMS"} ] } } ``` 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 Conversation API are returned as dictionaries by the Python SDK.