The Cat API- R Assignment 6

Author

Luke Peterson

Introduction

The purpose of this document is to demonstrate the use of The Cat Api and it’s functionality. The Cat Api allows users to users to retrieve information regarding cats including different breeds, facts regarding cats, different diet and health tips, etc.

This information could be useful for someone who is interested in learning more about different cat breeds before adopting a cat or simply wants to learn more about cats. The API also provides access to cat images and information about over 100 different breeds.

Setting up free account

Visit Getting Started | The Cat API

Sign up for an account

Receive email and get your API key

api_key <- "YOUR_API_KEY"

API Call

After loading the packages and defining the API key you must then set up your endpoints. Here I set up the endpoint to call to get the information regarding the Bengal cat breed.

api_key <- "YOUR_API_KEY"

cat_url <- "https://api.thecatapi.com/v1/"

breed_endpoint <- paste(cat_url, "breeds/beng", sep = "")

API Use

Now that we’ve setup our endpoints we can set up our main function for calling the Cat API. First I will call the breed information about the Bengal cat.

breed_response <- GET(
  breed_endpoint,
  add_headers("x-api-key" = api_key)
)

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

Now that we have both sets of data, create your data frame. I have included the breed name, origin, temperament, description, life expectancy, and weight.

bengal_df <- data.frame(
  Name = breed_data$name,
  Origin = breed_data$origin,
  Temperament = breed_data$temperament,
  Description = breed_data$description,
  Life_Expectancy = breed_data$life_span,
  Weight = breed_data$weight$metric
)

print(bengal_df)
data frame with 0 columns and 0 rows

Now that we have created our data frame, we can print the results to make sire we have done everything correctly.

print(bengal_df)
data frame with 0 columns and 0 rows