How To Write Your First HTTP Request to NASA
Tags: nasa, http, javascript
Published: 2019-08-03 17:09:31

---
title: How To Write Your First HTTP Request to NASA
published: true
description: 
tags: NASA, HTTP, JavaScript
---
I’m excited to introduce you to HTTP requests and provide you with tools and resources to get up and running.

Learning how to work with APIs is a great way to level up as a front end developer. It’s also a fun way to learn about how to retrieve data and what data is available to you.

The first thing you’ll need to do is request an API key from NASA by filling out this [form](https://api.nasa.gov/index.html#apply-for-an-api-key) to apply for an API key.

You can learn more about what an API key is in this [video](https://www.youtube.com/watch?v=1yFggyk--Zo).
The API key will give you the ability to communicate with the NASA API. After you sign up, you will be immediately directed to a page with your new API key.
It should look like this:

![](https://thepracticaldev.s3.amazonaws.com/i/9hahe5ymk1xcq6s21t8l.png)
_Sensitive information is blurred to protect the user’s identification and personal API key_

There are a number of different requests you can make with your new NASA API key.

My personal favorites include:


  * [Asteroids — NEOWs](https://api.nasa.gov/api.html#NeoWS): identifies asteroids based on their closest approach date to earth.
  * [Mars Rover Photos](https://api.nasa.gov/api.html#MarsPhotos): returns photos collected by NASA’s Curiosity, Opportunity, and Spirit rovers
  * [EONET](https://api.nasa.gov/api.html#EONET): allows users to browse the entire globe daily and look for natural events as they occur. Storms are regularly spotted in the tropics, dust storms over deserts, forest fires in the summers.


Please see the [NASA API documentation](https://api.nasa.gov/index.html) for a full list of capabilities.

#The Code

I will be using JavaScript’s [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) and show you, step-by-step, how to get everything running.

###Step 1

Set up Fetch to make a request using the NASA API of your choice.
You’ll need the URL for the API you’re attempting to request data from. I’ve chosen to use the URL from my first example, Asteroids — NEOWs:

`https://api.nasa.gov/neo/rest/v1/neo/browse?api_key=DEMO_KEY`

###Step 2

You’ll notice the `DEMO_KEY` at the end of the URL. This is what you’ll replace with your own personal API key.
Once the API key has been added, the URL should look very similar to this:

`https://api.nasa.gov/neo/rest/v1/neo/browseapi_key=34oLy4FhODjKGewo2VIy8dsfgSGq6Qb1TQEpAX5s`

The only difference is that your API key will be unique and not identical to the one above.

The Fetch method will take the URL listed above as its one mandatory argument in string form, like so:

`fetch("https://api.nasa.gov/neo/rest/v1/neo/browseapi_key=34oLy4FhODjKGewo2VIy8dsfgSGq6Qb1TQEpAX5s")`

###Step 3

Now we need to append a promise using then to the fetch request which will be returned and resolved to the response of that request.
You’ll notice we build out two cases, a then and a catch — one for success and one for failure, respectively:

{% gist https://gist.github.com/BetterProgramming/3250a2a6d1d288c72461009cf45bf66d.js %}

Here’s a demo of the executed code using [JSFiddle](jsfiddle.net) with [Chrome DevTools](https://developers.google.com/web/tools/chrome-devtools/).

When I run the code, the response from the API will show up in the network tab of the dev console:

![](https://thepracticaldev.s3.amazonaws.com/i/g0sq6aamutklxom1qy06.gif)

If you run this code using your unique API key in JSFiddle with the console open, you will either see the response object or you’re met with an alert box.

**Please note the above code will not run because the API key is not valid.**

###Conclusion and Resources

Feel free to reach out to me if you have questions about the code. It’ll be easiest for both of us if you send over a [JSFiddle](jsfiddle.net) of the issue you’re experiencing.

If NASA isn’t your thing, there are a ton of other APIs out there for you to work with, using similar code.

Here is a short list to check out:

[Twitter](https://developer.twitter.com/en/docs/api-reference-index): provides developers with a lot of different opportunities, including collecting data for trending words and topics in certain regions.

[FEC](https://apilist.fun/api/fec): provides developers with tools to collect information on campaign donations in US federal elections.

Thank you to [Kathryn Hodge](https://twitter.com/blondiebytes?lang=en) for her video about API keys that I referenced above. I’d also like to thank [Wyn Van Devanter](https://twitter.com/wynv), [Chloé Powell](https://twitter.com/cm__powell), Manfred Steyer and [Christian Howard](https://twitter.com/christianhoward) for their feedback and support.