Episode: 3472 Title: HPR3472: consuming an AQI API Source: https://hub.hackerpublicradio.org/ccdn.php?filename=/eps/hpr3472/hpr3472.mp3 Transcribed: 2025-10-25 00:03:58 --- This is Haka Public Radio episode 3472 for Tuesday, 23rd of November 2021, today's show is entitled, Consuming an AQI API and is part of the series, Programming 101, it is hosted by Jesra, and is about six minutes long, and carries a clean flag. The summary is just because the sky is clear, doesn't mean the air is safe to breathe. Hello, my name is Jesra, I'm a Californian, and will we was the air quality bad this summer? For those who don't know, this summer there were multiple mega fires on the west coast of North America, and the resulting ash and soot from those fires made it literally hazardous to breathe outside. The measurement of those particles in the air is known as the AQI, the air quality index. Primarily, there are two things that are measured when determining the air quality index, ozone levels, and PM 2.5. Now ozone is good at high altitudes, but it is not that great when it is on the Earth's surface and close to the ground. PM 2.5 is particulate matter that is 2.5 micrometers or smaller, and when a mega fire is raging, the ash and soot coming from that fire is going to be PM 2.5. When the afternoon sky is dark, and everything looks like a scene out of more door, and stepping outside, taking a breath is painful, then it's pretty obvious that PM 2.5 levels are very high. However, there are plenty of times when looking at the sky and going outside and taking a breath will not let me know if the PM 2.5 levels are dangerously high. To solve this issue, I started checking online to see what the AQI was in my area every morning, and I would determine whether or not I should bring an extra mask along with me that day. Determining the air quality was quite simple. I could visit airnail.gov, enter my zip code, and get a result of the AQI. My zip code covers an area of 139.56 square miles, but the result is accurate enough for my needs. When I would visit the website, I would enter my zip code, hit enter, and the screen would not refresh. This means that the interface was making an API call to a back end server. As soon as I realized that the data I was interested in was available by an API, gears started turning, and I started thinking of ways to more efficiently provide that information to myself. Hot digity. Sure would be nice if the AQI status was emailed to my phone every hour, if the AQI was above a certain threshold. The first step in this process involves determining exactly what information is sent to the API in order to get the data returned as a response. The easiest way to do this is to use the developer tools in Firefox, and here's how that process went for me. Step 1. Open Firefox, though web browser. Go to HTTPS, colon, forward slash, forward slash, www.airnow.gov. From there, I open the developer tools that can be done either through the menu or with Control Shift I. In the developer tools, select the Network tab. In the web page, enter the zip code in the form and submit it. After that, watch the Network tab for a post request to HTTPS, colon, forward slash, forward slash, airnow.gov api.com slash reporting area slash Jets. Click on that request in the Network tab. When that happens, another set of tabs are now available to display various bits of information regarding the request. From this data, it is possible to recreate the query. However, I took an even easier route, and right-click on the query in the Network tab, and selected Copy, Copy as Curl. To get the request as a Curl command, complete with all necessary arguments pre-filled. Since I didn't want to write my entire AQI fetching script in Bash, I copied the Curl command into a text file and ported the request to the Ruby programming language. The complete finished script is available in the show notes. The script now exists on a Raspberry Pi, and a Krontask runs the script every day, every hour from 7am until 7pm. As the script runs, it collects data from the api endpoint if the AQI is above 70, which means it is a moderate hazard. Then the collected data is formatted, and run through a script called TextJesra, which doesn't actually text me anymore, it now sends an email to me that I get on my phone. Now, when I'm out and about doing some errands in town or whatnot, I may get an email on my phone that says the AQI is moderately hazardous, and in which case I know that I should put on a mask, even if I can't smell smoke, and the sky looks to be clear. The script that I wrote could easily have been written in Python or in some other programming language. Ruby just happened to be what I had laying around at the time, so I just picked it up and hammered out the code. As far as consuming an api goes, it was a fairly easy, straightforward process. What about you, HPR listener? Is there an api that you've written a script to consume and process the data? If so, have that recording an episode about it, I'd love to hear it. Thank you. Have a wonderful day. You've been listening to Hecker Public Radio at HeckerPublicRadio.org. Today's show was contributed by an HPR listener like yourself. If you ever thought of recording a podcast, then click on our contribute link to find out how easy it really is. Hosting for HPR is kindly provided by an honest host.com, the internet archive and our sync.net. Unless otherwise stated, today's show is released under Creative Commons, Attribution, Share