library(tidyverse)
library(httr)
library(jsonlite)
library(imager)The Cat API
API Analysis: The Cat API!
What is it?
The Cat API is an API that contains lots of data about cats, such as:
Cat Images
Breed Information
Facts and more detailed facts
- (Paid)
Medical Data
- (Paid & Not Currently Available)
This data, while it can be silly in nature, can be quite important in the pet care space. For example, a person looking to be a pet owner can query breed information and images to look for a breed they may want to adopt, a vet could use the soon-to-be-added medical data to quickly and more accurately diagnose pet conditions, and of course, can be used to produce many cute cat pictures.
How do you access it?
This API has some calls you can make without an API key, but getting a key is simple! You can obtain a free key which can be obtained at https://thecatapi.com.
By scrolling down, you can view the API key types here:
upon clicking the GET FREE ACCESS button, you are taken to page where you are asked to provide some information on your usage with the api
After putting in your information and clicking submit, and if you are approved, you should get an email with your new Cat API key!
Demonstration
Load our libraries
Without an API Key
The Cat Api actually has a few non-api-key requests! This is one of them, which can grab a random cat picture!
catInfo = "https://api.thecatapi.com/v1/images/search" %>%
GET() %>%
content(as = "text",
encoding = "UTF-8") %>%
fromJSON()
catImage = load.image(catInfo$url)
plot(catImage)Another thing you can do is grab a data set of all the breeds of cats!
catBreeds = "https://api.thecatapi.com/v1/breeds" %>%
GET() %>%
content(as = "text",
encoding = "UTF-8") %>%
fromJSON()
head(catBreeds,4)# A tibble: 4 × 38
id name cfa_url vetstreet_url vcahospitals_url temperament origin
<chr> <chr> <chr> <chr> <chr> <chr> <chr>
1 abys Abyssinian http:/… http://www.v… https://vcahosp… Active, En… Egypt
2 aege Aegean <NA> http://www.v… <NA> Affectiona… Greece
3 abob American Bobt… http:/… http://www.v… https://vcahosp… Intelligen… Unite…
4 acur American Curl http:/… http://www.v… https://vcahosp… Affectiona… Unite…
# ℹ 31 more variables: country_codes <chr>, country_code <chr>,
# description <chr>, life_span <chr>, indoor <dbl>, lap <dbl>,
# alt_names <chr>, adaptability <dbl>, affection_level <dbl>,
# child_friendly <dbl>, dog_friendly <dbl>, energy_level <dbl>,
# grooming <dbl>, health_issues <dbl>, intelligence <dbl>,
# shedding_level <dbl>, social_needs <dbl>, stranger_friendly <dbl>,
# vocalisation <dbl>, experimental <dbl>, hairless <dbl>, natural <dbl>, …
With an API Key
Getting an API allows us to apply filters to our image-getting commands. We would be able to get more detailed information and facts and sort by breeds and such, but that is sadly paid access.
siberianCatsURL = paste(
"https://api.thecatapi.com/v1/images/search?",
"limit=3", # how many images we want
"&breed_ids=sibe", # the breed we're filtering by, siberian cats
"&api_key=<!API_KEY_HERE!>", # put your api key here!
sep=""
)
siberianCats = siberianCatsURL %>%
GET() %>%
content(as = "text",
encoding = "UTF-8") %>%
fromJSON()Replacing <!API_KEY_HERE!> with your given api key would allow this code to run!
siberianCats = read_csv("3SiberianCats.csv") # to keep my api key private, I have imported my results into my own csvRows: 3 Columns: 5
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (2): id, url
dbl (2): width, height
lgl (1): breeds
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
siberianCats# A tibble: 3 × 5
breeds id url width height
<lgl> <chr> <chr> <dbl> <dbl>
1 NA LSaDk6OjY https://cdn2.thecatapi.com/images/LSaDk6OjY.jpg 1080 1080
2 NA Rl39SPjDO https://cdn2.thecatapi.com/images/Rl39SPjDO.png 1198 1379
3 NA ZocD-pQxd https://cdn2.thecatapi.com/images/ZocD-pQxd.jpg 880 1100
let’s display the 2nd cat picture as an example!
siberianCatImage = load.image(siberianCats$url[2])
plot(siberianCatImage)