In this short tutorial we will use Python in our Mac Terminal to consume one of the Machine Learning APIs in this list. Let’s pick DuckDuckGo.
DuckDuckGo lets us define people, places, things, words, and concepts. It also provides a list of related topics. This is very useful if you want to get some context on a certain text.
Although we’re picking DuckDuckGo, note that the steps below also applies to the rest of the APIs in the Machine Learning APIs list, and all Mashape APIs. That means you can swap in and swap out APIs for your applications easily.
Let’s get started with Python!
Step 1: Sign up for an account in Mashape here. Mashape is an API Marketplace where you can find new and useful APIs for your application. To access these APIs, you would need a Mashape account.
Step 2: Once you have signed up, login to Mashape and go to DuckDuckGo’s interactive API documentation here. You can try the test console by keying in a word (e.g. “Mashape”) on the “q” parameter, and hit “Test Endpoint”.
Test Console for Duck Duck Go API
The test console gives you a way to “preview” the response of the API endpoints before you actually code.
You should see a response similar to below:
Sample Response for Duck Duck Go API
Step 3: Now let’s get to the Python part. On the test console, you are given options to pick a client library of your preferred language. Let’s click on the Python link. It will show us the snippet of code to call this endpoint.
Python code snippet
The code snippet uses an open source library called Unirest. Without going into too much detail, Unirest makes it dead easy to make HTTP requests for a variety of programming languages. We will need to get this Unirest library to make an HTTP call in Python.
Step 4: We’ll get back to that Python code snippet in a bit. Let’s go to the link shown above to download the Unirest library for Python. It shows us how easy it is to install the library:
Installing Unirest Python Library
(Note: ”pip” is a utility to install Python packages. You can install it by following the instructions here.)
Step 5: We will type in the commands above in our Terminal. Here’s what our Terminal looks like after successfully installing Unirest.
Successful installation of Python Unirest Library
That’s not too hard isn’t it
Step 6: The last step is to go to Python and import the Unirest library and call the endpoint. These are mentioned in Steps 4 and 3 respectively. We’re going to use command-line Python below.
Calling API via command line Python
The whole chunk of response output in the picture above is the same as in Step 2. From here you can refer to the specific object attributes that you will use for the rest of your application.
And that’s it! To recap:
- We signed up for a Mashape account and tried the API endpoint using the Mashape test console
- We installed the Unirest library to make it easy for us to call the endpoint in Python
- We imported Unirest into our Python script and ran the snippet specified in the Mashape test console to get the output.
It is important to keep in mind that these concepts extend to other APIs in Mashape and to other programming languages.
We hope this post was useful!