Using Open Data APIs to Find Local Sunset, Sunrise, and Moon Phase

Bill Doerrfeld | December 1, 2020

While on vacation last year I had a little fun writing some basic API use cases. I just wanted some simple landing pages that would show me helpful celestial data. When is sunset in my local timezone? When is the sunrise? What phase is the moon in?


You can view these three websites here: Sunset, Sunrise, Phase. Each should calculate each question correctly based on your local timezone. Below, I explain the process and link to the Github repos. 


sunset 🌇

Github

sunset is a super simple website that uses open data APIs to display the sunset time in your local timezone. Sunset demonstrates how to use HTTP GET calls to open data APIs. First, the code calls the GeoIPLookup.io API to return latitude and longitude coordinates based on the caller's IP address. Next, it passes these coordinates as parameters in a call to the Sunrise-Sunset API. The Sunrise-Sunset API returns the UTC timestamp for the sunset time. Lastly, sunset converts UTC into local time. It uses some JS functions to do that, and some janky slice methods to output what we want to see.


sunrise 🌄

Github

Sunrise is a super simple website that uses open data APIs to display the sunrise time in your local timezone. Sunrise demonstrates how to use HTTP GET calls to open data APIs. First, the code calls the GeoIPLookup.io API to return latitude and longitude coordinates based on the caller's IP address. Next, it uses passes these coordinates as parameters in a call to the Sunrise-Sunset API. The Sunrise-Sunset API returns the UTC timestamp for the sunrise time. Lastly, sunrise converts UTC into local time. It uses some JS functions to do that, and some janky slice methods to output what we want to see.


phase 🌘

Github

The moon is pretty lit. But just how lit is it? This simple one page site visually represents the current phase of the moon. I built it to demo an introductory example of practical API use cases. It's just a phase. phase calls the Farmsense API to display moon data. The Farmsense API is a free-to-use open API that returns useful moon phase and illumination data. After much research, it was the only free service I could find that returned the data I wanted. Given a unix timestamp, Farmsense returns information on the current moon phase, illumination percentage, and colloquial phase name. Depending on what the API returns, the script loads different images to reflect the current moon phase and illumination percentage. I also added a little bit of my own scientific interpretation on how lit the moon is.


🌘🌓🌔🌕🌖🌗🌒


Disclaimer

I built these projects to demo an introductory lesson on practical API use cases. As they are strictly demos they are not that great. Since Sunset and Sunrise depend on the caller's IP address, mobile use may return an incorrect time. Code is not elegant, it's just for sample purposes.


Large action models LAMs story Bill Doerrfeld The New Stack
By Bill Doerrfeld June 10, 2025
AI researchers are calling the next class of models large action models (LAMs). For The New Stack, I explored what LAMs are, what examples are emerging in the market, and what experts think.
7 proven AI prompting strategies for coding to try today
By Bill Doerrfeld June 9, 2025
My article for LeadDev explores specific prompting techniques proven to sharpen your AI-assisted software development.
Nordic APIs ranked #1 API blog on the web
By Bill Doerrfeld June 7, 2025
Nordic APIs, the API-specific blog I edit, was recently ranked the top API blog online by FeedSpot. After ten years managing this presence, I reflect a bit on the journey thus far.
Tips to improve your AI vibe coding
By Bill Doerrfeld June 3, 2025
Developers are realizing that being productive with AI coding assistants takes a lot more than just asking nicely. There's real craft to it.
AI coding is the easy part. Now it's time to focus on production.
By Bill Doerrfeld May 30, 2025
AI coding is the easy part. Now it's time to focus on DevOps to get it into production. In a recent interview for LeadDev's DirectorPlus, Honeycomb's CTO, Charity Majors, shares expert tips on how to accomplish this.
New tools help LLM devs improve training data decisions
By Bill Doerrfeld May 29, 2025
My latest for The New Stack explores new research from Ai2, DataDecide, which helps LLM developers make better decisions around training data.
MCP security vulnerabilities
By Bill Doerrfeld May 21, 2025
My APISEC|CON talk covers the hype around agentic AI and MCP, and delves into inherent flaws in MCP architectures and suggests mitigations.
Knowing when to use AI coding assistants Doerrfeld InfoWorld
By Bill Doerrfeld May 6, 2025
AI coding assistants are a productivity dream in some cases — and a debugging nightmare in others. So, where’s the line?
How semantic caching reduces LLM API calls
By Bill Doerrfeld May 5, 2025
Semantic caching is like typical caching, but for AI. It could eliminate a lot of redundant API calls to LLMs, reducing costs and improving performance.
Using agentic AI for business workflows
By Bill Doerrfeld April 30, 2025
For CIO.com, leading executives shared with me how they're actively utilizing agentic AI to enhance core business workflows.
More Posts