Consuming NASA API using Python — Part 1 | Daily Python #17

Ajinkya Sonawane
Daily Python
Published in
4 min readJan 23, 2020

This article is a tutorial on how to consume the NASA API using Python. This is the first part of the NASA API series.

Source: https://nordicapis.com/api_profile/nasa-api/

This article is a part of Daily Python challenge that I have taken up for myself. I will be writing short python articles daily.

Sign up on the NASA API website: https://api.nasa.gov/. After this step, you will receive an API key via email. This API Key will be used for further communication with the NASA API.

Requirements:

  1. Python 3.0
  2. Pip

Install the following packages:

  1. requests — Library to making HTTP requests.
  2. pprint — Library for printing structures of data in a formatted way.
  3. urllib — Library for handling URLs
pip install requests pprint urllib3

Let’s import these libraries

import requests
from urllib.request import urlretrieve
from pprint import PrettyPrinter
pp = PrettyPrinter()
pp = PrettyPrinter()

The ‘pp’ instance of PrettyPrinter will be used to beautify the JSON output.

Let’s store the API Key in a variable to be used in the program

apiKey = 'YOUR_NASA_API_KEY'

We will cover the following APIs in this article

  1. APOD — Astronomy picture of the day
  2. Asteroids NeoWs — NeoWs (Near Earth Object Web Service)
  3. EPIC — Earth Polychromatic Imaging Camera

Let’s start by writing a function to consume the APOD API

def fetchAPOD():
URL_APOD = "https://api.nasa.gov/planetary/apod"
date = '2020-01-22'
params = {
'api_key':api_key,
'date':date,
'hd':'True'
}
response = requests.get(URL_APOD,params=params).json()
pp.pprint(response)
fetchAPOD()
Source: https://api.nasa.gov/
Snip of the Output of the above code
Astronomy Picture for 23 Jan 2020

Let’s define functions to consume the Asteroids NeoWs API

There are 3 operations for fetching information of the Near Earth Objects.

  1. Feed
  2. Lookup
  3. Browse
Source: https://api.nasa.gov/
def fetchAsteroidNeowsFeed():
URL_NeoFeed = "https://api.nasa.gov/neo/rest/v1/feed"
params = {
'api_key':api_key,
'start_date':'2020-01-22',
'end_date':'2020-01-23'
}
response = requests.get(URL_NeoFeed,params=params).json()
pp.pprint(response)
fetchAsteroidNeowsFeed()
Snip of the Output of the above code snippet

Note: The above data can be used to study the path of the asteroids and their probability of impact with other Neos.

Source: https://api.nasa.gov/
def fetchAsteroidNeowsLookup():
asteroid_id = '3542519'
URL_NeoLookup = "https://api.nasa.gov/neo/rest/v1/neo/"+ asteroid_id
params = {
'api_key':api_key
}
response = requests.get(URL_NeoLookup,params=params).json()
pp.pprint(response)
fetchAsteroidNeowsLookup()
Snip of the Output of the above code snippet
Source: https://api.nasa.gov/
def fetchAsteroidNeowsBrowse():
URL_NeoBrowse = "https://api.nasa.gov/neo/rest/v1/neo/browse/"
params = {
'api_key':api_key
}
response = requests.get(URL_NeoBrowse,params=params).json()
pp.pprint(response)
fetchAsteroidNeowsBrowse()
Snip of the Output of the above code

Finally let’s define a function to consume the EPIC ( Earth Polychromatic Imaging Camera) API

Source: https://api.nasa.gov/
Source: https://api.nasa.gov/
def fetchEPICData():
URL_EPIC = "https://api.nasa.gov/EPIC/api/natural/"
params = {
'api_key':api_key,
}
response = requests.get(URL_EPIC,params=params).json()
pp.pprint(response)
fetchEPICData()
Snip of the Output of the above code snippet

Let’s save an image using the ‘image’ id we fetched from ‘fetchEPICData’

def fetchEPICImage():
YEAR = '2015'
MONTH = '10'
DAY = '31'
IMAGE_ID = 'epic_1b_20151031074844'
URL_EPIC = "https://epic.gsfc.nasa.gov/archive/natural/"
URL_EPIC = URL_EPIC + YEAR +'/' + MONTH + '/'+DAY
URL_EPIC = URL_EPIC + '/png'
URL_EPIC = URL_EPIC + '/' + IMAGE_ID + '.png'
print(URL_EPIC)
urlretrieve(URL_EPIC,IMAGE_ID+'.png')

fetchEPICImage()

Saved Image:

Source: NASA

I hope this article was helpful, do leave some claps if you liked it.

Follow the Daily Python Challenge here:

--

--