Posted on July 30th, 2019
Python is a hot programming language these days. For example, it has become the premier language for use within the data science discipline, especially around machine learning. Probably the fastest growing language in the industry in terms of new users, it is used extensively for Web and database applications. It also finds a sweet spot in verticals such as finance, education, and mechanical engineering.
One of the key themes of the language is its high level of extensibility, as there is a rich set of libraries available to use when building applications, one of which we will use in this exercise.
Python is an interpreted language, meaning the code is executed at runtime rather than being compiled and delivered as an executable. Interpreters are available for many operating systems, including Linux, Windows, and MacOS.
It is not a new language, since the original codebase was conceived of in the late 1980s, and the heavily used version 2 has been around for a couple of decades. Version 3 is the latest iteration of Python, the most recent build and version available here for installation at www.python.org.
In this blog, we will demonstrate the straight-forward consumption of a queried Web API that returns data in JSON format. We will use of one of Interzoid's APIs for this example. In order to execute the code, you will have to register to obtain an API key from Interzoid, which only requires providing your email address.
In this easy example, we will use Interzoid's Global Telephone Information API. It simply requires an international telephone number (with country code) along with the API license key as input values, returning demographics about the number. Here are some example results from calling the API.
Here is a walkthrough of the code:
Assuming you have Python installed (if not, do that first), you will also want to install the "requests" library. You can do this by issuing the following command on your Python Command-line interface (CLI):
> pip install requests
Then, in our Python code, we must import the library:
import requests
Next, we will use the library to make the actual call to the API, storing the results into "response".
response =
requests.get('https://api.interzoid.com/getglobalnumberinfo?license=YOURAPIKEY&intlnumber=+4906979550')
Then, let's display some of the returned data so we get a sense of what we are working with.
print(response.text)
print(response.status_code)
Next, let's check the http status code before continuing to make sure we have received a successful code
of '200'.
if response.status_code == 200:
Upon success, we will parse the JSON response and then print one of the individual returned values:
data = response.json()
print(data["Country"])
That's it! Calling any other Interzoid APIs (and many other APIs) would be the same, with the only changes being the URL of the API endpoint, the correct input parameters, and whatever logic you want to employ to store or print the resultant data.
Here is the complete Python code:
import requests
response = requests.get('https://api.interzoid.com/getglobalnumberinfo?license=YOURAPIKEY&intlnumber=+4906979550')
print(response.text)
print(response.status_code)
if response.status_code == 200:
data = response.json()
print(data["Country"])