Learning API: Rick and Morty

Author

Diana Ferra

Introduction

This is The Rick and Morty API.

Find the link HERE!

It let’s you look at data about:

  • the characters,

  • its episodes,

  • and different locations of each character.

This API is useful when wanting to practice working with APIs and transforming it into R format.

Loading Packages

To use this, you will want to load these packages:

library(tidyverse) 
library(jsonlite)  
library(magrittr)  
library(httr)     

Requesting

Today, we want to find human Rick alive, so let’s request the data from our URL:

#Build your URL
rickmorty_endpoint <- 
  "https://rickandmortyapi.com/api/character?"

#Remember, we want human Rick alive
name <-
  "name=rick"

status <-
  "&status=alive"

species <-
  "&species=human"

#This will get create the link:
rickmorty_api_url<-
  paste(rickmorty_endpoint,
        name,
        status,
        species,
        sep = "")

#Let's see it!
rickmorty_api_url
[1] "https://rickandmortyapi.com/api/character?name=rick&status=alive&species=human"

Awesome, we now have a URL! You can try inserting it into a search engine. (Psst.. I suggest checking ‘Pretty-print’).

GET() Request

Now, we want to send a GET() request to bring data to R.

#Send that GET() request
rickmorty_response <- 
  rickmorty_api_url %>% 
  GET()

# Did it work?
rickmorty_response$status_code 
[1] 200

A status_code of 200 tells us the request was…. SUCCESSFUL!

JSON to R

The API gives us data in JSON format, so next we will turn that API into R data.

HOW?!?!?

Like this:

rickmorty_api_data <-
  rickmorty_response %>%
  content(as = "text", 
          encoding = "UTF-8") %>%
  fromJSON()

Viewing Data

Let’s take a look at what we got.

rickmorty_api_data$results %>%
  view()

~ queue round of applause👏 ~

So far, it looks like our Rick’s are human, but are they alive?! Let’s see the data.

rickmorty_api_data$results$name
 [1] "Rick Sanchez"           "Black Rick"             "Cool Rick"             
 [4] "Cop Rick"               "Cowboy Rick"            "Mega Fruit Farmer Rick"
 [7] "Plumber Rick"           "Rick D716-B"            "Rick D716-C"           
[10] "Rick J-22"              "Rick K-22"              "Slow Rick"             
[13] "Teacher Rick"           "Baby Rick"              "Hairdresser Rick"      
[16] "Journalist Rick"        "Secret Service Rick"    "Steve Jobs Rick"       
[19] "Modern Rick"            "Rick Sanchez"          
rickmorty_api_data$results$status
 [1] "Alive" "Alive" "Alive" "Alive" "Alive" "Alive" "Alive" "Alive" "Alive"
[10] "Alive" "Alive" "Alive" "Alive" "Alive" "Alive" "Alive" "Alive" "Alive"
[19] "Alive" "Alive"
rickmorty_api_data$results$species
 [1] "Human" "Human" "Human" "Human" "Human" "Human" "Human" "Human" "Human"
[10] "Human" "Human" "Human" "Human" "Human" "Human" "Human" "Human" "Human"
[19] "Human" "Human"

Looks good 👍

Cleaning the Data

Helpful variables when looking for Rick are

  • ID

  • Name

  • Gender

  • Status

  • Species

The following code will help us focus on that:

rickmorty_df <-
  rickmorty_api_data %>% 
  use_series(results) %>% 
  select(id,
         name,
         gender,
         status,
         species)
rickmorty_df %>% 
  view()

Creating a Function

We want to get the most of this and functions help a lot, so let’s make a function that helps us retrieve data about our characters like Rick quickly.

rickmorty_GET_df <- 
  function(name_value, status_value, species_value) {
  
  rickmorty_endpoint <- 
    "https://rickandmortyapi.com/api/character?"
  
  name <- 
    paste("name=", 
          name_value, 
          sep = "")
  
  status <- paste("&status=",
                  status_value, 
                  sep = "")
  species <- paste("&species=",
                   species_value, 
                   sep = "")
  
  rickmorty_api_url <- paste(
    rickmorty_endpoint,
    name,
    status,
    species,
    sep = ""
  )
  
  rickmorty_df <- 
    rickmorty_api_url %>%
    GET() %>%
    content(as = "text", encoding = "UTF-8") %>%
    fromJSON() %>%
    use_series(results) %>%
    select(id, 
           name, 
           status, 
           species, 
           gender)
  
  return(rickmorty_df)
}

Testing Function

# Check it!
human_alive_ricks <- 
  rickmorty_GET_df("rick",
                   "alive",
                   "human")

human_alive_ricks %>% 
  view()

Conclusion

This tutorial hopefully helped to show how to interact with the Rick & Morty API using R.

We learned to:

  • Build API request

  • Retrieve JSON data

  • Convert data to R format

  • Create a function

This process can help you gather and analyze real-world data. Go make a difference!