Sinch Python SDK for Verification
The Sinch Verification Python SDK allows you to quickly interact with the Verification API from inside your Python applications. 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.
Syntax
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 itself.
Note:
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 on the side of this page is an example of how to use the Python SDK to initiatate an SMS PIN verification request. The Verification API call that accomplishes the same task is displayed below for reference:
import requests
applicationKey = "<REPLACE_WITH_APP_KEY>"
applicationSecret = "<REPLACE_WITH_APP_SECRET>"
to_number = '<REPLACE_WITH_YOUR_NUMBER>'
sinchVerificationUrl = "https://verification.api.sinch.com/verification/v1/verifications"
payload = {
"identity": {
"type": "number",
"endpoint": to_number
},
"method": "sms"
}
headers = {"Content-Type": "application/json"}
response = requests.post(
sinchVerificationUrl,
json=payload,
headers=headers,
auth=(applicationKey, applicationSecret)
)
data = response.json()
print(data)
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 Numbers API call using the Sinch Python SDK:
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 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"
)
Note:
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")
)
Verification 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.verification.[endpoint_category].[method]
. You can also create a domain-specific client from a general client. For example:from sinch import SinchClient
from sinch.domains.verification import Verification
sinch_client = SinchClient(
application_key="YOUR_application_key",
application_secret="YOUR_application_secret"
)
verification_client = Verification(sinch_client)
Endpoint categories
In the Sinch Python SDK, Verification API endpoints are accessible through the client (either a general client or a Verification-specific client). The naming convention of the endpoint's representation in the SDK matches the API:
verifications
verification_status
For example:
verification_response = sinch_client.verification.verifications.start_sms(
identity=VerificationIdentity(
type="number",
endpoint="YOUR_phone_number"
)
)
verifications
endpoint category
The verifications
category of the Python SDK corresponds to the verifications endpoint. The mapping between the API operations and corresponding Python methods are described below:API operation | SDK method |
---|---|
Start an SMS verification request | start_sms() |
Start a FlashCall verification request | start_flash_call() |
Start a phone call verification request | start_callout() |
Start a data verification request | start_seamless() |
Report a verification code by the identity of the recipient | report_by_identity() |
Report a verification code by the ID of the verification request | report_by_id() |
verification_status
endpoint category
The verification_status
category of the Python SDK corresponds to the verifications endpoint. The mapping between the API operations and corresponding Python methods are described below:API operation | SDK method |
---|---|
Get the status of a verification by the ID of the verification request | get_by_id() |
Get the status of a verification by the identity of the recipient | get_by_identity() |
Get the status of a verification by a reference value | get_by_reference() |
Request and query parameters
Requests and queries made using the Python SDK are similar to those made using the Verification API. Many of the fields are named and structured similarly.
There are some important distinctions to note. For example, consider the VerificationIdentity method. In the REST API,identity
is an object you populated with a type
string field and an endpoint
string field. But in the Python SDK this is a method and you pass the necessary fields as parameters, as demonstrated by the following example:identity=VerificationIdentity(type="number",endpoint="YOUR_phone_number")
Field name differences
When translating field names from the Verification 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.
Nested objects
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. 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.
Responses
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 Verification 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.