Html API header for project

Hello, I am working on making an app for showing moon phases and tides. The tides are supposed to come from an API offered by noaa, and noaa requires a header with the given API key to be included in the program for access to the data. However, in the documentation for startWebRequest(url, callback) for the data request did not include how to use a header, even though noaa.org was listed as an accepted source. What is the best way to include a header, if possible?

Here is a link to the API documentation for the noaa API: https://www.ncdc.noaa.gov/cdo-web/webservices/v2#gettingStarted

@paulina Thanks for posting this question. I’m going to ask a few questions to better understand your problem. Have you obtained an access token? Have you tried to access the data from noaa? If so, what was the result?

Yes, I have received the API key (access token) already, but I am not sure how to use it here. Without the key, accessing the data resulted in a 400 error. The NOAA website specifies that token should be passed as header (https://www.ncdc.noaa.gov/cdo-web/webservices/v2). I tried the following code:

startWebRequest(“https://noaa.gov/cdo-web/api/v2/stations”, function(status, type, content) {

console.log(status);
console.log(content.metadata.results.count);

});

You need to send the API key as a header. You cannot do that with startWebRequest because it will only take a URL.

@paulinanovitchenko Please let us know if you need further assistance.

Thanks for the explanation, the documentation did not state that it was impossible to add a header using startWebRequest. Do you know of a good way to send the key as a header in code.org?

I think the limitation is self imposed to keep kids out of trouble or never needed before. The Javascript object to do that is undefined in App Lab.

Great question @paulina. First off, huge thank you - you exposed a hole in our documentation. Currently, we don’t support headers in startWebRequest.

Thanks also to @jdonwells and @terence.stone25 for the top-notch analysis. As @jdonwells mentioned, the reason support for headers doesn’t exist right now is most likely that it was not needed before. Many of the other APIs either allow the key to be passed at the end of the url (i.e. https://hostname.com?key=12345) or don’t require one at all.

What we plan to do:

  1. Remove noaa.gov from our list of supported APIs and update our documentation to close the hole you discovered
  2. Add a check for required API headers to our vetting process for new APIs
  3. Begin the process of looking into what would need to change for us to start supporting headers in startWebRequest

Thank you again for bringing this to our attention. Unfortunately, this doesn’t solve your current problem. The best option we can recommend right now is using a different API. developer.accuweather.com and api.nasa.gov may have some of the data you are looking for.

Thank you,
Jessica
Code.org Engineer

1 Like

Thank you for clarifications! Rather than removing NOAA from the supported APIs, perhaps you could use https://www.ncdc.noaa.gov/cdo-web/token to request an API token for Code.org that all users could share? This way every user won’t have to set their own header but they would still be able to use the resource for climate data. It would be very unfortunate to loose support for NOAA and I feel hopeful they will be OK with supporting all the people learning to code and sharing one token.