Six Colors
Six Colors

by Jason Snell & Dan Moren

This Week's Sponsor

Kolide believes that maintaining endpoint security shouldn’t mean compromising employee privacy. Check out our manifesto: Honest Security.

By Jason Snell

Adding Apple WeatherKit data to my own weather widgets

A prototype iOS widget

I’ve spent 17 years with a weather station in my backyard, and parsing that data in interesting ways. Everybody needs a hobby.

At WWDC 2022, Apple announced that it was releasing its follow-on from Dark Sky, which it acquired in 2020: it’s called WeatherKit, and it will be available to apps on Apple’s platforms (via Swift), as well as on the web and other platforms (via a web-based REST API). App developers, who pay $100 a year to Apple for the privilege, will get 500,000 API queries per month as a part of their membership—and the prices go up from there.

Last week I had a moment of realization: I pay for an Apple developer account for various reasons. That means I’ve got access to WeatherKit! And that means that after years of building ways to visualize the current and past weather at my house, I could consider the future. (And yes, I could’ve done this years ago with a different weather API like OpenWeatherMap—which offers a million queries a month for free!—but I didn’t. And I’ve come to respect Dark Sky’s data. The opportunity to use a brand-new Apple service seemed too good to pass up.)

All my recent efforts parsing JSON data for things like air-quality widgets and menu bar items were sure to pay off because, at its core, Apple’s new web API is just passing back JSON. But unlike many other web APIs, which let you pass your API key in a URL string, Apple has chosen a more complicated approach.

First, you’ve got to set up a bunch of stuff on Apple’s developer site. Apple has to generate a private key and a service ID for you, and then you need to construct a JSON web token and include it in the header of API request you make in order to log in. I got most of the way there to implementing this in python using pyjwt, but I made one tiny error that I only realized after watching Simon Barker’s walkthrough of WeatherKit setup.

weather forecast in menu bar

Once that was all done, the avalanche of WeatherKit forecast data began to roll in. Now my challenge is to decide how to present the information. I’ve added a forecast graphic to my SwiftBar weather-status menu item and placed an SF Symbol representing the rest-of-day forecast next to the current temperature at my house, as relayed from my own weather station. It’s a work in progress, but I’m encouraged.

The next step is to revamp my two small iPhone and iPad weather home screen widgets, built in JavaScript using Scriptable, and turn them into a single medium-sized widget containing temperature, air quality, and a weather forecast in the style of my dearly departed Weather Line—all in one. My JavaScript skill is poor, and building widgets in Scriptable has its own challenges, but yesterday I managed to adapt an hourly weather widget to use my data sources and display days instead of hours. It’s ugly, but there’s plenty of time for me to make it prettier.

If you’re interested in playing with the WeatherKit REST API, I’ve got a sample script for you that parses some basic data and generates a basic bar chart. You’ll need to be an Apple Developer, of course. But if you are already one—or know someone who will provide you with an API key and access to those sweet 500,000 free calls—you might find accessing it to be a fun project.

If you appreciate articles like this one, support us by becoming a Six Colors subscriber. Subscribers get access to an exclusive podcast, members-only stories, and a special community.

Search Six Colors