API for Sunset, Sunrise, Moonset and Moonrise timings

Our Astronomy API provides timings for sunrise, sunset, moonrise, moonset, sun azimuth, moon azimuth, sun altitude, moon altitude, sun distance from the earth and moon distance from the earth from latitude and longitude or an IPv4 or IPv6 address for a given date.

  • objectlocation:Object,
    • fieldcountry_code2:"",
    • fieldcountry_code3:"",
    • fieldcountry_name:"",
    • fieldcontinent_name:"",
    • fieldcontinent_code:"",
    • fieldis_eu:"",
    • fieldcountry_name_official:"",
    • fieldstate_prov:"",
    • fieldstate_code:"",
    • fielddistrict:"",
    • fieldcity:"",
    • fieldzipcode:"",
    • field"latitude:",
    • field"longitude:"
  • calendardate:"",
  • fieldcurrent_time:"",
  • fieldsunrise:"",
  • fieldsunset:"",
  • fieldsun_status:"",
  • fieldsolar_noon:"",
  • fieldday_length:"",
  • integersun_altitude:,
  • integersun_distance:,
  • integersun_azimuth:,
  • fieldmoonrise:"",
  • fieldmoonset:"",
  • fieldmoon_status:"",
  • integermoon_altitude:,
  • integermoon_distance:,
  • integermoon_azimuth:,
  • integermoon_parallactic_angle:
  • fieldmoon_phase:
  • fieldmoon_illumination_percentage:
  • integermoon_angle:

Comprehensive Sun and Moon data

The Astronomy API provides a detailed dataset about twilight timings, different sun and moon parameters for a given date and location.

Response

1{
2  "location": {
3    "location": "New York, US'",
4    "country": "United States",
5    "state": "New York",
6    "city": "New York",
7    "locality": "Clinton",
8    "latitude": 40.76473335,
9    "longitude": -74.00083980660943
10  },
11  "date": "2026-12-09",
12  "current_time": "07:13:16.209",
13  "sunrise": "07:08",
14  "sunset": "16:28",
15  "sun_status": "-",
16  "solar_noon": "11:48",
17  "day_length": "09:20",
18  "sun_altitude": -0.04277378938141529,
19  "sun_distance": 147366924.6229829,
20  "sun_azimuth": 120.78987572078586,
21  "moonrise": "07:58",
22  "moonset": "16:41",
23  "moon_status": "-",
24  "moon_altitude": -6.805181589019477,
25  "moon_distance": 404814.0580441422,
26  "moon_azimuth": 120.86585289029688,
27  "moon_parallactic_angle": -47.18639517696103,
28  "moon_phase": "NEW_MOON",
29  "moon_illumination_percentage": "0.20",
30  "moon_angle": 5.164672210638351
31}

Getting Astronomical Information from a Location Coordinates

You can pass the latitude and longitude of a location as query parameters to get the astronomical information.

Request

curl 'https://api.ipgeolocation.io/astronomy?apiKey=API_KEY&lat=37.258&long=-122'

Response

1{
2  "location": {
3    "latitude": 37.258,
4    "longitude": -122
5  },
6  "date": "2024-11-04",
7  "current_time": "04:21:22.848",
8  "sunrise": "06:36",
9  "sunset": "17:06",
10  "sun_status": "-",
11  "solar_noon": "11:51",
12  "day_length": "10:30",
13  "sun_altitude": -27.1916974786371,
14  "sun_distance": 148361706.3935511,
15  "sun_azimuth": 89.39202575904915,
16  "moonrise": "09:55",
17  "moonset": "19:02",
18  "moon_status": "-",
19  "moon_altitude": -60.310006400447534,
20  "moon_distance": 396442.70137180487,
21  "moon_azimuth": 80.68476682031314,
22  "moon_parallactic_angle": -62.33291127227254,
23  "moon_phase": "WAXING_CRESCENT",
24  "moon_illumination_percentage": "8.23",
25  "moon_angle": 33.33523603807506
26}

