Created
January 31, 2024 14:25
-
-
Save RichardLitt/6b0e442dbc2fefa9f69fb153b8afd7d4 to your computer and use it in GitHub Desktop.
Get recent checklist feed in R
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Load necessary library | |
library(httr) | |
# Retrieve the eBird API Token from the environment variable | |
ebird_api_token <- Sys.getenv("EBIRD_API_TOKEN") | |
# Check if the token is not empty | |
if (nzchar(ebird_api_token) == FALSE) { | |
stop("eBird API token not found. Please make sure EBIRD_API_TOKEN is set.") | |
} | |
# API Endpoint | |
api_endpoint <- "https://api.ebird.org/v2/product/lists/US-VT-001?maxResults=200" | |
# Making the API call using GET from the httr package | |
response <- GET(url = api_endpoint, | |
add_headers(`x-ebirdapitoken` = ebird_api_token)) | |
# Check if the request was successful | |
if (http_status(response)$category != "success") { | |
stop("API request failed with status: ", http_status(response)$message) | |
} else { | |
cat("API request successful with status: ", http_status(response)$message, "\n") | |
} | |
# Saving the results to a JSON file | |
write(content(response, "text"), "results.json") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note: change 'success' to 'Success'.