DEV Community

Cover image for Simple way to use the github REST api
Sooraj (PS)
Sooraj (PS)

Posted on

Simple way to use the github REST api

Hey there all you guys. In this article I will show you how to use the github api to fetch your favorite repository information. No dependencies required!!. We will be using only javascript to fetch and display the data in a HTML file.

Note: This is a simple snippet for fetching a public repository data.

Let us first setup a simple JSON fetch inside out script tag like this

<!DOCTYPE html>
<html lang="en">
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <div class="my-project"></div>
    const searchQueryURL = '';

    window.addEventListener('DOMContentLoaded', function(e) {
      return fetch(searchQueryURL)
      .then(result => result.json())
      .then(response => console.log(response))
      .catch(err => console.log(err))
Enter fullscreen mode Exit fullscreen mode

You can see that I am using the Promise approach. You can also you async await methods, but I prefer going with promises because it's easier for me.

If we run this html file on the browser, our code will make a call to and you will receive a response object. This object will contain all the possible URLs that you can access from github. This is especially useful if you do not know what are the api routes available. These routes will also have query syntax to help you with understanding how to send the query params.You will receive something like this on your browser console.

  "current_user_url": "",
  "current_user_authorizations_html_url": "{/client_id}",
  "authorizations_url": "",
  "code_search_url": "{query}{&page,per_page,sort,order}",
  "commit_search_url": "{query}{&page,per_page,sort,order}",
  "emails_url": "",
  "emojis_url": "",
  "events_url": "",
  "feeds_url": "",
  "followers_url": "",
  "following_url": "{/target}",
  "gists_url": "{/gist_id}",
  "hub_url": "",
  "issue_search_url": "{query}{&page,per_page,sort,order}",
  "issues_url": "",
  "keys_url": "",
  "label_search_url": "{query}&repository_id={repository_id}{&page,per_page}",
  "notifications_url": "",
  "organization_url": "{org}",
  "organization_repositories_url": "{org}/repos{?type,page,per_page,sort}",
  "organization_teams_url": "{org}/teams",
  "public_gists_url": "",
  "rate_limit_url": "",
  "repository_url": "{owner}/{repo}",
  "repository_search_url": "{query}{&page,per_page,sort,order}",
  "current_user_repositories_url": "{?type,page,per_page,sort}",
  "starred_url": "{/owner}{/repo}",
  "starred_gists_url": "",
  "user_url": "{user}",
  "user_organizations_url": "",
  "user_repositories_url": "{user}/repos{?type,page,per_page,sort}",
  "user_search_url": "{query}{&page,per_page,sort,order}"
Enter fullscreen mode Exit fullscreen mode

Now as an example I will use the repos api to query the data from one of my public repositories. For this we will change the url to

const searchQueryURL = '';
Enter fullscreen mode Exit fullscreen mode

using the syntax of{owner}/{repo}

You will now recieve a JSON object containing all the information about that repository. Something like this

  // ... other info
  "name": "tooltip-sequence",
  "full_name": "SoorajSNBlaze333/tooltip-sequence",
  "private": false,
  "html_url": "",
  "description": "A simple step by step tooltip helper for any site",
  "fork": false,
  "url": "",
  "stargazers_count": 146,
  "watchers_count": 146,
  "language": "JavaScript",
  "has_issues": true,
  "has_projects": true,
  "has_downloads": true,
  "has_wiki": true,
  "has_pages": false,
  "forks_count": 5,
  "mirror_url": null,
  "archived": false,
  "disabled": false,
  "open_issues_count": 0,
  "forks": 5,
  "open_issues": 0,
  "watchers": 146,
  "default_branch": "master",
  "temp_clone_token": null,
  "network_count": 5,
  "subscribers_count": 3
  // ... other info
Enter fullscreen mode Exit fullscreen mode

I have removed some of the large urls before pasting to avoid taking too much space. From this data you can create your own representations of your projects on your blogs or your personal sites. Something like this

Alt Text

For further informations and documentation, please do refer

Hope you guys like it. :)

Top comments (5)

sabarishcodes profile image
Sabarish Rajamohan

Cool stuff. Will definitely try it out.
Quick question Sooraj, do you have any resources for Oauth in Github??

soorajsnblaze333 profile image
Sooraj (PS)

Hey Sabarish, Glad you found it useful :)

I do not currently have an article for Oauth, but you can check out the documentation here ->

As your question, I shall definitely add resources for other actions for github. :)

sabarishcodes profile image
Sabarish Rajamohan

Thanks Sooraj. Looking forward to it!

hasanelsherbiny profile image
Hasan Elsherbiny

nice article bro i have also written an article comparing between REST Apis and Soap

if you are interested give it a chance

patelvimal profile image
vimal patel

Is there any API to get all the public repositories which exists in Github?