Trouble implementing RapidAPI R code - getting authentication errors

I’m having problems getting the R code from RapidAPI to function properly. The sample code they give me looks like this:

library(httr)

GET(
  url = api_endpoint,
  config = list(x-rapidapi-key = MY_KEY,
                x-rapidapi-host = HOST_NAME,
                useQueryString = TRUE),
  datefilter = "2020-01-15"
)

This doesn’t work because the variable names have dashes in them which breaks R syntax. I tried to fix it by changing the approach:

# Set up variables
api_key <- "my_secret_key" # actual key defined elsewhere
base_url <- "https://stock-data.p.rapidapi.com/v2/historical-prices"
output_format <- "json"

# Build the request URL
full_url <- paste(base_url,
                  "?symbol=AAPL",
                  "&x-rapidapi-host:", api_host,
                  "&x-rapidapi-key:", api_key,
                  "&format=", output_format, sep="")
httr::GET(full_url)

But I keep getting 401 unauthorized errors even though my API key works fine when I test it directly on the RapidAPI website. I also tried this alternative method:

library(httr)

endpoint <- "https://stock-data.p.rapidapi.com/v2/historical-prices"

params <- list(
  ticker = "AAPL",
  market = "NASDAQ"
)

api_response <- VERB("GET",
                    endpoint,
                    add_headers(x_rapidapi_host = 'stock-data.p.rapidapi.com',
                               x_rapidapi_key = 'my_secret_key'),
                    query = params,
                    content_type("application/octet-stream"))

print(api_response)
content(api_response, "text")

Still no luck. Anyone know what I’m doing wrong here?

You’re mixing up headers with query parameters. RapidAPI needs specific header formatting, and you can’t put headers like x-rapidapi-key in the URL - they only go in request headers.

Try this:

library(httr)

response <- GET(
  url = "https://stock-data.p.rapidapi.com/v2/historical-prices",
  add_headers(
    "X-RapidAPI-Key" = "your_actual_key_here",
    "X-RapidAPI-Host" = "stock-data.p.rapidapi.com"
  ),
  query = list(
    symbol = "AAPL",
    format = "json"
  )
)

Use the exact header names from RapidAPI’s docs (usually capitalized) and check that your API key hasn’t expired or hit rate limits. I’ve seen this same issue before - mixing headers with query parameters always breaks authentication.

check if ur api key’s still active - rapidapi kills keys after they sit unused for a while. also verify u’re actually subscribed to that endpoint since some apis have different tiers. your code looks good, but i’d test it with curl first to see if the api’s working before diving into r debugging.

It’s your header formatting. RapidAPI headers need underscores with add_headers(), not hyphens. I hit this same issue last year and wasted hours on it.

Try this:

library(httr)

response <- GET(
  "https://stock-data.p.rapidapi.com/v2/historical-prices",
  add_headers(
    `X-RapidAPI-Key` = "your_key_here",
    `X-RapidAPI-Host` = "stock-data.p.rapidapi.com"
  ),
  query = list(symbol = "AAPL")
)

See the backticks around header names? That’s how you handle hyphens properly. Also check you’re using the exact host name from RapidAPI’s docs - they sometimes use different subdomains than expected. Make sure your subscription covers historical data endpoints too.