Look up a phone number with Python
Read a phone number’s base information and two optional properties, checking each property’s status before using its value.
1. Prepare the workspace
Create a Bird API key with the Lookup scope and use a number you control in international format. Review Lookup pricing: the base lookup is billed, and requested optional properties are billed when delivered. This is a live lookup rather than a free sandbox request.
2. Install and configure
Use Python 3.10 or later. In a new project directory:
Codebeispiel
python3 -m venv .venv
source .venv/bin/activate
python -m pip install messagebird-sdk
export BIRD_API_KEY="YOUR_API_KEY"
export BIRD_TEST_PHONE="YOUR_PHONE_NUMBER"Replace the environment values with your own workspace credentials and test destination. The SDK selects the API region from the key. Run this example on your server or development machine.
3. Run the example
Save as lookup.py:
Codebeispiel
import os
from bird import Bird
with Bird(api_key=os.environ["BIRD_API_KEY"]) as client:
number = client.lookup.phone_number(
phone_number=os.environ["BIRD_TEST_PHONE"], type=["porting", "score"]
)
print(number.country_code, number.line_type)
if number.score is not None and number.score.status == "ok":
print("Credibility:", number.score.value)
if number.porting is not None and number.porting.status == "ok":
print("Ported:", number.porting.ported)Codebeispiel
python lookup.py4. Inspect the outcome
The script prints the country and line type, then prints an optional property only when its status is ok. Missing output for a requested property means this example did not receive an answered value; it does not mean false or zero.
Use the phone-number guide to interpret each field. Number information does not prove possession; use Verify when the customer must demonstrate control of the destination.
Next steps
Verwandte Ressourcen
Weiter mit der Dokumentation, Anleitungen und Beispielen zu diesem Thema. Die Ressourcen sind auf Englisch.
Anleitung ansehenPhone number lookup: check a number before you sendDas Konzept verstehenWhat does OTP mean? One-time passwords explainedDie Funktion erkundenPhone number lookupDem Lernpfad folgenBuild your first integration
Implementierungs-Briefing erhalten