You can quickly see how the Number Lookup API works by looking up a number using the API.
Before you can get started, you need the following already set up:
- Set all Number Lookup API configuration settings.
- Python and a familiarity with how to create a new file.
- PIP (package installer for Python) and a familiarity with how to install Python modules.
We'll be using the requests
module to make HTTP requests. If it's not already installed globally, open a command prompt and use the following command to install the requests
module:
pip install requests
Create a new file named app.py
and paste the provided "app.py" code found on this page into the file.
This tutorial uses basic authentication for testing purposes. We recommend using OAuth for authentication in a production environment. You can follow the steps in this guide, but use the code samples from here to use OAuth authentication instead.
# Use this code to look up a phone number using the Number Lookup API.
import requests
key = "YOUR_access_key"
secret = "YOUR_access_secret"
project_id = "YOUR_project_id"
number = "YOUR_phone_number"
url = "https://lookup.api.sinch.com/v2/projects/" + project_id + "/lookups"
def lookup(num):
payload = {
"number": num
}
headers = {
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers, auth=(key, secret))
data = response.json()
print(data)
lookup(number)
Before you can run the code, you need to update some values so you can connect to your Sinch account. Update the following parameters with your own values:
Assign your values to the following parameters:
Parameter | Your value |
---|---|
key | The access key found on your Sinch Build dashboard. |
secret | The access secret found on your Sinch Build dashboard. |
project_id | Your project ID found on your Sinch Build dashboard. |
number | The phone number that you want to look up in E.164 format. |
Save the file.
Now you can execute the code and look up your phone number. Run the following command:
python app.py
You should receive a response in your console with details about the phone number you specified.
If after running your app you receive a 5000 error response, you may have forgotten to save your file after adding your authentication values. This is an easy mistake to make! Try saving the file and running the app again.