Serving an API from a Static Site Host for Almost Free
This site is a static site built with Jekyll and hosted by Netlify for free under their “Starter” tier. Static sites are great for blogs and websites where the information doesn’t change very often because they can be served very efficiently with very small load times. My first blog was built with Wordpress and hosted on Bluehost and the first time I had a post hit the front page of Hacker News, the MySQL database got overwhelmed and everything broke. The next time I hit the front page of Hacker News I had switched to a static site and even a shared host like Bluehost was able to deliver my blog post over 30,000 times in a single day.
There are a number of limitations I considered before using a static site to serve an API:
- With a static site, you can’t provide an endpoint for any HTTP verb other than
GET. This means that the API consumer cannot really interact with or manipulate the underlying data in any way.
POSTrequests mean no automatic user signup. I could probably use a different service for user sign ups but where’s the fun in that.
- Netlify has made deploying a breeze, but I don’t have access to the underlying web server to set up any authentication on my pages or API.
Living within those limitations, I decided to make the Utah coronavirus data that I have been gathering available to the public. I’m not sure if anyone would have a use for it or even see this but I was interested in finding the limitations of Netlify as a static API host.
Making a static API is as simple as generating a JSON response for each possible API request and storing it statically as a
.json file. Handling 404s is a little wonky because it serves an HTML 404 page. For a project that nobody will likely ever see, that’s good enough.
The real magic is a once-daily job that I have running on a Raspberry Pi that has to:
- Collect the data
- Store it in the Google Sheet that generates the graphs
- Generate the new JSON file
- Commit the JSON file to the Github repo attached to the Netlify instance
- Push the commit to Github
If you’re interested to learn more about this setup, feel free to reach out via email or Twitter and I can go into more detail.