httr GET returning "No API key found in headers or querystring"

I'm new to httr and jsonlite and API calls in general. I'm trying to obtain CO2 data from here: https://docs.co2signal.com/#introduction
I'm receiving the "No API key found in headers or querystring" message. I might be doing something silly. I note that the API documentation mentions that:
"CO2 Signal expects the API key to be included as a header in all requests to the server" I'm not sure if this means I should not use the authenticate function.

Code:

install.packages("httr")
install.packages("jsonlite")

#Require the package so you can use it
require("httr")
require("jsonlite")

auth_token <-  "xxxxx"    # I'm using my proper API key here
password <- ""

url <- "https://api.co2signal.com/v1/latest"
country_code <- "DE"

# The below may not be the correct format
# de_call <- paste(url, "?","countryCode","=", country_code, "%20&amp;", "auth-token=", auth_token, sep="")
de_call <- paste(url, "?","countryCode","=", country_code, sep="")
de_call

get_de_co2 <- GET(de_call, authenticate(auth_token, password, type = "basic"))

get_co2s_text <- content(get_de_co2, "text")
get_co2s_text

From the API doc, it seems like token is expected in the header of the request in the auth-token field.

CO2 Signal expects the API key to be included as a header in all requests to the server: auth-token: myapitoken

You should not use authenticate here but just add a header with

httr::add_headers('auth-token' = auth_token)

that would mean

get_de_co2 <- GET(de_call, add_headers('auth-token' = auth_token))

I did not try, it just a guess from the doc you link too and how httr and curl works.
Please try and get back here to tell me if it worked. :relaxed:

1 Like

Yes, that worked. Thankyou.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.