library(tidyverse)
library(jsonlite)
library(magrittr)
library(httr) PokeAPI Tutorial
Introduction
In this tutorial we will be investigating and learning the Pokemon API that is publicly available at https://pokeapi.co/docs/v2. After learning the basic structure of the API and data set, we will be looking at a basic example of a GET() call to pull specific results that we find interesting from the data set. This data set is perfect as a starting point to learn API call functions, or is a great tool to explore trends and interactions between Pokemon types, skills, location, or other specific characteristics like height and gender!
Setting Up Your Environment
The libraries we will be using and you will want to run consist of the following below:
API Link and Setup
Provided on the website, we can find the general URL https://pokeapi.co/api/v2/, which will be the base URL we can use to find specific data from the API. Unlike other APIs, the Pokemon API uses slashes (/) to separate its conditions. The data set also provides its own link column to help you understand and modify the specific URL you want based on your goal.
Below we will first create our starting URL endpoint to build off of.
poke_endpoint <- "https://pokeapi.co/api/v2/"Calling the API
First we will take a look at the basic structure of the Pokemon data set by viewing the basic URL we just set up before.
json_test <-
poke_endpoint %>%
GET() %>%
content(as = "text",
encoding = "UTF-8") %>%
fromJSON()
names(json_test) [1] "ability" "berry"
[3] "berry-firmness" "berry-flavor"
[5] "characteristic" "contest-effect"
[7] "contest-type" "egg-group"
[9] "encounter-condition" "encounter-condition-value"
[11] "encounter-method" "evolution-chain"
[13] "evolution-trigger" "gender"
[15] "generation" "growth-rate"
[17] "item" "item-attribute"
[19] "item-category" "item-fling-effect"
[21] "item-pocket" "language"
[23] "location" "location-area"
[25] "machine" "meta"
[27] "move" "move-ailment"
[29] "move-battle-style" "move-category"
[31] "move-damage-class" "move-learn-method"
[33] "move-target" "nature"
[35] "pal-park-area" "pokeathlon-stat"
[37] "pokedex" "pokemon"
[39] "pokemon-color" "pokemon-form"
[41] "pokemon-habitat" "pokemon-shape"
[43] "pokemon-species" "region"
[45] "stat" "super-contest-effect"
[47] "type" "version"
[49] "version-group"
Above we can see the different data we can narrow in on depending on the findings we want to explore. At first it looks messy, but it lists the different information provided to us in this data set, with the exact link you would need to input to call the large amounts of data inside! After deciding what information we want to look at, we can easily use a sequence of use_series functions to narrow in on certain sets of data. For example, we see that if we just wanted to see the different Pokemon in the data set, the link we would need is https://pokeapi.co/api/v2/pokemon to enter that part of the data set, while only selecting the name column since the data set provides a column with their specific links as well.
Calling the API (Example)
In our example we will be pulling all of the names of the Pokemon who fall under the category of the fire type. Below is the code necessary to add the type indicator to the URL endpoint we already have, as well as the necessary use_series functions to navigate to the Pokemons names.
type <- "fire"
json_test_series <-
paste(poke_endpoint, "type/", type, sep = "") %>%
GET() %>%
content(as = "text",
encoding = "UTF-8") %>%
fromJSON() %>%
use_series(pokemon) %>%
use_series(pokemon) %>%
select(name)head(json_test_series, 15) name
1 charmander
2 charmeleon
3 charizard
4 vulpix
5 ninetales
6 growlithe
7 arcanine
8 ponyta
9 rapidash
10 magmar
11 flareon
12 moltres
13 cyndaquil
14 quilava
15 typhlosion
nrow(json_test_series)[1] 109
This call is displaying us the first 15 Pokemon in the data set that fall under the type fire to reduce clutter, as well as telling us the total amount of Pokemon that fall under this criteria. This call can be done to all the other criteria in the data set like location, skills, and gender!
Conclusion
Hopefully this tutorial provided you with the key basic knowledge to explore and pull data from the PokeAPI, which can be used for great practice using GET() functions and calls using JSON. This data set is also perfect for those interested in the data held on different Pokemon across multiple generations. Enjoy exploring!