GitHub as a web application is a huge and complex entity. Think about all the repositories, users, branches, commits, comments, SSH keys and third party apps that are a part of it. Moreover, there are multiple ways of communicating with it. There are desktop apps for GitHub, extensions for Visual Studio Code and Atom Editor, git cli, Android and iOS apps to name a few.
People at GitHub, and third party developers alike, can’t possibly manage all this complexity without a common interface. This common interface is what we call the GitHub API. Every GitHub utility like a cli, web UI, etc uses this one common interface to manage resources (resources being entities like repositories, ssh keys, etc).
In this tutorial we will learn a few basics of how one interfaces with an API using GitHub API v3 and Python3. The latest v4 of GitHub API requires you to learn about GraphQL which results in steeper learning curve. So I will stick to just version three which is still active and pretty popular.
Web APIs are what enable you to use all the services offered by a web app, like GitHub, programmatically using language of your choice. For example, we are going to use Python for our use case, here. Technically, you can do everything you do on GitHub using the API but we will restrict ourselves to only reading the publicly accessible information.
Your Python program will be talking to an API just the same way as your browser talks to a website. That is to say, mostly via HTTPS requests. These requests will contain different ‘parts’, starting from the method of the request [GET, POST, PUT, DELETE], the URL itself, a query string, an HTTP header and a body or a payload. Most of these are optional. We will however need to provide a request method and the URL to which we are making the request.
What these are and how they are represented in an HTTPS request is something we will see slow as we start writing Python Scripts to interact with GitHub.