NASA APOD API

Science
Beginner

Overview

Access NASA's Astronomy Picture of the Day, featuring stunning space images with explanations

Integration Guide

Step 1: Get API Key

NASA's APOD API is free to use, but you'll need an API key for production use.

  1. Visit NASA API Portal
  2. Fill out the form to generate an API key
  3. You'll receive your API key instantly
  4. For testing, you can use the demo key: "DEMO_KEY" (limited to 30 requests per hour)

Step 2: Set Up Your Project

Create the necessary files for your APOD viewer application:

  • index.html - The HTML structure
  • styles.css - CSS for styling
  • script.js - JavaScript for API integration

Step 3: Implement the Code

Copy the following code into your project files:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>NASA Astronomy Picture of the Day</title>
  <link rel  initial-scale=1.0">
  <title>NASA Astronomy Picture of the Day</title>
  <link rel="stylesheet" href="styles.css">
</head>
<body>
  <div class="container">
    <h1>NASA Astronomy Picture of the Day</h1>
    
    <div class="date-picker">
      <label for="date-input">Select a date:</label>
      <input type="date" id="date-input">
      <button id="view-btn">View</button>
      <button id="random-btn">Random</button>
    </div>
    
    <div class="apod-container" id="apod-container">
      <!-- APOD content will be displayed here -->
      <div class="loading">Loading today's astronomy picture...</div>
    </div>
  </div>
  
  <script src="script.js"></script>
</body>
</html>

Step 4: Replace the API Key

For production use, replace DEMO_KEY in the JavaScript file with your actual API key.

Step 5: Test Your Implementation

Open your HTML file in a browser to test your APOD viewer application. You should be able to:

  • View today's Astronomy Picture of the Day
  • Select a specific date to view that day's picture
  • Get a random astronomy picture
  • Read the explanation provided by NASA for each image

Error Handling

Common Errors

  • 403 Forbidden: Invalid API key
  • 429 Too Many Requests: Rate limit exceeded
  • 400 Bad Request: Invalid parameters (e.g., invalid date format)

Rate Limiting

NASA's API has the following rate limits:

  • DEMO_KEY: 30 requests per hour, 50 requests per day
  • Registered API key: 1,000 requests per hour

Media Types

The APOD API can return either images or videos. Our example code handles both types, but be aware that you'll need to check the media_type field to determine how to display the content.

API Details

Base URL
https://api.nasa.gov/planetary/apod
Authentication
API Key
Documentation
Official Docs

Endpoints

  • GET
    /

    Get the Astronomy Picture of the Day

    Parameters
    • api_key*
      string - Your API key
    • date
      string - The date of the APOD image to retrieve (YYYY-MM-DD)
    • start_date
      string - The start of a date range (YYYY-MM-DD)
    • end_date
      string - The end of a date range (YYYY-MM-DD)
    • count
      number - Number of random images to retrieve (cannot be used with date or date range)
    • thumbs
      boolean - Return URL of thumbnail image for video media types