Application Programming Interfaces

Chris Bail
Duke University

What Is an Application Programming Interface (API)?

What Is an Application Programming Interface (API)?

Growth of APIS

A List of APIs

 

There are now nearly 20,000 APIs and counting:

https://www.programmableweb.com/apis/directory

How Does an API Work?

 

Simple Example with Google Maps API

Anatomy of an API Call

Output of API call:

Designing your Own API Calls

Designing your Own API Calls

 

API Credentials

Example: Facebook API

Rate Limiting

An Example with Twitter's API

Navigate to:

https://apps.twitter.com.

An Example with Twitter's API

Select Account Type and Describe Use

The Waiting Is the Hardest Part...

Your Developer Console

Callback URL

 

Keys and Access Tokens

The rtweet Package

 

install.packages("rtweet")

Define Your Credentials

 

app_name<-"YOURAPPNAMEHERE"
consumer_key<-"YOURKEYHERE"
consumer_secret<-"YOURSECRETHERE"
access_token<-"YOURACCESSTOKENHERE"
access_token_secret<-"YOURACCESSTOKENSECRETHERE"

Authenticate Yourself with Twitter API

 

library(rtweet)
create_token(app=app_name, consumer_key=consumer_key, consumer_secret=consumer_secret, access_token=access_token,
access_secret=access_token_secret,
set_renv = TRUE)

Your First API Call

 

korea_tweets<-search_tweets("#Korea", n=3000, include_rts = FALSE)

Browse the Results

 

names(korea_tweets)

Browse the Results

 

Browse the Results

 

head(korea_tweets$text)

Plot the Results

 

ts_plot(korea_tweets, "3 hours") +
  ggplot2::theme_minimal() +
  ggplot2::theme(plot.title = ggplot2::element_text(face = "bold")) +
  ggplot2::labs(
    x = NULL, y = NULL,
    title = "Frequency of Tweets about Korea from the Past Day",
    subtitle = "Twitter status (tweet) counts aggregated using three-hour intervals",
    caption = "\nSource: Data collected from Twitter's REST API via rtweet"
  )

Plot the Results

 

Next, Let's Search by Location

 

nk_tweets <- search_tweets("korea",
  "lang:en", geocode = lookup_coords("usa"), 
  n = 1000, type="recent", include_rts=FALSE
  )
geocoded <- lat_lng(nk_tweets)

Plot

 

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

Plot

 

Get Tweets from Individual Account

 

sanders_tweets <- get_timelines(c("sensanders"), n = 5)
head(sanders_tweets$text)

Get Tweets from Individual Account

 

Get General Information About a User

 

sanders_twitter_profile <- lookup_users("sensanders")

Browse Fields

 

sanders_twitter_profile$description

Browse Fields

 

sanders_twitter_profile$location

Browse Fields

 

sanders_twitter_profile$followers_count

Get Users' Favorites

 

sanders_favorites<-get_favorites("sensanders", n=5)
sanders_favorites$text

Get Users' Favorites

 

Get Networks

 

sanders_follows<-get_followers("sensanders")

Check Rate Limits

 

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

Check Rate Limits