Trouble getting RapidAPI R code to function properly

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?

had the same issue a bit ago. just use add_headers() alone and keep your headers separate from url params. httr should manage the hyphens fine on its own, so this might help with the 401 error you’re facing.

You’re probably mixing up headers and parameters. I had the same issue with RapidAPI - headers need to go in the headers section, not mixed with your query stuff.

Try this:

library(httr)

response <- GET(
  url = "https://finance-data.p.rapidapi.com/market/v2/get-prices",
  add_headers(
    "X-RapidAPI-Key" = "your_actual_key_here",
    "X-RapidAPI-Host" = "finance-data.p.rapidapi.com"
  ),
  query = list(
    ticker = "AAPL",
    period = "1d"
  )
)

Two things to check: I capitalized the X in those header names because some APIs throw fits about lowercase. Also double-check your endpoint URL matches exactly what RapidAPI shows - different finance providers use different URLs even on the same platform.

Your second attempt was the closest. The main problem is you’re mixing headers with URL parameters in your first fix - RapidAPI headers should never go in the URL itself. For httr, try GET() directly with add_headers() instead of VERB(). I had the same auth issues until I found my API key had trailing whitespace from copying it wrong. Double-check your key doesn’t have hidden characters and make sure you’re using the exact host value from RapidAPI’s docs. Also check your subscription status for that endpoint - some need paid plans even with valid keys.