Updated April 15, 2023
Introduction to GitHub API
GitHub Application Program interface (API) is software that acts as an interface between Users and GitHub which offers a collaboration platform for developers to manage software version control, draw information useful for code development from its contents repository. Through these APIs, developer reaches libraries and pull public information freely and private information with proper authentication.
Users, apart from maintaining codes with version controls in GitHub, can create other content in the repository using APIs and share it with the development community. APIs facilitate social coding with contributions from various developers and social networking of software developers using Platform.
Usage of GitHub API
Before getting into the usage of API let’s analyze what is ReST APIs.
ReST API
It is also an application interface that is compatible with REST architectural style and it enables developers to interact with RESTful Web services. The meaning of REST is Representational State Transfer. It is not a standard or Protocol but it is a set of constraints that can be implemented by developers in several ways.
When the request for an object, made by a client in ReSTful API, is executed produces results in one of the formats: JSON (Javascript Object Notation) or HTML or PHP or Text. JSON is the accepted format and it is easy to read and also language agnostic.
Usage
A command to access GitHub Repository should be in the form of a URL sent through any browser and the sample URL is https://api.github.com/users/ABCDEFG/repos. This URL will return the first 30 repos for the user name ABCDEFG from the GitHub repository in JSON format.
Various steps in the usage of APIs
- Connecting to API Server Connection to the server (GitHub API) can be established through xml http request and data objects can be sought from it. Most of the browsers have these requests as built-in functions and enable seeking, receiving, and sending data from API servers.
- API Function Wrapper Creation The complete API call can be wrapped into a function so that the user name can be passed dynamically and get the information about the user.
- Create a new HttpRequest object (thru instantiation) Through the browser console a new object for HttpRequest should be created.
- Defining API Endpoint There are several endpoints available in GitHub and one should select the right endpoint depending on the data to be extracted from the repository. As an example, data can be requested for a particular user by passing on the corresponding user name in the call function.
- Get connected to Server While connecting to the server, Users will have to specify whether they are going to take the data from the server or put the data into the server.
- Sending the Request to the server and Parsing the data from the server After establishing the connection with the server, users need to declare the kind of operation they intend to do in the program and the corresponding data transfer type will have to be used.
- Results of the request. The response from the API for the requested will be listed can be viewed by the users. By default, the first 30 results will be displayed.
- Retrieving select data from the results. From the dataset returned by API, users can extract select data that they want by simply writing a code and displaying them.
GitHub API Examples
Extracting a user’s followers list
URL to get this information is
https://api.github.com/ABCDEFGH/followers
This URL will extract all the followers of the user ABCDEFGH and the details of 30 such followers will be made available for viewing and it will be in JSON format. Details like
- Login Name – YYYYYYY
- User Id – 12345639
- Node Id – MDQDKDK202
- Avatar URL – https://avatars2.githubuercontent.com/…. Hoe the user is represented in internet
- Gravatar id – Nil
- URL- User – https://api.github.com/users/YYYYYYY
- Html_url – https:/github.com/YYYYYYY
- Followers URL – https://api.github.com/users/YYYYYYY/followers
- Following URL – https://api.github.com/users/YYYYYYY/following
- glst URL – https://api.github.com/users/YYYYYYY/glst
- starred URL – https://api.github.com/users/YYYYYYY/starred
- Subscription URL – https://api.github.com/users/YYYYYYY/subscriptions
- Organisation URL – https://api.github.com/users/YYYYYYY/orgs
- Repos URL – https://api.github.com/users/YYYYYYY/repos
- Events URL – https://api.github.com/users/YYYYYYY/events
- Received events URL – https://api.github.com/users/YYYYYYY/received events
- Type – User
- Site Admn – False
Like this details of 30 such followers will be listed for User’s viewing
Extracting a user’s information
URL to get this information is
https://api.github.com/users/{username}
https://api.github.com/users/ABCDEFGH
The output from API post-execution of this URL, for user ABCDEFGH, will be listed in JSON format
- Login Name – ABCDEFGH
- User Id – 12345639
- Node Id – MDQDKDK202
- Avatar URL – https://avatars2.githubuercontent.com/…. Hoe the user is represented in internet
- Gravatar id – Nil
- URL- User – https://api.github.com/users/ABCDEFGH
- Html_url – https://github.com/ABCDEFGH
- Followers URL – https://api.github.com/users/ABCDEGH/followers
- Following URL – https://api.github.com/users/ABCDEFGH/following
- glst URL – https://api.github.com/users/ABCDEFGH/glst
- starred URL – https://api.github.com/users/ABCDEFGH/starred
- Subscription URL – https://api.github.com/users/ABCDEFGH/subscriptions
- Organisation URL – https://api.github.com/users/ABCDEFGH/orgs
- Repos URL – https://api.github.com/users/ABCDEFGH/repos
- Events URL – https://api.github.com/users/ABCDEFGH/events
- Received events URL – https://api.github.com/users/ABCDEFGH/received events
- Type – User
- Site Admn – False
Executing commands with help of tokens
Accessing private data as well the creation of repository / gists require authentication in the form of tokens and tokens will be generated using command
https://github.com/settings/tokens
These tokens will get only be displayed and they will have to be preserved by users and should be made use of in further commands
GitHub API List
Remote branches in a repository can be obtained by using the command
git branch -a to list both local and remote branches
git branch -r to list remote branches only
git branch to list local branches
git remote show to see remote branches and their metadata
Conclusion
API enables community developers to post their contributions easily for the benefit of users. It also produces very good documentation and helps developers to showcase their work to the public by posting all their work with help of APIs.
Recommended Articles
This is a guide to GitHub API. Here we discuss the various steps in the usage of GitHub APIs along with examples in detail. You may also have a look at the following articles to learn more –