The Sinch Python SDK allows you to quickly interact with the Number Lookup API from inside your Python applications. When using the Python SDK, the code representing requests and queries sent to and responses received from the Number Lookup API are structured similarly to those that are sent and received using the Number Lookup 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 Number Lookup API, including any differences that may exist between the API itself and the SDK. For a full reference on Number Lookup API calls and responses, see the Number Lookup API Reference.
The code sample on this page is an example of how to use the Python SDK to look up a phone number. We've also provided an example that accomplishes the same task using the REST API.
"""
Sinch Python Snippet
This snippet is available at https://github.com/sinch/sinch-sdk-python/tree/main/examples/snippets
"""
import os
from dotenv import load_dotenv
from sinch import SinchClient
load_dotenv()
sinch_client = SinchClient(
project_id=os.environ.get("SINCH_PROJECT_ID") or "MY_PROJECT_ID",
key_id=os.environ.get("SINCH_KEY_ID") or "MY_KEY_ID",
key_secret=os.environ.get("SINCH_KEY_SECRET") or "MY_KEY_SECRET",
)
# The phone number to look up in E.164 format (e.g. +1234567890)
phone_number = "PHONE_NUMBER"
response = sinch_client.number_lookup.lookup(number=phone_number)
print(f"Number lookup result:\n{response}")This example highlights the following required to successfully make a Number Lookup 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.
from sinch import SinchClient
sinch_client = SinchClient(key_id="key_id", key_secret="key_secret", project_id="YOUR_project_id")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(
key_id=os.getenv("KEY_ID"),
key_secret=os.getenv("KEY_SECRET"),
project_id=os.getenv("PROJECT_ID")
)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.number_lookup.[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.number_lookup import NumberLookup
number_lookup_client = NumberLookup(sinch_client)
In the Sinch Python SDK, Number Lookup API endpoints are accessible through the client (either a general client or a Number Lookup-specific client). The naming convention of the endpoint's representation in the SDK matches the API:
lookup
For example:
lookup_number = sinch_client.number_lookup.lookup(number="YOUR_PHONE_NUMBER")
The lookup category of the Python SDK corresponds to the lookup endpoint. The mapping between the API operations and corresponding Python methods are described below:
| API operation | SDK method |
|---|---|
| Look up a phone number | lookup |
Requests and queries made using the Python SDK are similar to those made using the Number Lookup API. Many of the fields are named and structured similarly. For example, consider the representations of a Number Lookup API region code. One field is represented in JSON, and the other is using our Python SDK:
mobile_country_code = "310"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 Number Lookup 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 Number Lookup API and their modified counterparts in the Number Lookup API Python SDK:
| API field name | SDK field name |
|---|---|
regionCode | region_code |
type | number_type |
types | number_types |
mobileCountryCode | mobile_country_code |
swapPeriod | swap_period |
phoneNumber | phone_number |
portingDate | porting_date |
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 Number Lookup 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.