Repositories

Learn and practice the functionalities of repositories in the GitHub API.

Overview

Repositories are containers where the complete project and its files are presented on GitHub. In addition, various versions of the files also exist in that repository. The owner of the repository can be the person themselves or anyone they would like to share the ownership with. We can create branches inside a repository where each type of file can be isolated from another.

Create a repository

Instead of actually moving to the website and manually creating the repository, we can rather generate an HTTP POST request to do so.

Press + to interact
GitHub repository creation endpoint
GitHub repository creation endpoint

Request Parameters

Parameters

Type

Category

Description

name

String

Required

The name of the repository that we want to create

description

String

Optional

The repository’s description

homepage

String

Optional

The repository’s information URL

private

Boolean

Optional

This is true if the repository is private

has_issues

Boolean

Optional

This is true if issues are enabled and false if issues are disabled

has_projects

Boolean

Optional

This is true if projects are enabled and false if projects are disabled

has_wiki

Boolean

Optional

This is true if wiki is enabled and false if wiki is disabled

is_template

Boolean

Optional

This is true if the repository can be made as a template repository and false if it can’t be

allow_squash_merge

Boolean

Optional

This is true if squash merge is enabled and false if squash merge is disabled

allow_merge_commit

Boolean

Optional

This is true if merge commit is enabled and false if merge commit is disabled

allow_rebase_merge

Boolean

Optional

This is true if rebase merge is enabled and false if rebase merge is disabled

allow_auto_merge

Boolean

Optional

This is true if auto merge is enabled and false if auto merge is disabled

delete_branch_on_merge

Boolean

Optional

This is true if the delete branch on merge is enabled and false if the delete branch on merge is disabled

Let’s try creating it by using the API in the code widget below. Please provide the repository name in the widget below against the REPOSITORY key. It'll be replaced in the code in line 9.

Press + to interact
Please provide values for the following:
ACCESS_TOKEN
Not Specified...
REPOSITORY
Not Specified...
const endpointUrl = 'https://api.github.com/user/repos';
const headers = {
Authorization: 'token {{ACCESS_TOKEN}}',
Accept: 'application/vnd.github.v3+json',
};
const body = JSON.stringify({
name: '{{REPOSITORY}}',
});
const options = {
method: 'POST',
headers,
body,
};
async function CreateRepo() {
try {
const response = await fetch(endpointUrl, options);
printResponse(response);
} catch (error) {
printErrors(error);
}
};
CreateRepo();

The HTTP response of the above code can be the following:

  • 201: The repository has been created.

  • 403: It is forbidden for the repository to be created.

  • ...