Introducing our Snowflake Data Cloud Native Application: AI-Driven Data Quality built into SQL statements! Learn More

Consuming an API with the Python Programming Language

by Interzoid Team


Posted on July 30th, 2019


Python Code Example of Consuming an API or Microservice using REST or JSON

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"])


See our Snowflake Native Application. Achieve Data Quality built-in to SQL statements.
Identify inconsistent and duplicate data quickly and easily in data tables and files.
More...
Connect Directly to Cloud SQL Databases and Perform Data Quality Analysis
Achieve better, more consistent, more usable data.
More...
Try our Pay-as-you-Go Option
Start increasing the usability and value of your data - start small and grow with success.
More...
Launch Our Entire Data Quality Matching System on an AWS EC2 Instance
Deploy to the instance type of your choice in any AWS data center globally. Start analyzing data and identifying matches across many databases and file types in minutes.
More...
Free Usage Credits
Register for an Interzoid API account and receive free usage credits. Improve the value and usability of your strategic data assets now.
Automate API Integration into Cloud Databases
Run live data quality exception and enhancement reports on major Cloud Data Platforms direct from your browser.
More...
Check out our APIs and SDKs
Easily integrate better data everywhere.
More...
Example API Usage Code on Github
Sample Code for invoking APIs on Interzoid in multiple programming languages
Business Case: Cloud APIs and Cloud Databases
See the business case for API-driven data enhancement - directly within your important datasets
More...
Documentation and Overview
See our documentation site.
More...
Product Newsletter
Receive Interzoid product and technology updates.
More...