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}" }
Table of Contents
Project Rest End Points
Route | Method | Description |
---|---|---|
/projects | GET | Get all projects |
/projects/visible | GET | Get a list of projects which the authenticated user can only see. |
/projects/:id | GET | Get specific project details by project Id. |
/projects | POST | Create a new Project. |
/projects/user/:user_id | POST | Create a new Project only for specific users. |
/projects/:id | PUT | Updated project by project ID. |
Repository Rest End Points
Route | Method | Description |
---|---|---|
/projects/:id/repository/tree | GET | Get all repositories of the specific project |
/projects/:id/repository/archive | GET | Get an archive of the repository. |
/projects/:id/repository/contributors | GET | Get all contributors of the specific repositories. |
Repository Branch Rest End Points
Route | Method | Description |
---|---|---|
/projects/:id/repository/branches | GET | Get a list of repository branches from a project |
/projects/:id/repository/branches/:branch | GET | Get details of a single repository branch. |
/projects/:id/repository/branches | POST | Create a new repository branch. |
/projects/:id/repository/branches/:branch | DELETE | Delete the repository branch. |
List repository commits Rest Api
Route | Method | Description |
---|---|---|
/projects/:id/repository/commits | GET | Get a list of commits from a project repository |
/projects/:id/repository/commits | POST | Create a commit on the repository branch. |
/projects/:id/repository/commits/:sha | GET | Get single commit details. |
Users Rest API
Route | Method | Description |
---|---|---|
/users | GET | Get a list of users |
/users/:id | GET | Get single user details. |
/users | POST | Create a new user. |
/users/:id | PUT | Update user by userId. |
/users/:id | DELETE | Delete 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.