Overview

The Sunrise and Sunset API offers developers a programmatic interface to retrieve accurate solar event times for any given latitude, longitude, and date. This API is designed for applications requiring precise astronomical calculations, such as determining the exact moment of sunrise, sunset, solar noon, and various twilight phases (civil, nautical, and astronomical). Its primary utility lies in providing time-sensitive data that varies significantly by location and day.

Developers commonly integrate the Sunrise and Sunset API into a range of applications. For outdoor event planning, it enables scheduling activities like photography sessions, construction work, or agricultural tasks to align with daylight hours or specific twilight conditions. In smart home systems, it can automate lighting or blinds based on local solar cycles. Astronomical applications leverage the data for observation planning, while geospatial services can enrich location-based information with relevant solar context.

The API operates via simple HTTP GET requests, returning data in JSON format. This design prioritizes ease of integration, making it accessible for both web and mobile development environments. It abstracts the complex astronomical algorithms, providing developers with a direct endpoint to query solar data without needing to implement the underlying calculations. The service supports queries for single dates or provides calculations for a 30-day period, accommodating various use cases from real-time lookups to predictive scheduling.

Beyond basic sunrise and sunset times, the API includes metrics like solar noon, which is the time when the sun reaches its highest point in the sky, and day length, which indicates the total duration of daylight. The different twilight types—civil, nautical, and astronomical—offer granular control for applications that require specific light conditions, such as determining when stars become visible or when ambient light levels drop below a certain threshold. For example, civil twilight is often used for legal definitions of daylight, while astronomical twilight marks when the sun is 18 degrees below the horizon, and direct sunlight no longer illuminates the sky (Mozilla Twilight definition).

The API's straightforward design and consistent data format aim to reduce development overhead, allowing teams to focus on their core application logic while relying on an external service for solar event calculations. Its free tier supports initial development and lower-volume applications, with scalable paid plans available for higher request volumes. This makes it suitable for projects ranging from hobbyist applications to commercial services requiring robust solar data integration.

Key features

  • Sunrise and Sunset Times: Provides the exact local times for sunrise and sunset for any given latitude, longitude, and date (Sunrise and Sunset API documentation).
  • Solar Noon Calculation: Returns the time when the sun reaches its highest point in the sky for the specified location and date.
  • Day Length Determination: Calculates the total duration of daylight hours between sunrise and sunset.
  • Twilight Times (Civil, Nautical, Astronomical): Offers precise times for the beginning and end of civil, nautical, and astronomical twilight, useful for varying light conditions.
  • Flexible Date Queries: Supports queries for a single date or provides a 30-day forecast of solar events.
  • JSON Response Format: Delivers data in a standard JSON format, facilitating parsing and integration into web and mobile applications.
  • HTTP GET Requests: Utilizes simple, stateless HTTP GET requests for easy API interaction.

Pricing

The Sunrise and Sunset API offers a free tier for basic usage and paid tiers for increased request volumes. Pricing is effective as of May 2026.

Plan Monthly Requests Price (USD/month) Features
Free 1,000 $0 All core API features, limited requests
Basic 10,000 $5 All core API features, increased request limit
Standard 50,000 $15 All core API features, higher request limit
Premium 200,000 $40 All core API features, most generous request limit

For more detailed pricing information and current terms, refer to the Sunrise and Sunset API pricing page.

Common integrations

  • Web Applications: Integrating solar event times into web platforms using JavaScript (e.g., React, Vue, Angular) or backend frameworks (e.g., Node.js, Python, Ruby) to display local sunrise/sunset times for users.
  • Mobile Applications: Incorporating location-based solar data into iOS and Android apps for features like photography planning, outdoor activity scheduling, or smart home automation.
  • Smart Home Systems: Connecting with platforms like Home Assistant or custom IoT solutions to automate lighting, blinds, or other devices based on local solar cycles.
  • Data Analytics Platforms: Using tools like Python with Pandas or R to analyze solar data in conjunction with other datasets for research or predictive modeling.
  • Mapping and Geospatial Services: Enhancing map applications (e.g., built with ArcGIS Developers or Google Maps Platform) by overlaying solar event information relevant to specific locations.

Alternatives

  • Astronomy API (Open-Meteo): Provides free weather and astronomy data for non-commercial use, including sunrise, sunset, and twilight times.
  • Time and Date AS: Offers a comprehensive API for time, date, and astronomical data, including advanced solar and lunar calculations.
  • Google Maps Platform: While primarily a mapping service, it can be combined with custom calculations or other APIs to derive solar event data for locations.

Getting started

To get started with the Sunrise and Sunset API, you can make a simple HTTP GET request to its endpoint. The API requires latitude and longitude coordinates, and optionally a date. Here's an example using Python's requests library to fetch sunrise and sunset times for New York City on a specific date:

import requests
import json

# Define the API endpoint and parameters
BASE_URL = "https://api.sunrise-sunset.org/json"
LATITUDE = 40.7128  # New York City latitude
LONGITUDE = -74.0060 # New York City longitude
DATE = "2026-05-28"   # Desired date

params = {
    "lat": LATITUDE,
    "lng": LONGITUDE,
    "date": DATE,
    "formatted": 0  # Use 0 for ISO 8601 formatted times
}

try:
    # Make the API request
    response = requests.get(BASE_URL, params=params)
    response.raise_for_status() # Raise an exception for HTTP errors

    # Parse the JSON response
    data = response.json()

    # Check if the request was successful
    if data["status"] == "OK":
        results = data["results"]
        print(f"Sunrise for {DATE} at ({LATITUDE}, {LONGITUDE}): {results['sunrise']}")
        print(f"Sunset for {DATE} at ({LATITUDE}, {LONGITUDE}): {results['sunset']}")
        print(f"Solar Noon: {results['solar_noon']}")
        print(f"Day Length: {results['day_length']} seconds")
        print(f"Civil Twilight Begins: {results['civil_twilight_begin']}")
        print(f"Civil Twilight Ends: {results['civil_twilight_end']}")
    else:
        print(f"Error: {data['status']}")
        if "error" in data: # Some APIs provide an 'error' field in case of non-OK status
            print(f"Details: {data['error']}")

except requests.exceptions.RequestException as e:
    print(f"An error occurred during the request: {e}")
except json.JSONDecodeError:
    print(f"Failed to decode JSON from response: {response.text}")
except KeyError as e:
    print(f"Missing key in API response: {e}. Full response: {data}")

This Python script sends a GET request to the Sunrise and Sunset API, passing the latitude, longitude, and date as query parameters. The formatted=0 parameter ensures that the times are returned in ISO 8601 format, which is generally easier to parse programmatically. The script then prints the retrieved sunrise, sunset, solar noon, day length, and civil twilight times. Error handling is included to manage potential network issues or API response problems.