Skip to content

Instantly share code, notes, and snippets.

@RichardLitt
Created January 31, 2024 14:25
Show Gist options
  • Save RichardLitt/6b0e442dbc2fefa9f69fb153b8afd7d4 to your computer and use it in GitHub Desktop.
Save RichardLitt/6b0e442dbc2fefa9f69fb153b8afd7d4 to your computer and use it in GitHub Desktop.
Get recent checklist feed in R
# 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")
@RichardLitt
Copy link
Author

Note: change 'success' to 'Success'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment