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:
Note: In below examples, we used API Key everywhere. Astronomy API can be called from client side JavaScript with Request Origin authentication as well on only paid plans.
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':
Response
1{
2 "location": {
3 "location_string": "New York, US",
4 "country_name": "United States",
5 "state_prov": "New York",
6 "city": "New York",
7 "locality": "Clinton",
8 "latitude": "40.76473",
9 "longitude": "-74.00084"
10 },
11 "astronomy": {
12 "date": "2025-04-24",
13 "current_time": "05:51:22.144",
14 "sunrise": "06:03",
15 "sunset": "19:45",
16 "sun_status": "-",
17 "solar_noon": "12:54",
18 "day_length": "13:42",
19 "sun_altitude": -3.0152536025828494,
20 "sun_distance": 150347202.36135986,
21 "sun_azimuth": 69.93188786672897,
22 "moonrise": "04:18",
23 "moonset": "16:07",
24 "moon_status": "-",
25 "moon_altitude": 16.78325682627405,
26 "moon_distance": 365570.33476424805,
27 "moon_azimuth": 113.77523077715728,
28 "moon_parallactic_angle": -44.181547442478056,
29 "moon_phase": "WANING_CRESCENT",
30 "moon_illumination_percentage": "-16.26",
31 "moon_angle": 312.4309152252624
32 }
33}
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:
Response
1{
2 "location": {
3 "latitude": "-27.47480",
4 "longitude": "153.01700"
5 },
6 "astronomy": {
7 "date": "2025-04-24",
8 "current_time": "20:09:43.894",
9 "sunrise": "06:09",
10 "sunset": "17:22",
11 "sun_status": "-",
12 "solar_noon": "11:45",
13 "day_length": "11:13",
14 "sun_altitude": -37.65833389225631,
15 "sun_distance": 150347202.36135986,
16 "sun_azimuth": 265.3769249305037,
17 "moonrise": "01:52",
18 "moonset": "14:55",
19 "moon_status": "-",
20 "moon_altitude": -55.15253040725513,
21 "moon_distance": 365506.13858637254,
22 "moon_azimuth": 198.19587897138103,
23 "moon_parallactic_angle": 163.8276392254988,
24 "moon_phase": "WANING_CRESCENT",
25 "moon_illumination_percentage": "-16.15",
26 "moon_angle": 312.60236927414405
27 }
28}
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':
Response
1{
2 "ip": "8.8.8.8",
3 "location": {
4 "continent_code": "NA",
5 "continent_name": "North America",
6 "country_code2": "US",
7 "country_code3": "USA",
8 "country_name": "United States",
9 "country_name_official": "United States of America",
10 "is_eu": false,
11 "state_prov": "California",
12 "state_code": "US-CA",
13 "district": "Santa Clara",
14 "city": "Mountain View",
15 "zipcode": "94043-1351",
16 "latitude": "37.42240",
17 "longitude": "-122.08421"
18 },
19 "astronomy": {
20 "date": "2025-04-24",
21 "current_time": "03:11:49.137",
22 "sunrise": "06:21",
23 "sunset": "19:52",
24 "sun_status": "-",
25 "solar_noon": "13:06",
26 "day_length": "13:31",
27 "sun_altitude": -31.60250693222838,
28 "sun_distance": 150347202.36135986,
29 "sun_azimuth": 36.50375780575871,
30 "moonrise": "04:32",
31 "moonset": "16:30",
32 "moon_status": "-",
33 "moon_altitude": -15.426423427760831,
34 "moon_distance": 365498.85191422235,
35 "moon_azimuth": 85.59169056829211,
36 "moon_parallactic_angle": -52.75408030634809,
37 "moon_phase": "WANING_CRESCENT",
38 "moon_illumination_percentage": "-16.14",
39 "moon_angle": 312.62186391379714
40 }
41}
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:
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)
- Korean (ko)
- Persian (fa)
- Portuguese (pt)
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:
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:
Alternatively, you can also call this endpoint with ip address and date parameters:
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.
What's New in /v2/astronomy
Below are the key updates introduced in the /v2/astronomy
API endpoint compared to the previous /astronomy
version:
- All astronomy-related data is now grouped under a single
astronomy
object, making the response more consistent with our overall API structure. - When calling
/v2/astronomy
without any parameters or withip=
, the response now includes anip
field to indicate the IP address used for the lookup. - When the
location=
parameter is used, thelocation
object now follows the same field naming conventions as in IP-based queries for consistency across responses:location.location
→location.location_string
location.country
→location.country_name
location.state
→location.state_prov
Reference to Astronomy API Response
Below we have provided separate tables for location
object and astronomy
object fields that can be returned by Astronomy API.
• Standalone fields reference
Field | Type | Description | Can be empty? |
---|---|---|---|
ip | string | The IP address used for the astronomy lookup. Returned when queried using the ip= parameter or with no parameters. | Yes |
• location
json object reference
Field | Type | Description | Can be empty? |
---|---|---|---|
location_string | string | Location is the string, you provided for searching. | Yes |
continent_code | string | 2-letter code of the continent. | Yes |
continent_name | string | Name of the continent. | Yes |
country_code2 | string | Country code (ISO 3166-1 alpha-2) of the country. | Yes |
country_code3 | string | Country code (ISO 3166-1 alpha-3) of the country. | Yes |
country_name | string | Name of the country. | Yes |
country_name_official | string | Formal name of the country. | Yes |
is_eu | boolean | Is the country belong to European Union? | Yes |
state_prov | string | Name of the state/province/region. | Yes |
state_code | string | Code of the state/province/region. | Yes |
district | string | Name of the district or county. | Yes |
city | string | Name of the city. | Yes |
locality | string | Smaller area, part or region of a city. | Yes |
zipcode | string | ZIP/Postal code of the place. | Yes |
latitude | float | Latitude of the place. | No |
longitude | float | Longitude of the place. | No |
• astronomy
json object fields reference
Field | Type | Description | Can be empty? |
---|---|---|---|
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 atcurrent_time , measured in degrees. | No |
sun_distance | float | The distance from Earth to the sun atcurrent_time , in kilometers. | No |
sun_azimuth | float | The azimuth angle of the sun atcurrent_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 atcurrent_time , measured in degrees. | No |
moon_distance | float | The distance from Earth to the moon atcurrent_time , in kilometers. | No |
moon_azimuth | float | The azimuth angle of the moon atcurrent_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
andsunset
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 themoonrise
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 Status | Description |
---|---|
400 Bad Request | It is returned for one of the following reasons:
|
401 Unauthorized | It is returned for one of the following reasons:
|
405 Method Not Allowed |
|
429 Too Many Requests | It is returned for one of the following reasons:
|
499 Client Closed Request |
|
5XX Server Side Error |
|
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: