Build a command line application that consumes a public API — here’s how!

Published Dec 28, 2017
Build a command line application that consumes a public API — here’s how!

A command line application, like the name suggests, is an executable that runs within a console/shell (terminal on Mac and cmd on Windows). There’s a massive chance that you’ve used one of these apps before; there’s a host of command line applications out there like pip, curl, git and Grep. Generally, a command line application will have subcommands (arguments) and options aka switches.

Everything you need to get you started

  1. Virtual Environment (virtualenv)
    Working with virtual environments is a habit you should get into ASAP. Virtualenvs do a great job in terms of isolating the various projects you may be working on. A virtualenv is basically an isolated copy of python and all the libraries you might need to run a particular project, and it keeps all the dependencies of all your projects independent of each other so that they don’t break because of backwards compatibility issues or conflicting dependencies. 
    To get started with virtualenv, use pip to install it. Alternatively, if you’re using Pycharm (which is amazing!) as you’re IDE, you’re covered as it ships with virtualenv. You can create a virtualenv either at project creation time or later on via the project interpreter settings.
  2. Command-Line parsing Library
    I like to think of these as frameworks; there’s a ton of them (like Clint, docopt, cliff, click…) but I’ve barely had anytime to explore them and have no solid basis for comparison. As such, for this article, I’ll speak solely of Click (created by Armin Ronacher) as it’s what I used to create my command line app.
    Click has great documentation as well as an exhaustive video tutorial that will get you up and running in under 5 minutes. I will definitely be using Click much more going forward; I’ll be sure to document whatever discoveries I may make.
    I recommend that you explore the many other parsing libraries out there and make an informed decision based on your project and it’s needs.
  3. The Python requests library
    For your application to consume data from a public API, you need to, well, find a public API. A quick google search should do the trick, but you can check out Open Weather Map and News API
    Now you need a way to request the API for data (most likely in json or xml format) which is where the appropriately named requests library comes in handy. Run the pip install requests command in your shell of choice to install the library. It’s documentation gives you a glance at all you can achieve with this library. In my experience, parsing json from a public API using the requests library should not take you more than three lines of code for a basic case.
  4. Requirements.txt file
    You will not need this in order to build a CLI app and you probably will not need to create one of these for a while, at least for as long as you’re building practice apps. A requirements.txt file is a text file with a list of the packages your app depends on (dependencies) at a given point in time. The different packages are written like so; click==6.7, each one on it’s own line.
    What’s the point of having requirements files, you ask, well, they really come into their own when you start to distribute your app to other people. The requirements file ensures that your executable is installed along with all the dependencies it requires to work.
    Apps can grow to multiple directories, thousands of lines of code and tons of dependencies; which is why it’s super cool that you don't have to manually create the requirements file. Run pip freeze > requirements.txt and a requirements file will be created in the current working directory.

What I built

The app I built (Vortex) fetches and parses json from the News API and processes it into formatted output showing a list of the latest news articles, their authors and descriptions. Granted, it’s quite basic and could use a ton of optimisation but I took the first step today, and I’ll be making many more of those.

Here’s a link to the public Github repo for the first app I built

As I harness more and more of Click’s power to make the app offer more variety and give more control to the user, I’ll share the highlights of that journey.

If you found this article the least bit helpful, hit the heart button and help me get it to more people who need help starting out with CLI applications. I’m on Twitter (@EdmondAtto) if you have any questions.

Discover and read more posts from Edmond B. Atto
get started