APIs

Chris Bail
Duke University

What Is an Application Programming Interface?

What Is an Application Programming Interface?

Growth of APIS

More than >22,000 APIs!

How Does an API Work?

 

Google Maps API Example

Anatomy of an API Call

Output of API call:

Designing your Own API Calls

Designing your Own API Calls

 

API Credentials

Example: Facebook API

Example: Facebook API

Rate Limiting

Rate Limiting

plot of chunk unnamed-chunk-1

Throttling

plot of chunk unnamed-chunk-2

An Example with Twitter's API

developer.twitter.com

Get Started

Create App

App Dashboard

App-lication

Callback URL

The Waiting Is the Hardest Part...

Your Developer Console

Keys and Access Tokens

The rtweet Package

 

Developed by the heroic Michael Kerney

install.packages("rtweet")

Define Your Credentials

# load rtweet

library(rtweet)

# create credentials as objects (these are FAKE CREDENTIALS)
# you need to replace them with your own.

api_key <- "aafghaioeriokjasfkljhsa"
api_secret_key <- "234897234kasdflkjashk"

# replace "my_awesome_app" with the name of YOUR app below

token <- create_token(
  app = "my_awesome_app",
  consumer_key = api_key,
  consumer_secret = api_secret_key)

# after you run the code above, a browser window will open asking you to authorize the app. 
# Once you do, you can begin making calls

Your First API Call

 

covid_19_tweets<-search_tweets("coronavirus", n=4000)

Browse the Results

 

names(covid_19_tweets)

Browse the Results

 

head(covid_19_tweets$text)

Browse the Results

Plot Results by Time

ts_plot(covid_19_tweets, "secs") +
  ggplot2::theme_minimal() +
  ggplot2::theme(plot.title = ggplot2::element_text(face = "bold")) +
  ggplot2::labs(
    x = NULL, y = NULL,
    title = "Frequency of Tweets about Covid-19 Around 1pm, May 3, 2020",
    subtitle = "Tweet counts aggregated by second",
    caption = "\nSource: Data collected from Twitter's REST API via rtweet"
  )

Plot Results by Time

Next, Let's Search by Location

 

covid_geo_tweets <- search_tweets("coronavirus",
  "lang:en", geocode = lookup_coords("usa"), 
  n = 3000, type="recent", include_rts=FALSE
  )
geocoded <- lat_lng(covid_geo_tweets)

Plot

 

par(mar = c(0, 0, 0, 0))
maps::map("world", lwd = .25)
with(geocoded, points(lng, lat, pch = 20, cex = .50, col = rgb(0, .3, .7, .75)))

Plot

 

Get Tweets from Individual Account

 

trump_tweets <- get_timelines(c("realDonaldTrump
"), n = 5)
head(trump_tweets$text)

Five Trump Tweets

 

Get General Information About a User

 

obama_twitter_profile <- lookup_users("BarackObama")

Browse Fields

 

obama_twitter_profile$description

Browse Fields

 

obama_twitter_profile$location

Browse Fields

 

obama_twitter_profile$followers_count

Get Users' Favorites

 

obama_favorites<-get_favorites("BarackObama", n=5)

Get Networks

 

watts_followers<-get_followers("duncanjwatts")

Check Rate Limits

 

rate_limits<-rate_limit()
head(rate_limits[50:55,1:4])

Check Rate Limits

 

Get Trending Topics by Location

 

get_trends("New York")

rtweet can even post tweets!

 

post_tweet("This Lecture is SICSS-xy")

 

Note: this is a very useful function if you are building a bot

Wrapping API Calls within a Loop

There Are R Packages for Other APIs

 

Here are a few: RgoogleMaps, googlelanguageR,rOpenSci,WDI,rOpenGov,rtimes

Many more are available but not yet on CRAN (install from github or using devtools)

A SICSS-sourced list of APIs for CSS

APIs Also do Analyses!

 

For example, visualization plotly

Challenges of Working with APIs