Getting Astronomical Information from an IPv4 or IPv6 Address

You can pass any IPv4 or IPv6 address as a query parameter to get the astronomical information.

Request

curl 'https://api.ipgeolocation.io/astronomy?apiKey=API_KEY&ip=8.8.8.8'

Response

1{
2  "location": {
3    "continent_code": "NA",
4    "continent_name": "North America",
5    "country_code2": "US",
6    "country_code3": "USA",
7    "country_name": "United States",
8    "country_name_official": "United States of America",
9    "is_eu": false,
10    "state_prov": "California",
11    "state_code": "US-CA",
12    "district": "Santa Clara",
13    "city": "Mountain View",
14    "zipcode": "94043-1351",
15    "latitude": 37.4224,
16    "longitude": -122.08421
17  },
18  "date": "2024-11-04",
19  "current_time": "04:24:04.939",
20  "sunrise": "06:37",
21  "sunset": "17:06",
22  "sun_status": "-",
23  "solar_noon": "11:51",
24  "day_length": "10:29",
25  "sun_altitude": -26.720690286733994,
26  "sun_distance": 148361706.39355108,
27  "sun_azimuth": 89.66947559541882,
28  "moonrise": "09:56",
29  "moonset": "19:01",
30  "moon_status": "-",
31  "moon_altitude": -59.84017619349532,
32  "moon_distance": 396436.927965668,
33  "moon_azimuth": 80.87803466587684,
34  "moon_parallactic_angle": -62.15740140382433,
35  "moon_phase": "WAXING_CRESCENT",
36  "moon_illumination_percentage": "8.24",
37  "moon_angle": 33.35668681050102
38}

Use Cases

Photography

With every sunrise and sunset, nature creates a masterpiece. Take advantage of them and plan your photography sessions ahead of time for the perfect capture with our sunrise-sunset API.

iplocation

Sports

Planning a game of golf? Make sure you plan your tee for the right light. Because you know what they say, "The most important shot is the first one."

geoip

Travelers

Plan the perfect sunset romantic dinner, hiking trip or star-gazing party by taking advantage of the best star API you can find.

iplook-up

Astronomy

"Twilight is the time of the day when the sky looks like it has been painted by a graffiti artist". Use our astronomy API to plan the perfect time to view this breath-taking phenomenon and explore one of the most over-whelming covers of the heavens.

ipgeolocator

Solar panel placement...?

Trying to figure out the optimal angle for your solar panels? Use our API to get the exact solar azimuth and calculate the best solar panel angle by street address.

iplook-up

Ready to get started?Get Started with IP Intelligence API Today

Astronomy API Documentation

Astronomy API

The Astronomy API provides the location-based rise and set times for the Sun and Moon along with the current position, distance from earth, and azimuth of the Sun and the Moon for a specific date at the queried time.

The Astronomy calculations are much more complex than producing an accurate result from obscure formulas throwing in a few numbers. There is always a tradeoff between the accuracy and computing time. Our Astronomy API focuses more on producing an acceptable results and has an accuracy of around one minute that is good enough for applications like sunrise/sunset timers but is not sufficient for astronomical purposes.

There are three ways to consume the Astronomy API:

  • Using any Location Address (preferrably, city address)
  • Using Geo Coordinates (latitude & longitude)
  • Using any IPv4 or IPv6 address

The three variations of Astronomy API take the different input sources to produce astronomical information along with location details. Here is how to consume each variation:

Getting Astronomical Information for an Address

You can pass the address of a location as query parameters location to get the astronomical information. Here is an example to get the astronomical information for address 'New York, US':

curl 'https://api.ipgeolocation.io/astronomy?apiKey=API_KEY&location=New%20York,%20US'

Response

1{
2  "location": {
3    "location": "New York, US",
4    "country": "United States",
5    "state": "New York",
6    "city": "New York",
7    "locality": "Clinton",
8    "latitude": 40.76473335,
9    "longitude": -74.00083980660943
10  },
11  "date": "2024-11-04",
12  "current_time": "08:13:22.978",
13  "sunrise": "06:30",
14  "sunset": "16:48",
15  "sun_status": "-",
16  "solar_noon": "11:39",
17  "day_length": "10:18",
18  "sun_altitude": 16.120293371563754,
19  "sun_distance": 148361706.39355108,
20  "sun_azimuth": 128.26575101712496,
21  "moonrise": "09:49",
22  "moonset": "18:29",
23  "moon_status": "-",
24  "moon_altitude": -14.81836182838389,
25  "moon_distance": 396331.25698682835,
26  "moon_azimuth": 113.85094239453929,
27  "moon_parallactic_angle": -51.40390182984849,
28  "moon_phase": "WAXING_CRESCENT",
29  "moon_illumination_percentage": "8.43",
30  "moon_angle": 33.74826084504092
31}

Location details like country name, state/province name, and city are provided in the response for the provided location that can be used for multiple purposes.

Getting Astronomical Information for Location Coordinates

You can pass the latitude and longitude of a location as query parameters lat and long to get the astronomical information. Here is an example to get the astronomical information for '-27.4748, 153.017' coordinates:

curl 'https://api.ipgeolocation.io/astronomy?apiKey=API_KEY&lat=-27.4748&long=153.017'

Response

1{
2  "location": {
3    "latitude": -27.4748,
4    "longitude": 153.017
5  },
6  "date": "2024-11-04",
7  "current_time": "23:28:59.711",
8  "sunrise": "04:54",
9  "sunset": "18:08",
10  "sun_status": "-",
11  "solar_noon": "11:31",
12  "day_length": "13:14",
13  "sun_altitude": -46.89343713201794,
14  "sun_distance": 148361706.39355108,
15  "sun_azimuth": 180.89506040990295,
16  "moonrise": "06:15",
17  "moonset": "20:46",
18  "moon_status": "-",
19  "moon_altitude": -25.365567944837675,
20  "moon_distance": 396297.670873638,
21  "moon_azimuth": 214.4609302018781,
22  "moon_parallactic_angle": 145.49291581261554,
23  "moon_phase": "WAXING_CRESCENT",
24  "moon_illumination_percentage": "8.49",
25  "moon_angle": 33.87230698192152
26}

Getting Astronomical Information for an IPv4 or IPv6 Address

You can pass any IPv4 or IPv6 address as a query parameter ip to get the astronomical information. Here is an example to get the astronomical information for the IP address '8.8.8.8':

curl 'https://api.ipgeolocation.io/astronomy?apiKey=API_KEY&ip=8.8.8.8'

Response

1{
2  "location": {
3    "continent_code": "NA",
4    "continent_name": "North America",
5    "country_code2": "US",
6    "country_code3": "USA",
7    "country_name": "United States",
8    "country_name_official": "United States of America",
9    "is_eu": false,
10    "state_prov": "California",
11    "state_code": "US-CA",
12    "district": "Santa Clara",
13    "city": "Mountain View",
14    "zipcode": "94043-1351",
15    "latitude": 37.4224,
16    "longitude": -122.08421
17  },
18  "date": "2024-11-04",
19  "current_time": "05:42:37.429",
20  "sunrise": "06:37",
21  "sunset": "17:06",
22  "sun_status": "-",
23  "solar_noon": "11:51",
24  "day_length": "10:29",
25  "sun_altitude": -11.233664962711332,
26  "sun_distance": 148361706.39355108,
27  "sun_azimuth": 101.18075504079837,
28  "moonrise": "09:56",
29  "moonset": "19:01",
30  "moon_status": "-",
31  "moon_altitude": -44.88852591210425,
32  "moon_distance": 396268.3036138962,
33  "moon_azimuth": 93.53924174509945,
34  "moon_parallactic_angle": -63.45786017771775,
35  "moon_phase": "WAXING_CRESCENT",
36  "moon_illumination_percentage": "8.54",
37  "moon_angle": 33.98061036315428
38}

