A Little About Command Line Apps That Consume Public APIs in Python

In the beginning… was the command line - Neal Stephenson

When coding was bare metal

So the first ever people to use computers only interacted with them via the terminal, or if you like, the command line. The black screen you see most devs, and yes, hackers, use.

It’s a nice tool devs use to do good work.

A lot of tutorials have documented how to use command line argument parsers like click, docopt, argparse and the likes. Today we are not going to talk about those. But how to write simple terminal apps that communicate with an external API lying somewhere across the internet.

To get started, find an API you want to consume or ‘eat’ from. I mean, get data from. A good place to find one is here.

If you don’t mind, a good one to start with is the openweathermap api, which allows you to query the current weather or weather forecast of a place, among several other interesting queries you can make.

For most of these api’s you need a ‘key’ to access them. Which you might probably have to sign up or create an account with them.

A key is basically a token the api makers use to validate your app’s request to consume their data. It’s also how they make money by limiting your app to only access their data a certain no of times. So users need to purchase/ upgrade your account to access the api more times a day.

But as a single user, don’t you worry, their ‘free’ is more than enough for you.

As you get the key, take some time to familiarise yourself with the api, how it gets queried, how it stores data (is it json or something), or how it returns data. This will go a long way when it comes to coding the app.

Once you have the key, now it’s time to write the code. Ssssh, we are not writing any code here today, am just guiding you and explaining some concepts.

The process is simple.

First, if it is your plan, you can ask the user to input some of the variables you’ll need to query the api with. For the openweathermap-api, such a variable may be the name of a ‘city’ you want to get its weather report.

But remember, if it’s not your plan to get user input, you can just provide this values as a default.

Use the variables you have to create a full query to the api. As an example, a full query to the OPW api to get Nairobi’s weather report might be something like "http://api.openweathermap.org/data/2.5/weather?q=Nairobi&APPID=YOUR-KEY-HERE"

Now it’s time to fetch data.

For Python, install, import and use the requests library. The process is simple again, use the requests library to send a request to the api using the supplied full url query. Then read the data and jsonify it (if it’s stored in json form as in the OPW case. That’s why it’s good to read the docs and know how the api makers store that data)

Store the jsonified data in a variable.

You can convert them into any other data structure you want them to be stored in.

Then simply display it!

Want me to share the code for this? Let me know in the comments!

Happy jsonifying!

Love this? Hit the green heart button below to recommend it to others too.