The Sinch Python SDK allows you to quickly interact with the Verification API from inside your Python applications. When using the Python SDK, the code representing requests and queries sent to and responses received from the Verification API are structured similarly to those that are sent and received using the Verification 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 Verification API, including any differences that may exist between the API itself and the SDK. For a full reference on Verification API calls and responses, see the Verification API Reference.
The code sample below is an example of how to use the Python SDK to initiate an SMS PIN verification request. We've also provided an example that accomplishes the same task using the REST API.
#This example initiates a SMS PIN verification request using the Python SDK.
from sinch import SinchClient
from sinch.domains.verification.models import VerificationIdentity
sinch_client = SinchClient(
application_key="YOUR_application_key",
application_secret="YOUR_application_secret"
)
response = sinch_client.verification.verifications.start_sms(
identity=VerificationIdentity(
type="number",
endpoint="YOUR_phone_number"
)
)
print(response)This example highlights the following required to successfully make a Verification 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 start using the SDK, you need to initialize the main client class with your credentials from your Sinch dashboard and additionally add your Verification app credentials.
from sinch import SinchClient
sinch_client = SinchClient(
application_key="YOUR_application_key",
application_secret="YOUR_application_secret"
)For testing purposes on your local environment it's fine to use hardcoded values, but before deploying to production we strongly recommend using environment variables to store the credentials, as in the following example:
import os
from sinch import SinchClient
sinch_client = SinchClient(
application_key=os.getenv("APPLICATION_KEY"),
application_secret=os.getenv("APPLICATION_SECRET")
)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.numbers.[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.numbers import Numbers
numbers_client = Numbers(sinch_client)
In the Sinch Python SDK, Numbers API endpoints are accessible through the client (either a general client or a Numbers-specific client). The naming convention of the endpoint's representation in the SDK matches the API:
availableactiveregions
For example:
numbers_available = sinch_client.numbers.available.list(
region_code="US",
number_type="LOCAL"
)
The available category of the Python SDK corresponds to the availableNumbers endpoint. The mapping between the API operations and corresponding Python methods are described below:
| API operation | SDK method |
|---|---|
| Rent the first available number matching the provided criteria | rent_any |
| Activate a new phone number | activate |
| Search for available phone numbers | list |
The active category of the Python SDK corresponds to the activeNumbers endpoint. The mapping between the API operations and corresponding Python methods are described below:
| API operation | SDK method |
|---|---|
| List active numbers for a project | list |
| Update active number | update |
| Retrieve active number | get |
| Release active number | release |
The regions category of the Python SDK corresponds to the availableRegions endpoint. The mapping between the API operations and corresponding Python methods are described below:
| API operation | SDK method |
|---|---|
| List available regions | list |
Requests and queries made using the Python SDK are similar to those made using the Numbers API. Many of the fields are named and structured similarly. For example, consider the representations of a Numbers API region code. One field is represented in JSON, and the other is using our Python SDK:
region_code = "US"Note that the fields are nearly the same. 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.
When translating field names from the Numbers API to the Python SDK, remember that many of the API field names are in camelCase, whereas the Python SDK field names are in snake_case. This pattern change manages almost all field name translations between the API and the SDK.
Below is a table detailing field names present in the Numbers API and their modified counterparts in the Numbers API Python SDK:
| API field name | SDK field name |
|---|---|
regionCode | region_code |
type | number_type |
types | number_types |
numberPattern.pattern | number_pattern |
numberPattern.searchPattern | number_search_pattern |
phoneNumber | phone_number |
smsConfiguration | voice_configuration |
capability | capabilities |
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 sms configuration objects below. One is represented in JSON, the other as a Python dictionary:
sms_configuration = {
"servicePlanId": "service_plan_string"
}Note that, in both cases, the servicePlanId object is structured in exactly the same way as they would be in a normal Python call to the Numbers 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.
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 Numbers API are returned as dictionaries by the Python SDK. Additionally, if there are any responses that differ significantly from the API responses, we note them in the endpoint category documentation.