Developer Guide
Python integration guide
A minimal Telebase client in Python is a single function: build the request URL with the phone number URL-encoded, send it with a Bearer token, and parse the JSON response. This page covers that function, the three error responses worth handling explicitly, and why failed lookups should not be retried automatically.
How do you make a lookup call in Python?
Use the standard library's urllib.parse.quote to URL-encode the phone number, and requests for the HTTP call. The + in an E.164 number must become %2B; passing a raw + in a query string is interpreted as a space and will produce a failed or incorrect lookup.
import requests from urllib.parse import quote API_KEY = "tb_test_xxxxxxxxxxxxxxxxxxxxxxxx" BASE_URL = "https://app.telebase.io/v1/lookup" def lookup(phone_number: str) -> dict: encoded_number = quote(phone_number, safe="") response = requests.get( f"{BASE_URL}?phone={encoded_number}", headers={"Authorization": f"Bearer {API_KEY}"}, timeout=35, ) response.raise_for_status() return response.json() result = lookup("+447700900001") print(result["carrier"], result["numberType"], result["simSwap"])
quote(phone_number, safe="") handles the + to %2B conversion correctly, so there is no need to encode it manually.
How should you handle errors?
response.raise_for_status() will raise an HTTPError for any non-200 response, but it is worth catching that and branching on the status code so your onboarding or authentication flow can respond appropriately rather than surfacing a raw exception.
def lookup(phone_number: str) -> dict: encoded_number = quote(phone_number, safe="") response = requests.get( f"{BASE_URL}?phone={encoded_number}", headers={"Authorization": f"Bearer {API_KEY}"}, timeout=35, ) if response.status_code == 401: raise RuntimeError("Missing or malformed API key") if response.status_code == 400: raise ValueError(f"Invalid phone number: {phone_number}") if response.status_code == 402: raise RuntimeError("Insufficient funds, request a top-up in the dashboard") response.raise_for_status() # catches anything else non-200 return response.json()
What each status means
- 401: the key is missing, invalid, blocked or disabled, or the account needs production access. Check the header reads exactly
Bearer tb_test_...with one space after Bearer. - 400: the phone number failed validation, commonly a missing country code, a raw
+instead of%2B, or a number outside valid E.164 length. - 402: the prepaid balance is too low (
insufficient_funds). Request a top-up in Billing in the dashboard.
Should you retry failed lookups?
No, not automatically. Telebase has no idempotency key, so a repeated request is a new lookup and can be charged again. A 5xx response or a timeout does not prove that the lookup was free, and the provider can take up to 30 seconds to answer. Set a client timeout of at least 35 seconds, and if a request fails or times out, check the Check Log in the dashboard before deciding whether to send another lookup. Every error except 402 and 500 includes "chargedCents": 0.
Never retry 401, 400, 402 or 422 responses: fix the API key, the phone number format or the account balance instead. Retry a 429 only after the retryAfterSeconds it reports.
What does the response contain?
In every response
- simSwap: SWAPPED, NO_SWAP or UNKNOWN for a fixed 10-day window
- carrier: the network operator serving the number, or null
- country: ISO country code of the number, or null
- numberType: mobile, landline, tollFree, premium or null
- active: true when the number format is confirmed, otherwise null
- chargedCents: what the lookup cost in EUR cents
New to the API entirely? Start with the quickstart guide for the first request end to end.
Get sandbox access