Note: When you get the astronomical information through an IP address, API will also return the extra fields about place information along with the astronomical information.

Using Client or Machine IP Address

You can call the astronomy API without passing any coordinates or IP address as well. It will use the calling machine's IP address to return the regional astronomical information. Here is an example:

curl 'https://api.ipgeolocation.io/astronomy?apiKey=API_KEY'

Note:In above examples, we used API Key everywhere. Astronomy API can be called from client side JavaScript with Request Origin authentication as well on paid plans.

Response in Multiple Languages

The astronomy information lookup using an IP address can include geolocation information in the following languages:

  • English (en)
  • German (de)
  • Russian (ru)
  • Japanese (ja)
  • French (fr)
  • Chinese Simplified (cn)
  • Spanish (es)
  • Czech (cs)
  • Italian (it)
  • Persian (fa)
  • Korean (ko)

By default, the API responds in English. But you can change the response language by passing the language code as a query parameter lang. Here is an example:

curl 'https://api.ipgeolocation.io/astronomy?apiKey=API_KEY&ip=1.1.1.1&lang=cn'

Only the paid plan subscriptions can get the response in languages other than English. All the other subscriptions will only get the response in English.

Getting Astronomical Information for a Specific Date

Along with the above two variations, there is a third variation that you can get the astronomical information for a specific date. You can pass date query parameter with the date value. Here are some example calls for coordinates search along with date:

curl 'https://api.ipgeolocation.io/astronomy?apiKey=API_KEY&lat=-27.4748&long=153.017&date=2025-01-01'

Alternatively, you can also call this endpoint with ip address and date parameters:

curl 'https://api.ipgeolocation.io/astronomy?apiKey=API_KEY&ip=1.1.1.1&date=2025-01-01'

Note:Date must be in 'YYYY-MM-DD' format. No other format will be accepted and by default, current date will be used. All the astronomical information is calculated at the current time of day at the provided location.

Reference to Astronomy API Response

Field Type Description Can be empty?
location.location string Location is the decoded form of the string you provided for searching. Yes
location.continent_code string 2-letter code of the continent. Yes
location.continent_name string Name of the continent. Yes
location.country_code2 string Country code (ISO 3166-1 alpha-2) of the country. Yes
location.country_code3 string Country code (ISO 3166-1 alpha-3) of the country. Yes
location.country string Name of the country. It is returned in case of location search. Yes
location.country_name string Name of the country. Yes
location.country_name_official string Formal name of the country. Yes
location.is_eu boolean Is the country belong to European Union? Yes
location.state string Name of the state/province/region. It is returned in case of location search. Yes
location.state_prov string Name of the state/province/region. Yes
location.state_code string Code of the state/province/region. Yes
location.district string Name of the district or county. Yes
location.city string Name of the city. Yes
location.locality string Smaller area, part or region of a city. Yes
location.zipcode string ZIP/Postal code of the place. Yes
location.latitude float Latitude of the place. No
location.longitude float Longitude of the place. No
date string Provided or current date in the format 'yyyy-MM-dd'. No
current_time string Current time of the extracted location in the format 'HH:mm:ss.SSS'. No
sunrise string Time at which sun rises at the extracted location in the format 'HH:mm'. No
sunset string Time at which sun sets at the extracted location in the format 'HH:mm'. No
sun_status* string Represents the sun rise and sun set status. No
solar_noon string The time of day when the sun is at its highest point in the sky, in the format 'HH:mm'. No
day_length string The total length of daylight for the current day in format 'HH:mm', representing the time from sunrise to sunset. No
sun_altitude float The sun's altitude angle above the horizon at current_time, measured in degrees. No
sun_distance float The distance from Earth to the sun at current_time, in kilometers. No
sun_azimuth float The azimuth angle of the sun at current_time, indicating its compass direction in degrees. No
moonrise string Time at which moon rises at the extracted location in the format 'HH:mm'. No
moonset string Time at which moon sets at the extracted location in the format 'HH:mm'. No
moon_status* string Represents the moon rise and moon set status. No
moon_altitude float The moon's altitude angle above the horizon at current_time, measured in degrees. No
moon_distance float The distance from Earth to the moon at current_time, in kilometers. No
moon_azimuth float The azimuth angle of the moon at current_time, indicating its compass direction in degrees. No
moon_parallactic_angle float The angle between the celestial pole and the moon's position relative to the location, measured in degrees. No
moon_phase string The current phase of the moon (e.g., "WAXING_CRESCENT"), indicating its position in the lunar cycle. No
moon_illumination_percentage string The percentage of the moon's surface that is illuminated by sunlight, as viewed from Earth. No
moon_angle float The angular diameter of the moon as observed from Earth, measured in degrees. No

