BYTETOOLS

Sunrise-Sunset API

Free sunrise and sunset API with no key. Get sunrise, sunset, solar noon, twilight times and day length for any coordinates and date. Tested curl example.

No API key requiredCORS enabledHTTPSFree tier

Endpoint tested and returned HTTP 200 on 2026-08-19

What is the Sunrise-Sunset API?

The Sunrise-Sunset API is a free, key-free API that returns sunrise, sunset, solar noon, day length and the various twilight times for any latitude, longitude and date.

This API solves a genuinely fiddly astronomical calculation. Sunrise and sunset depend on latitude, longitude, date and atmospheric refraction, and the twilight phases each have their own solar elevation thresholds. Getting it right yourself is a surprising amount of work for what feels like a small feature.

One detail matters more than any other: results are returned in UTC by default. Pass `formatted=0` to get unambiguous ISO 8601 timestamps and convert to local time yourself — treating the default output as local time is the most common bug when integrating this API.

Quick facts

Base URL
https://api.sunrise-sunset.org
Authentication
No key required.
Rate limit
No published hard limit; the maintainers request reasonable use.
Pricing
Free for both personal and commercial use.
CORS
Enabled — callable directly from browser JavaScript
Official docs
Read the docs

How to use the Sunrise-Sunset API

Every request below was executed against the live API on 2026-08-19, and the response shown is the real body it returned — not an illustration.

1. Sunrise and sunset times for Lahore

GET https://api.sunrise-sunset.org/json?lat=31.5204&lng=74.3587

curl
curl 'https://api.sunrise-sunset.org/json?lat=31.5204&lng=74.3587'
JavaScript (fetch)
const res = await fetch("https://api.sunrise-sunset.org/json?lat=31.5204&lng=74.3587");
if (!res.ok) throw new Error(`Request failed: ${res.status}`);
const data = await res.json();
console.log(data);
Python (requests)
import requests

res = requests.get("https://api.sunrise-sunset.org/json?lat=31.5204&lng=74.3587", timeout=20)
res.raise_for_status()
print(res.json())
Response — HTTP 200
{
  "results": {
    "sunrise": "12:29:02 AM",
    "sunset": "1:43:27 PM",
    "solar_noon": "7:06:14 AM",
    "day_length": "13:14:25",
    "civil_twilight_begin": "12:04:56 AM",
    "civil_twilight_end": "2:07:33 PM",
    "nautical_twilight_begin": "11:34:45 PM",
    "nautical_twilight_end": "2:37:43 PM",
    "astronomical_twilight_begin": "11:03:31 PM",
    "astronomical_twilight_end": "3:08:58 PM"
  },
  "status": "OK",
  "tzid": "UTC"
}

Parameters

ParameterTypeRequiredDescription
latfloatRequiredLatitude in decimal degrees. 31.5204
lngfloatRequiredLongitude in decimal degrees. 74.3587
datestringOptionalDate as YYYY-MM-DD, or `today`/`tomorrow`. Defaults to today. 2026-08-19
formattedintegerOptionalSet to 0 for ISO 8601 timestamps instead of human-readable strings. 0
tzidstringOptionalIANA time zone to return results in rather than UTC. Asia/Karachi

Response fields

results.sunrise / sunsetstring
Sunrise and sunset times — in UTC unless you pass `tzid`.
results.solar_noonstring
Moment the sun reaches its highest point.
results.day_lengthstring
Length of daylight, in seconds when `formatted=0`.
results.civil_twilight_begin / endstring
Civil twilight boundaries — the practical limits of usable outdoor light.
statusstring
`OK` on success, or an error string such as `INVALID_REQUEST`.

What you can build with the Sunrise-Sunset API

  • Switch a site or app between light and dark theme at the user's real sunset
  • Schedule smart-home lighting and irrigation around actual daylight
  • Plan photography golden hour and blue hour shoots
  • Show prayer, fasting or observance times that depend on solar position

Common errors and how to fix them

status: INVALID_REQUEST

Missing or malformed `lat`/`lng`.

Fix: Send both as plain decimal numbers; the HTTP status stays 200, so check the `status` field.

status: UNKNOWN_ERROR

Server-side failure, usually transient.

Fix: Retry once after a short delay.

Times look several hours wrong

You read the default output as local time.

Fix: Pass `formatted=0` and convert from UTC, or pass `tzid` with an IANA zone.

Sunrise-Sunset API — frequently asked questions

Is the Sunrise-Sunset API free?

Yes, it is free for personal and commercial use with no API key or signup required.

Why are the sunrise times wrong for my location?

The API returns UTC by default. Either pass `tzid` with an IANA time zone, or pass `formatted=0` and convert the ISO timestamps to local time in your own code.

What is the difference between civil, nautical and astronomical twilight?

They are defined by how far the sun sits below the horizon — 6, 12 and 18 degrees respectively. Civil twilight is the period with enough natural light for outdoor activity, which is the one most applications want.

Does it work at the poles?

Yes, but during polar day or polar night there is no sunrise or sunset, and the API signals this rather than returning a normal time. Handle that case explicitly if you support extreme latitudes.

Tools that pair with this API

Sunrise-Sunset is an independent third-party service and is not affiliated with ByteTools or ByteVancer. Details on this page were verified on 2026-08-19; always check the official documentation before relying on this API in production, as terms and limits can change.