I need help making RapidAPI work with R. I keep getting errors when trying to use their sample code.
The original code they show looks like this:
library(httr)
response <- GET(
url = api_endpoint,
config = list("x-rapidapi-key" = my_key,
"x-rapidapi-host" = host_name,
useQueryString = TRUE),
date_param = "2020-01-15"
)
This fails because of the hyphen characters in the header names. I tried fixing it like this:
# Set up variables
api_key <- "my_secret_key" # actual key stored elsewhere
base_url <- "https://finance-data.p.rapidapi.com/market/v2/get-prices"
output_format <- "json"
# Build request URL
full_url <- paste0(base_url,
"?ticker=AAPL",
"&x-rapidapi-host:", api_host,
"&x-rapidapi-key:", api_key,
"&format=", output_format)
result <- httr::GET(full_url)
But I get a 401 unauthorized error even though my key works fine on the RapidAPI website.
I also tried this approach:
library(httr)
endpoint <- "https://finance-data.p.rapidapi.com/market/v2/get-prices"
params <- list(
ticker = "AAPL",
period = "1d"
)
api_call <- VERB("GET",
endpoint,
add_headers("x-rapidapi-host" = "finance-data.p.rapidapi.com",
"x-rapidapi-key" = "my_secret_key"),
query = params,
content_type("application/json"))
print(api_call)
print(content(api_call, "text"))
Still no luck. Anyone know what I’m doing wrong here?