Assignment 6: Charity Search API

Author

Elizabeth Fitzgerald

Charity search service

This database shares information on charities across the United States, such as their location, category, and tax-deductible eligibility. With this database, users are able to find charities that support causes they care about, charities near them they may want to contribute to, and easily access links to the charity’s website for donating.

How to get your own API key

To access this database’s data, go to https://orghunter.3scale.net/#plans and select which plan you want. Create an account, confirm your email, then wait for another email, which will contain your API key; you must activate it by clicking the link provided in this second email.

For this API, it is important to note that your personal API key is called “user_key” in the database.

Example API call

You can use this database to load charities from a specific city, category, tax eligibility, and more. In this example, you can load Cincinnati charities.

First, load the required packages in R, in order to complete any API call requests.

library(tidyverse) # For all the tidy things
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.1     ✔ tibble    3.2.1
✔ lubridate 1.9.4     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(jsonlite)  # For converting json data into data frames

Attaching package: 'jsonlite'

The following object is masked from 'package:purrr':

    flatten
library(magrittr)  # For extracting items from list objects using piping grammar

Attaching package: 'magrittr'

The following object is masked from 'package:purrr':

    set_names

The following object is masked from 'package:tidyr':

    extract
library(httr)      # For interacting with HTTP verbs

Then, create a function that will generate a URL that is filtered to charities only in a specific city.

get_charity_url <- 
  function(user_key,city,per_page) {
    charity_endpoint <- "https://data.orghunter.com/v1/charitysearch?" #define the endpoint
    
    user_key <- paste("user_key=",user_key, sep = "") #define your user key
    
    city <- paste("&city=", city, sep= "") #define your specific city
    
    per_page <- paste("&per_page=100",per_page,sep="") #return the highest number of results per page

    charity_url <- 
      paste(charity_endpoint,user_key,city,per_page) #create a url
    return(charity_url)
  }

Lastly, create a function that will put your URL’s data into a data frame.

charity_url_data <- 
  function(user_key,city,per_page) {
    charity_url <- 
      create_charity_url(user_key,city,per_page)
        charity_data <- 
      charity_url %>% 
      GET() %>% 
      content(as = "text",
              encoding = "UTF-8") %>% 
      fromJSON() %>% 
      use_series(data) #use series "data" instead of results for this specific API!
    return(charity_data)
  }