Overview
NASAExplorer uses the NASA APOD (Astronomy Picture of the Day) API to fetch astronomical images. This guide will help you configure the API key securely in your Android project.Prerequisites
- Android Studio Koala or higher
- Active internet connection
- NASA API account
Obtaining Your API Key
Visit NASA API Portal
Navigate to https://api.nasa.gov/ to register for a free API key.
Complete Registration
Fill out the registration form with your:
- First and Last Name
- Email Address
- Application Purpose
The DEMO_KEY is available for testing but has limited rate limits (30 requests per hour, 50 requests per day). It’s recommended to register for a personal API key.
Configuration Setup
Adding the API Key to local.properties
The NASA API key is stored inlocal.properties to keep it secure and separate from version control.
Add the API Key
Open Replace
local.properties and add your NASA API key:local.properties
YOUR_NASA_API_KEY_HERE with the actual key from NASA.How It Works
Build Configuration
Theapp/build.gradle.kts file reads the API key from local.properties and injects it as a BuildConfig field:
app/build.gradle.kts
Repository Usage
TheNasaRepository accesses the API key through BuildConfig:
NasaRepository.kt
API Endpoints Used
NASAExplorer uses the following NASA APOD endpoints:Base URL
1. Get Image of the Day
api_key(required): Your NASA API keydate(optional): Date in YYYY-MM-DD format
2. Get Images in Date Range
api_key(required): Your NASA API keystart_date(required): Start date in YYYY-MM-DD formatend_date(optional): End date in YYYY-MM-DD format
3. Get Random Images
api_key(required): Your NASA API keycount(required): Number of random images (max: 100)
Network Configuration
The API is configured using Retrofit with the following setup:ApiNetworkModule.kt
Rate Limits
NASA API rate limits:
- Free API Key: 1,000 requests per hour
- DEMO_KEY: 30 requests per hour, 50 requests per day
Troubleshooting
Missing API Key Error
If you see a build error likeAPI_KEY not found:
- Verify
local.propertiesexists in the project root - Confirm the property is named exactly
nasaApiKey - Ensure no extra spaces around the equals sign
- Sync Gradle files
Invalid API Key Response
If API requests fail with 403 Forbidden:- Double-check your API key is correct
- Verify the key is active in your NASA account
- Check if you’ve exceeded rate limits
- Try using DEMO_KEY temporarily for testing
Connection Issues
- Active internet connection
- Network permissions in AndroidManifest.xml
- Proper DNS resolution
Security Best Practices
What’s Protected
The.gitignore file includes:
Next Steps
Once your API key is configured:- Set up Firebase for user authentication
- Configure security rules for production
- Build and run the app on your device
For production releases, consider implementing API key obfuscation techniques or using a backend proxy to protect your credentials.