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.
- Visit NASA API Portal
- Fill out the form to generate an API key
- You'll receive your API key instantly
- 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.
Note
The DEMO_KEY is limited to 30 requests per hour and 50 requests per day. For a production application, you should use your own API key, which allows for 1,000 requests per hour.
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
Best Practice
Implement caching for APOD data to reduce API calls. Since the APOD only changes once per day, you can safely cache responses for 24 hours.
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 keydatestring - The date of the APOD image to retrieve (YYYY-MM-DD)start_datestring - The start of a date range (YYYY-MM-DD)end_datestring - The end of a date range (YYYY-MM-DD)countnumber - Number of random images to retrieve (cannot be used with date or date range)thumbsboolean - Return URL of thumbnail image for video media types