NASA Picture of the Day API

Author

Matthew Niederjohn

Explanation

The NASA website includes various APIs that allow for the collection of NASA data and imagery. These includes various tools such as the Astronomy Picture of the Day, Earth Imagining Camera, and Earth Natural Event Tracker. The various NASA API tools have be found at https://api.nasa.gov, where users can generate their own API Key for free.

The service demonstrated in this tutorial is the Astronomy Picture of the Day. The APOD allows users to view an image of space each and every day, and is a very popular tool on the site. In terms of selecting data with the API, available information about APOD includes:

  • Date the image was taken

  • Explanation of the image

  • Image URL

  • Media Type

  • Image Title

  • Photographer

API Setup

An endpoint as well as the proper setup packages are needed in order to make a call from the APOD data. An example of the APOD endpoint and the necessary packages that can be used to begin the call are listed below:

# setup packages
library(tidyverse)
library(jsonlite)
library(magrittr)

# endpoint
nasa_endpoint <- "https://api.nasa.gov/planetary/apod?"

In order to have access to the endpoint, it is also necessary that to generate an API Key. That can be accomplished on the NASA API site, where the key is emailed within a few seconds. From there, the key can be copied beyond the endpoint in order to begin a call to the data. An example API Key is listed below:

api_key <- "api_key=DEMO_KEY" # using your own API Key

Call to API

Making a call to the API requires that specific fields or parameters are outlined. In this example, all images from 2023 are selected using a start date and end date variable. A definition of those field parameters are listed below:

# call parameters
start_date<-"2023-01-01"
end_date<-"2023-12-31"

Once fields are chosen as parameters, a final call API call can be made in order to retrieve data. The final call will include all the aspects of the query string needed to order to acheive the output. This will include the endpoint, API Key, and fields that were outlined previously. This will output the URL needed as follows:

# get data:
nasa_api_GET <- 
  paste(nasa_endpoint,api_key,"&start_date=",start_date, "&end_date=", end_date,sep = "")

nasa_api_GET
[1] "https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY&start_date=2023-01-01&end_date=2023-12-31"

API URL

https://api.nasa.gov/planetary/apod?api_key=YOURGENERATEDKEY&start_date=2023-01-01&end_date=2023-12-31

Data Frames

Upon generating this URL, data can be retrieved in Rstudio using the JSON document that has been created. The eval() function can be used to convert the data to Rstudio in order to be viewed as a data frame. The result of this conversion is listed below:

# data frame
nasa_data <-
  nasa_api_GET %>%
  eval() %>%
  fromJSON()

Once this data is retrieved within Rstudio, the next step is to extract the data in order to be viewed in a table. A depiction of NASA image of the day data from the second day of 2023 would look as follows:

Date Explanation URL media_type title copyright
2023-01-02 Look up tonight and see a lot of planets… https://apod.nasa.gov/apod/image/2301/All image After Sunset Planet Parade Tunc Tezel

Example Images

Once the data is called, all images are available via the URL offered in the table compiled in the data frame. This data table allows for someone to search for images within Rstudio based on features in the image’s explanation. For example, it is possible to search for images that feature a full moon by using the following filter function:

# search for images containing 'full moon'
full_moon <-
nasa_data %>%
  filter(str_detect(explanation, "Full Moon"))

This data filter complies a table of images that include “Full Moon” in its description. With this data, access to URLs containing this desired image can be achieved. This allows for quick access to the URL containing each image. Several examples of full moons can be seen under these links:

https://apod.nasa.gov/apod/image/2308/GianniTumino_SturgeonMoon_Palette_JPG_LOGO_2048.jpg

https://apod.nasa.gov/apod/image/2307/CocoaBeach_BuckMoon_Seeley-201.jpg

Summary

Overall, the Astronomy Picture of the Day is one of more limited APIs on NASA site. Searching for various images can be quite unique and interesting, however there are not an extensive number of variables that can be called or filtered for. In the future, it would be interesting to experiment with other APIs offered on NASA’s site, as there are other platforms such as EPIC or EONOT that feature some cool capabilities with categories and coordinates and so forth.