Sinch SDK for Python programming language
Project description
Sinch Python SDK
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.
Prerequisites
- Python in one of the supported versions - 3.9, 3.10, 3.11, 3.12
- pip
- Sinch account
Installation
You can install this package by typing:
pip install sinch
Getting started
Client initialization
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")
)
Products
Sinch client provides access to the following Sinch products:
- Numbers
- SMS
- Verification
- Voice API
- Conversation API (beta release)
Logging
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.
Sample apps
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'}
...
Handling exceptions
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
.
Custom HTTP client implementation
By default, two HTTP implementations are provided:
- Synchronous using
requests
HTTP library - Asynchronous using
aiohttp
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
License
This project is licensed under the Apache License. See the LICENSE file for the license text.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file sinch-1.0.0.tar.gz
.
File metadata
- Download URL: sinch-1.0.0.tar.gz
- Upload date:
- Size: 55.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.16
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 12b3c5bd91644b5919d92c7fb7eac1ef2c5599444bb1b9a4441cbea8366af86c |
|
MD5 | 2628315558eaae87eb3dec2827d816ff |
|
BLAKE2b-256 | 4dc7b8afe086a111faf5094f86865bc4f008abd9528328e99364349708600ad0 |
File details
Details for the file sinch-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: sinch-1.0.0-py3-none-any.whl
- Upload date:
- Size: 146.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.16
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 16a2eeddfb5720320f2c8236a2c8adf9297c679c728617dc2748009a7a37ea4e |
|
MD5 | 67e5cb9cec0fae7d453e5827e00b04a5 |
|
BLAKE2b-256 | 8f03e1eb6d11eec4135ceca058a8729a142b33f42b7c1c40893f2ed5fd1dedea |