GitHub Rest Api Example and Uses

Git is the most popular revision control application and GitHub is a hosting service for git repositories, recently GitHub launch a new Rest API v3.0 and published on its official website.

You can access all Schema of Rest API URLs from here. This tutorial help to access GitHub rest call using rest client.

I am using the guzzle PHP rest client to access rest call and API supports Cross Origin Resource Sharing (CORS) for AJAX requests from any origin.

first of all, I will list down all available rest endpoints,

{
  "current_user_url": "https://api.github.com/user",
  "current_user_authorizations_html_url": "https://github.com/settings/connections/applications{/client_id}",
  "authorizations_url": "https://api.github.com/authorizations",
  "code_search_url": "https://api.github.com/search/code?q={query}{&page,per_page,sort,order}",
  "commit_search_url": "https://api.github.com/search/commits?q={query}{&page,per_page,sort,order}",
  "emails_url": "https://api.github.com/user/emails",
  "emojis_url": "https://api.github.com/emojis",
  "events_url": "https://api.github.com/events",
  "feeds_url": "https://api.github.com/feeds",
  "followers_url": "https://api.github.com/user/followers",
  "following_url": "https://api.github.com/user/following{/target}",
  "gists_url": "https://api.github.com/gists{/gist_id}",
  "hub_url": "https://api.github.com/hub",
  "issue_search_url": "https://api.github.com/search/issues?q={query}{&page,per_page,sort,order}",
  "issues_url": "https://api.github.com/issues",
  "keys_url": "https://api.github.com/user/keys",
  "notifications_url": "https://api.github.com/notifications",
  "organization_repositories_url": "https://api.github.com/orgs/{org}/repos{?type,page,per_page,sort}",
  "organization_url": "https://api.github.com/orgs/{org}",
  "public_gists_url": "https://api.github.com/gists/public",
  "rate_limit_url": "https://api.github.com/rate_limit",
  "repository_url": "https://api.github.com/repos/{owner}/{repo}",
  "repository_search_url": "https://api.github.com/search/repositories?q={query}{&page,per_page,sort,order}",
  "current_user_repositories_url": "https://api.github.com/user/repos{?type,page,per_page,sort}",
  "starred_url": "https://api.github.com/user/starred{/owner}{/repo}",
  "starred_gists_url": "https://api.github.com/gists/starred",
  "team_url": "https://api.github.com/teams",
  "user_url": "https://api.github.com/users/{user}",
  "user_organizations_url": "https://api.github.com/user/orgs",
  "user_repositories_url": "https://api.github.com/users/{user}/repos{?type,page,per_page,sort}",
  "user_search_url": "https://api.github.com/search/users?q={query}{&page,per_page,sort,order}"
}

Project Rest End Points

RouteMethodDescription
/projectsGETGet all projects
/projects/visibleGETGet a list of projects which the authenticated user can only see.
/projects/:idGETGet specific project details by project Id.
/projectsPOSTCreate a new Project.
/projects/user/:user_idPOSTCreate a new Project only for specific users.
/projects/:idPUTUpdated project by project ID.

Repository Rest End Points

RouteMethodDescription
/projects/:id/repository/treeGETGet all repositories of the specific project
/projects/:id/repository/archiveGETGet an archive of the repository.
/projects/:id/repository/contributorsGETGet all contributors of the specific repositories.

Repository Branch Rest End Points

RouteMethodDescription
/projects/:id/repository/branchesGETGet a list of repository branches from a project
/projects/:id/repository/branches/:branchGETGet details of a single repository branch.
/projects/:id/repository/branchesPOSTCreate a new repository branch.
/projects/:id/repository/branches/:branchDELETEDelete the repository branch.

List repository commits Rest Api

RouteMethodDescription
/projects/:id/repository/commitsGETGet a list of commits from a project repository
/projects/:id/repository/commitsPOSTCreate a commit on the repository branch.
/projects/:id/repository/commits/:shaGETGet single commit details.

Users Rest API

RouteMethodDescription
/usersGETGet a list of users
/users/:idGETGet single user details.
/usersPOSTCreate a new user.
/users/:idPUTUpdate user by userId.
/users/:idDELETEDelete user by userId.

How to pass parameters into GitHub Rest call using Curl

curl -i "https://api.github.com/repos/vmg/redcarpet/issues?state=closed"

How to access the above rest call using Guzzle PHP rest client,

Read: Simple Example and Demo of Guzzle

GitHub provides many rest clients that need to use authentication tokens, so access that rest endpoints user must be authorized. GitHub provides three types of user authentication processes.

Basic Authentication

curl -u "username" https://api.github.com

OAuth2 Token (sent in a header)

curl -H "Authorization: token OAUTH-TOKEN" https://api.github.com

OAuth2 Token (sent as a parameter)

curl https://api.github.com/?access_token=OAUTH-TOKEN

This is a very simple process of authentication, Rest needs a GitHub token to validate user authorization. You can get more information from here

Simple Example to get All project Using Github Private Token

<!-- wp:preformatted -->
<pre class="wp-block-preformatted">require_once 'vendor/autoload.php';
use GuzzleHttp\Client;
 
$client = new Client([
    // Base URI is used with relative requests
    'base_uri' => 'https://api.github.com/api/v3',
    // You can set any number of default request options.
    'timeout'  => 2.0,
            'headers' => ['Content-Type' => 'application/json', "Accept" => "application/json", "PRIVATE-TOKEN" => 'avxfskkslpopisaw'],
            //ssl false
            'verify' => false
]); 
 
$response = $client->request('GET', '/projects');
echo "</pre>
<!-- /wp:preformatted -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted">";
echo $response->getBody();
?></pre>
<!-- /wp:preformatted -->

I have passed the private token on "PRIVATE-TOKEN" key, You can get your private token by registering apps into git.