[Consuming APIs with Python](http://www.apiconsf.com/apiconschedule#how-to-consume)

APIConSF 2014.05.27

Raymond Yee

[@rdhyee](http://twitter.com/rdhyee), [blog.dataunbound.com](http://blog.dataunbound.com),

raymond.yee@gmail.com

unglue.it and U.C. Berkeley


In [2]:
# I keep this as a cell in my title slide so I can rerun 
# it easily if I make changes, but it's low enough it won't
# be visible in presentation mode.
%run talktools


Abstract

Excited about the incredibly array of APIs but don't know how to use them? Although APIs serve many purposes, there are common approaches to consuming any APIs that you will learn in this workshop. We will survey the many types of available APIs and concentrate on a few important examples of APIs. In this workshop, we will use Python, a commonly used and powerful language for working with APIs. Even if you don't know Python, you'll still pick up any basic concepts that you can apply the next time you encounter another API to use.

Goal is to present an introduction on how to use APIs with a focus on using the Python programming language.

There are many APIs (over 11000 listed in API Directory of ProgrammableWeb) -- hence, it'd be impossible to cover how to consume all of them.

My Background

Pro Web 2.0 Mashups

I have been on the faculty for the School of Information, UC Berkeley and I do software engineering for https://unglue.it. I'll be writing again for programmableweb.com very soon. I'm very much interested in open data, open source, and teaching people about these subjects -- as well as creating tools for creative thinking.

Outline

working through examples:

What do you want to learn?

Let's take some notes:

https://etherpad.mozilla.org/apiswithpython

Geocoder.us

http://geocoder.us/

  • free for non-commercial use

Look at the following URL in a web browser for comparison:

http://rpc.geocoder.us/service/csv?address=1600+Pennsylvania+Ave,+Washington+DC

See my writeup on Geocoding for more info.


In [3]:
url = "http://rpc.geocoder.us/service/csv?address=1600+Pennsylvania+Ave,+Washington+DC"

In [4]:
# let's break out the address explicitly

import urllib

url = "http://rpc.geocoder.us/service/csv?" + \
        urllib.urlencode({
           'address':'1600 Pennsylvania Ave, Washington DC'
})
    
url


Out[4]:
'http://rpc.geocoder.us/service/csv?address=1600+Pennsylvania+Ave%2C+Washington+DC'

In [5]:
import requests
r = requests.get(url).content
r.split(",")


Out[5]:
['38.898748',
 '-77.037684',
 '1600 Pennsylvania Ave NW',
 'Washington',
 'DC',
 '20502\n']