Using Python code to access an API

This page is aimed at showing journalists and others how to do something interesting and useful with Python code.

Specifically, it shows how to use a Python script to retrieve data from ProPublica’s Congress API about the 20 most recent bills sponsored by any current member of the U.S. Congress. It doesn’t teach much about coding per se. That would take a lot more than one web page can offer. But it will show you how to write, edit and run Python code using the point-and-click Anaconda coding platform and its built-in Jupyter Notebook application. Once you can do that, you’ll be ready to start fiddling with Python instead of fiddling with getting to the point where you can start fiddling with Python. That’s progress.

There are two prerequisites:

  • Download and install the (free) Anaconda platform on your computer. Installation procedures vary by operating system. Visit Anaconda’s Individual Edition installation page, scroll to the bottom of the page, and choose the Python 3.7 installer suitable for your system. There are separate installation guides for Windows, Mac OS, and Linux.
  • Visit the ProPublica Congress API page and complete and submit the “Request an API key” form to get your free API key. The key is a unique code that will arrive by e-mail, usually within miniutes. You will need it in order to get the Python script to work.

Once you have knocked out these two tasks, just watch each of the videos below, in order, and follow along. The videos have no sound, only on-screen captions. I wanted them to be usable in a variety of situations, not all of which are conducive to audio. I have embedded the videos to make them convenient to access. You may find details easier to see if you view the videos full screen, though, or open them on YouTube.


Part 1: Creating an Anaconda environment. The first thing you should do after starting Anaconda for the first time is create an “environment.” Creating an environment is sort of like creating a separate version of Anaconda that is dedicated just to whatever coding project you are working on. It’s good practice to create an environment for each coding project, or at least type of coding project, you work on. Keeping projects in separate environments helps keep them from conflicting with one another and causing program execution problems.

This video shows you how to set up an environment. The video assumes you have installed Anaconda and have launched it.


Part 2: installing Jupyter Notebook. Jupyter Notebook is an application within the Anaconda platform that lets you write, annotate, and run Python scripts. Python is quickly becoming one of the most popular coding languages around. Before Jupyter Notebook came along, you pretty much had to run Python from a computer’s command prompt. Jupyter Notebook is a much easier way to work with Python.

You have to install Jupyter Notebook in the environment you have created before you can open and use it. This video shows the steps.


Part 3: Testing Jupyter Notebook. The best way to find out whether everything has been set up correctly is to write and run a small Python program. It is a tradition among programmers to begin learning a new computer language by learning how to get the language to print the phrase, “Hello, world.” This video shows you how to do that.

Creating and running a small Python script seems to be especially important if you are running a Mac, especially an older Mac. Older Macs came with Python 2.7 (the old version of Python) preinstalled. Running the script shown in the video seems to help such Macs figure out that you want them to run the latest version of Python instead.


Part 4: Running the Congress API script. At last, here is what you came for. This video will show you how to copy, paste and run code from this Jupyter Notebook, which you’ll probably want to go ahead an open in a separate tab on your browser. As mentioned earlier, the code will let you:

  • Find the ID number for each member of any U.S. state’s delegation to either the U.S. Senate or the U.S. House of Representatives. As you may remember from civics class, each state has two U.S. senators, while the number of representatives it has in the U.S. House of Representatives is proportional to the state’s population.
  • Use the ID number of the senator or representative of your choice to look up the senator or representative’s bill sponsorship data.
  • Display the day on your computer screen, and also save the data to a text file on your computer.

As noted above, you will need your free API Key from ProPublica. You can get your key by visiting https://www.propublica.org/datastore/api/propublica-congress-api and completing and submitting the “Request an API Key” form. Your key will arrive by e-mail sent to whatever e-mail address you use when you complete the form.

Again, the video shows you how to copy and run code from the Jupyter Notebook at: https://drkblake.com/latest-bills-by-a-member-of-congress/.


Part 5: Using the output. Here’s a look at one practical thing you can do with the script’s output.