sun_status - Indicates the current state of the sun relative to the horizon. If both the sunrise and sunset values are not equal to "-:-", sun_status will be set to "-", indicating no special status. If the sun is continuously above the twilight angle, sun_status may be set to "Always above the twilight angle", indicating perpetual daylight. Conversely, if the sun remains below the twilight angle, the status may read "Always below the twilight angle", indicating that the sun does not rise within the current 24-hour period.

moon_status - Represents the current state of the moon in relation to the horizon. If both the moonrise and moonset values are not equal to "-:-", moon_status will be set to "-", indicating no special status. If the moon is always visible above the horizon, moon_status may be assigned "Always above the horizon", signifying that the moon does not set within the current 24-hour period. Conversely, if the moon is consistently below the horizon, it may read "Always below the horizon", indicating it does not rise during this timeframe.

Error Codes

Astronomy API returns HTTP status code 200 for a successful API request along with the response.

While, in case of a bad or invalid request, Astronomy API returns 4xx HTTP status code along with a descriptive message explaining the reason for the error.

Below is a detailed explanation of the specific HTTP status codes and their corresponding error conditions:

HTTP StatusDescription
400
Bad Request

It is returned for one of the following reasons:

  • If the provided location address is invalid or not found.

  • If special character(s) ( ) [ ] { } | ^ ` is passed in the API URL either as paramter or its value. Specially in case of API key.

  • If the provided date is not in the format 'yyyy-MM-dd'.

  • If the provided IP address does not found in our database.

401
Unauthorized

It is returned for one of the following reasons:

  • If API key (as apiKey URL parameter) is missing from the request to IP Geolocation API.

  • If an invalid (a random value) API key is provided.

  • If the API request is made from an unverified ipgeolocation.io account.

  • If your account has been disabled or locked to use by the admin due to abuse or illegal activity.

  • When the request to IP Geolocation API is made using API key for a database subscription

  • When the request to IP Geolocation API is made on the 'paused' subscription.

  • If you’re making API requests after your subscription trial has been expired.

  • If your active until date has passed and you need to upgrade your account.

405
Method Not Allowed
  • If wrong HTTP request method is used for calling the endpoint. Only GET method is allowed for /astronomy  endpoint requests.

429
Too Many Requests

It is returned for one of the following reasons:

  • If the API usage limit has reached for the free subscriptions, or paid subscriptions with the status 'past due', 'deleted' or 'trial expired'.

  • If the surcharge API usage limit has reached against the subscribed plan.

499
Client Closed Request
  • If the client has set the very short request or connection timeout, leading to the server closing the request prematurely.

5XX
Server Side Error
  • If a 500 (Internal Server Error), 502 (Bad Gateway), 503 (Service Unavailable), 504 (Gateway Timeout), or 505 (HTTP Version Not Supported) status code is returned, it indicates an issue on our end. Please contact us with your request at support@ipgeolocation.io for further assistance.

API SDKs

To facilitate the developers, we have added some SDKs for various programming languages. The detailed documentation on how to use these SDKs is available in the respective SDK's documentation page linked below.

Our SDKs are also available on Github. Feel free to help us improve them. Following are the available SDKs: