Assignment 6 Learning a New API (PokeAPI) by Cole Tenfelde
Introduction
For this assignment, I chose to use the PokéAPI, a free web API that provides detailed information about Pokémon. I picked this API because I am a huge Pokémon fan and wanted to practice my skills in something I know a fair amount of information about. The PokéAPI includes a lot of information from all parts of the Pokémon video game world such as Pokémon moves, items, and individual Pokémon. I am going to demonstrate making an API call to gather Pokémon data to build my “Champion Team” box (a collection of 6 Pokémon. I challenge you to create a better team than me.
Why Someone Would Use This API
PokéAPI is useful because it gives a structured access to Pokémon data without needing to manually search through websites. This API could be used in many different ways from building Pokémon apps to learning specifics behind how Pokémon games work. PokéAPI is beginner friendly and does not require an API key to access.
Setting up the API
The first steps is to download any packages needed:
I used the following:
tidyverse - for data wrangling
jsonlite - for converting JSON into R objects
magrittr - for the piping operations
httr - for the GET request
How The API Works
The PokéAPI uses a URL structure:
base_url <- "https://pokeapi.co/api/v2/"End points can then be added to the end of this url to obtain specific information you want. For my assignment I used the endpoint “pokemon/{pokemon_name}”. This URL can be sent to the API using the GET() function and it would return many fields giving specifics about the Pokémon I searched for in a JSON format. There are many other endpoints that you can use found on the PokéAPI website.
Demonstration of an API Call
In my R script, I created a function called get_pokemon() that takes a Pokémon name as input, builds the URL, sends a GET request, converts the response from JSON into an R object, and extracts useful information into a dataframe. This data frame I built is the Pokémon I would have on my team if I was a Champion in a Pokémon game.
My function collected the following information for each Pokémon:
name
id
type(s)
height
weight
hp
attack
defense
special attack
special defense
speed
ability 1
ability 2
#Cole Tenfelde
#Assignment 6: Learning a new API
library(tidyverse)
library(jsonlite)
library(magrittr)
library(httr)
get_pokemon <- function(pokemon_name) {
poke_url <- paste0("https://pokeapi.co/api/v2/pokemon/", pokemon_name)
poke_url_response <-
poke_url %>%
GET()
data <- poke_url_response %>%
content(as = "text", encoding = "UTF-8") %>%
fromJSON()
ability_1 <- data$abilities$ability$name[1]
ability_2 <- data$abilities$ability$name[2]
# stats
hp <- data$stats$base_stat[data$stats$stat$name == "hp"]
attack <- data$stats$base_stat[data$stats$stat$name == "attack"]
defense <- data$stats$base_stat[data$stats$stat$name == "defense"]
sp_attack <- data$stats$base_stat[data$stats$stat$name == "special-attack"]
sp_defense <- data$stats$base_stat[data$stats$stat$name == "special-defense"]
speed <- data$stats$base_stat[data$stats$stat$name == "speed"]
tibble(
name = data$name,
id = data$id,
types = paste(data$types$type$name, collapse = ", "),
height = data$height,
weight = data$weight,
hp = hp,
attack = attack,
defense = defense,
sp_attack = sp_attack,
sp_defense = sp_defense,
speed = speed,
ability_1 = ability_1,
ability_2 = ability_2
)
}
pokemon_box <- bind_rows(
get_pokemon("zekrom"),
get_pokemon("dragapult"),
get_pokemon("mewtwo"),
get_pokemon("gouging-fire"),
get_pokemon("urshifu-rapid-strike"),
get_pokemon("corviknight")
)
pokemon_box
write_csv(pokemon_box, "pokemon_data.csv")Output
library(readr)
pokemon_data <- read_csv("pokemon_data.csv")
pokemon_dataConclusion
I used an API I was unfamiliar with to create custom URLs, send GET requests, convert in JSON data, and pull the information I wanted from the millions bits of data available in PokéAPI.