{
  "openapi": "3.1.0",
  "info": {
    "title": "SunAndMoon sun and moon API",
    "version": "1.0.0",
    "summary": "Sunrise, sunset, golden hour, blue hour, twilight and moon times for a city, computed with SunCalc.",
    "description": "Free, public, read-only JSON API. No key and no sign-up. Every time is returned twice: `iso` as a UTC instant and `local` as HH:MM in the city's own time zone. Events that do not occur on that day (polar day or night, a day with no moonrise or moonset) are `null`, never a guessed value. Licensed CC BY 4.0: wherever you show these times, name SunAndMoon and link to sunandmoon.app.",
    "license": {
      "name": "CC BY 4.0",
      "identifier": "CC-BY-4.0",
      "url": "https://creativecommons.org/licenses/by/4.0/"
    },
    "contact": {
      "name": "SunAndMoon",
      "url": "https://sunandmoon.app/contact"
    }
  },
  "externalDocs": {
    "description": "Field reference, examples and licence",
    "url": "https://sunandmoon.app/developers"
  },
  "servers": [
    {
      "url": "https://sunandmoon.app"
    }
  ],
  "paths": {
    "/api/sun/{slug}": {
      "parameters": [
        {
          "name": "slug",
          "in": "path",
          "required": true,
          "description": "City slug, the same as in the site URL /sun/{slug}, for example `london` or `new-york`.",
          "schema": {
            "type": "string"
          },
          "example": "london"
        }
      ],
      "get": {
        "operationId": "getSunAndMoon",
        "summary": "Sun and moon times for one city on one day",
        "parameters": [
          {
            "name": "date",
            "in": "query",
            "required": false,
            "description": "Local calendar date in the city, as YYYY-MM-DD. Optional. A value that does not match YYYY-MM-DD is not an error: it is ignored and today in the city's time zone is used instead. The `date` field of the response holds the date that was used.",
            "schema": {
              "type": "string",
              "pattern": "^\\d{4}-\\d{2}-\\d{2}$"
            },
            "example": "2026-12-25"
          }
        ],
        "responses": {
          "200": {
            "description": "Sun and moon data for the city and date.",
            "headers": {
              "Access-Control-Allow-Origin": {
                "description": "Always `*`.",
                "schema": { "type": "string" }
              },
              "Cache-Control": {
                "description": "`public, s-maxage=3600, stale-while-revalidate=86400`.",
                "schema": { "type": "string" }
              },
              "X-Robots-Tag": {
                "description": "Always `noindex`.",
                "schema": { "type": "string" }
              }
            },
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/SunResponse" }
              }
            }
          },
          "404": {
            "description": "No city with this slug.",
            "headers": {
              "Access-Control-Allow-Origin": {
                "description": "Always `*`.",
                "schema": { "type": "string" }
              }
            },
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/Error" },
                "example": {
                  "error": "unknown_city",
                  "message": "No city with slug \"atlantis\"."
                }
              }
            }
          }
        }
      },
      "options": {
        "operationId": "preflightSunAndMoon",
        "summary": "CORS preflight",
        "responses": {
          "204": {
            "description": "Empty body. Allows GET and OPTIONS from any origin with a Content-Type header.",
            "headers": {
              "Access-Control-Allow-Origin": {
                "description": "Always `*`.",
                "schema": { "type": "string" }
              },
              "Access-Control-Allow-Methods": {
                "description": "`GET, OPTIONS`.",
                "schema": { "type": "string" }
              },
              "Access-Control-Allow-Headers": {
                "description": "`Content-Type`.",
                "schema": { "type": "string" }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "Time": {
        "type": ["object", "null"],
        "description": "A moment, or null when the event does not occur that day.",
        "required": ["iso", "local"],
        "additionalProperties": false,
        "properties": {
          "iso": {
            "type": "string",
            "format": "date-time",
            "description": "UTC instant in ISO 8601.",
            "example": "2026-07-12T03:57:00.000Z"
          },
          "local": {
            "type": "string",
            "description": "HH:MM in the city's own time zone (24-hour clock).",
            "example": "04:57"
          }
        }
      },
      "Window": {
        "type": "object",
        "required": ["start", "end"],
        "additionalProperties": false,
        "properties": {
          "start": { "$ref": "#/components/schemas/Time" },
          "end": { "$ref": "#/components/schemas/Time" }
        }
      },
      "MorningEvening": {
        "type": "object",
        "required": ["morning", "evening"],
        "additionalProperties": false,
        "properties": {
          "morning": { "$ref": "#/components/schemas/Window" },
          "evening": { "$ref": "#/components/schemas/Window" }
        }
      },
      "City": {
        "type": "object",
        "required": ["name", "country", "slug", "region", "lat", "lon", "timezone"],
        "additionalProperties": false,
        "properties": {
          "name": { "type": "string", "example": "London" },
          "country": { "type": "string", "example": "United Kingdom" },
          "slug": { "type": "string", "description": "Matches the site URL /sun/{slug}.", "example": "london" },
          "region": { "type": "string", "description": "Continent or region the city is grouped under on the site.", "example": "Europe" },
          "lat": { "type": "number", "description": "Latitude in degrees, WGS84.", "example": 51.5074 },
          "lon": { "type": "number", "description": "Longitude in degrees, WGS84.", "example": -0.1278 },
          "timezone": { "type": "string", "description": "IANA time zone name.", "example": "Europe/London" }
        }
      },
      "Twilight": {
        "type": "object",
        "required": ["astronomicalDawn", "nauticalDawn", "civilDawn", "civilDusk", "nauticalDusk", "astronomicalDusk"],
        "additionalProperties": false,
        "properties": {
          "astronomicalDawn": { "$ref": "#/components/schemas/Time", "description": "Sun rising through -18 degrees." },
          "nauticalDawn": { "$ref": "#/components/schemas/Time", "description": "Sun rising through -12 degrees." },
          "civilDawn": { "$ref": "#/components/schemas/Time", "description": "Sun rising through -6 degrees." },
          "civilDusk": { "$ref": "#/components/schemas/Time", "description": "Sun setting through -6 degrees." },
          "nauticalDusk": { "$ref": "#/components/schemas/Time", "description": "Sun setting through -12 degrees." },
          "astronomicalDusk": { "$ref": "#/components/schemas/Time", "description": "Sun setting through -18 degrees." }
        }
      },
      "Sun": {
        "type": "object",
        "required": ["sunrise", "sunset", "solarNoon", "dayLengthMinutes", "goldenHour", "blueHour", "twilight"],
        "additionalProperties": false,
        "properties": {
          "sunrise": { "$ref": "#/components/schemas/Time", "description": "Centre of the sun at -0.833 degrees, rising." },
          "sunset": { "$ref": "#/components/schemas/Time", "description": "Centre of the sun at -0.833 degrees, setting." },
          "solarNoon": { "$ref": "#/components/schemas/Time", "description": "Sun at its highest." },
          "dayLengthMinutes": {
            "type": ["integer", "null"],
            "minimum": 0,
            "maximum": 1440,
            "description": "Minutes between sunrise and sunset.",
            "example": 977
          },
          "goldenHour": { "$ref": "#/components/schemas/MorningEvening", "description": "Sun between +6 and -0.833 degrees." },
          "blueHour": { "$ref": "#/components/schemas/MorningEvening", "description": "Sun between -0.833 and -6 degrees." },
          "twilight": { "$ref": "#/components/schemas/Twilight" }
        }
      },
      "Moon": {
        "type": "object",
        "required": ["phase", "phaseName", "illumination", "moonrise", "moonset"],
        "additionalProperties": false,
        "properties": {
          "phase": {
            "type": "number",
            "minimum": 0,
            "maximum": 1,
            "description": "Position in the synodic month: 0 new moon, 0.25 first quarter, 0.5 full moon, 0.75 last quarter. Computed for the moment of the request."
          },
          "phaseName": {
            "type": "string",
            "enum": ["New Moon", "Waxing Crescent", "First Quarter", "Waxing Gibbous", "Full Moon", "Waning Gibbous", "Last Quarter", "Waning Crescent"]
          },
          "illumination": {
            "type": "number",
            "minimum": 0,
            "maximum": 1,
            "description": "Fraction of the disc lit."
          },
          "moonrise": { "$ref": "#/components/schemas/Time" },
          "moonset": { "$ref": "#/components/schemas/Time" }
        }
      },
      "SunResponse": {
        "type": "object",
        "required": ["city", "date", "sun", "moon", "source", "attribution", "attribution_html"],
        "additionalProperties": false,
        "properties": {
          "city": { "$ref": "#/components/schemas/City" },
          "date": {
            "type": "string",
            "description": "The city's local date the times are for, YYYY-MM-DD.",
            "example": "2026-07-12"
          },
          "sun": { "$ref": "#/components/schemas/Sun" },
          "moon": { "$ref": "#/components/schemas/Moon" },
          "source": {
            "type": "string",
            "const": "SunCalc"
          },
          "attribution": {
            "type": "string",
            "const": "https://sunandmoon.app",
            "description": "The URL to link to when you show these times."
          },
          "attribution_html": {
            "type": "string",
            "const": "Sun data: <a href=\"https://sunandmoon.app\">SunAndMoon</a>",
            "description": "A ready-made HTML credit line that names SunAndMoon and links to sunandmoon.app."
          }
        }
      },
      "Error": {
        "type": "object",
        "required": ["error", "message"],
        "additionalProperties": false,
        "properties": {
          "error": { "type": "string", "const": "unknown_city" },
          "message": { "type": "string" }
        }
      }
    }
  }
}
