Here you'll find documentation related to the Sinch Python SDK, including how to install it, initialize it, and start developing Python code using Sinch services.
To use Sinch services, you'll need a Sinch account and access keys. You can sign up for an account and create access keys at dashboard.sinch.com.
For more information on the Sinch APIs on which this SDK is based, refer to the official developer documentation portal.
- Python in one of the supported versions - 3.9, 3.10, 3.11, 3.12
- pip
- Sinch account
You can install this package by typing:
pip install sinch
To initialize communication with Sinch backed, credentials obtained from Sinch portal have to be provided to the main client class of this SDK. It's highly advised to not hardcode those credentials, but to fetch them from environment variables:
from sinch import SinchClient
sinch_client = SinchClient(
key_id="key_id",
key_secret="key_secret",
project_id="some_project",
application_key="application_key",
application_secret="application_secret"
)
import os
from sinch import SinchClient
sinch_client = SinchClient(
key_id=os.getenv("KEY_ID"),
key_secret=os.getenv("KEY_SECRET"),
project_id=os.getenv("PROJECT_ID"),
application_key=os.getenv("APPLICATION_KEY"),
application_secret=os.getenv("APPLICATION_SECRET")
)
Sinch client provides access to the following Sinch products:
- Numbers
- SMS
- Verification
- Voice API
- Conversation API (beta release)
Logging configuration for this SDK utilizes following hierarchy:
- If no configuration was provided via
logger_name
orlogger
configurable, SDK will inherit configuration from the root logger with theSinch
prefix. - If
logger_name
configurable was provided, SDK will use logger related to that name. For example:myapp.sinch
will inherit configuration from themyapp
logger. - If
logger
(logger instance) configurable was provided, SDK will use that particular logger for all its logging operations.
If all logging returned by this SDK needs to be disabled, usage of NullHanlder
provided by the standard logging
module is advised.
Usage example of the numbers
domain:
available_numbers = sinch_client.numbers.available.list(
region_code="US",
number_type="LOCAL"
)
Returned values are represented as Python dataclasses
:
ListAvailableNumbersResponse(
available_numbers=[
Number(
phone_number='+17862045855',
region_code='US',
type='LOCAL',
capability=['SMS', 'VOICE'],
setup_price={'currency_code': 'EUR', 'amount': '0.80'},
monthly_price={'currency_code': 'EUR', 'amount': '0.80'}
...
Each API throws a custom, API related exception for an unsuccessful backed call.
Example for Numbers API:
from sinch.domains.numbers.exceptions import NumbersException
try:
nums = sinch_client.numbers.available.list(
region_code="US",
number_type="LOCAL"
)
except NumbersException as err:
pass
For handling all possible exceptions thrown by this SDK use SinchException
(superclass of all Sinch exceptions) from sinch.core.exceptions
.
By default, two HTTP implementations are provided:
- Synchronous using
requests
HTTP library - Asynchronous using
httpx
HTTP library
For creating custom HTTP client code, use either SinchClient
or SinchClientAsync
client and inject your transport during initialisation:
sinch_client = SinchClientAsync(
key_id="Spanish",
key_secret="Inquisition",
project_id="some_project",
transport=MyHTTPAsyncImplementation
)
Custom client has to obey types and methods described by HTTPTransport
abstract base class:
class HTTPTransport(ABC):
@abstractmethod
def request(self, endpoint: HTTPEndpoint) -> HTTPResponse:
pass
This project is licensed under the Apache License. See the LICENSE file for the license text.