DEV Community

Josh Kasuboski
Josh Kasuboski

Posted on • Originally published at joshkasuboski.com on

9 6

Serve a JSON API with GitHub

I wanted to add stats to a site, but I already capture them in a GitHub Repo. Let's just pull from there.

The Stats Repo

I made a repo that pulls in stats (kasuboski/stats). It uses a GitHub Action I made for a Dev.to Hackathon that pulls post stats from Dev.to.

The repo gets periodically updated with a stats/dev-to.json file. GitHub lets you browse the contents of files at raw.githubusercontent.com. In my case, this file is at https://raw.githubusercontent.com/kasuboski/stats/main/stats/dev-to.json.

Fetching the data

I have a landing page served from my Raspberry Pi Cluster. It was a placeholder with a link to my personal site. Now it also shows stats from my Dev.to posts.

Stats on the landing page

The landing page itself is just vanilla HTML/CSS/JS. It uses mvp.css to get quick styles. You can find the code at kasuboski/joshcorp-site. The javascript needed to add the stats is below. It's just in a script tag in the body.

function getStats() {
  const stats = document.querySelector('#stats');
  const reactions = document.querySelector('#reactions-value');
  const views = document.querySelector('#views-value');
  const url = 'https://raw.githubusercontent.com/kasuboski/stats/main/stats/dev-to.json';
  fetch(url)
    .then(res => res.json())
    .then(data => {
      console.log(data);
      reactions.innerText = data.public_reactions_count;
      views.innerText = data.page_views_count;
      stats.style.display = "block";
    })
    .catch(err => {
      console.error('Error fetching stats: ', err);
    })
}

window.onload = getStats;

I'm sure this probably isn't something GitHub exactly recommends… but as long as you don't have too much traffic it should be fine.

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (2)

Collapse
 
stephenwhitmore profile image
Stephen Whitmore • Edited

I like this idea for data that's public. I store notes publically in a GitHub repo and have been dancing around the idea of building a webapp that displays them for easier reading. They're all markdown files rather than json though.

Collapse
 
raghavmisra profile image
Raghav Misra

What you could do, is fetch the contents, use a parser such as remarkable to convert it to HTML and either serve it from a server, or set something's innerHTML! That's how I built a blog two years ago :D (I used this GitHub method too).

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay