Join For Free

Consuming Machine Learning APIs with Python Posted 1 year ago


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!

  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.
  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”. 


    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:


  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.


    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.

  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:


    (Note:  ”pip” is a utility to install Python packages.  You can install it by following the instructions here.)

  5. We will type in the commands above in our Terminal.  Here’s what our Terminal looks like after successfully installing Unicorn.


    That’s not too hard isn’t it :)

  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.


    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 (not just the Machine Learning ones) and to other programming languages.  

We hope this post was useful!

  1. mashape posted this