How to create repository in github through github API?

Go to Settings -> Developer Settings -> Personal Access Token Under OAuth Apps. Now, Generate a New Access token with Required privileges enabled. Ignore This if you already have one.

User Account:

Replace ACCESS_TOKEN with Token and NEW_REPO_NAME with your New Repository Name in the command below:

curl -H "Authorization: token ACCESS_TOKEN" --data '{"name":"NEW_REPO_NAME"}' https://api.github.com/user/repos

Organization:

curl -H "Authorization: token ACCESS_TOKEN" --data '{"name":"NEW_REPO_NAME"}' https://api.github.com/orgs/ORGANIZATION_NAME/repos

Step 1: Create a personal access token and use it in place of password

Step 2: On command line use the given API as a POST request

https://api.github.com/orgs/<organisation_name>/repos?access_token=<generated token>

or

https://api.github.com/user/<username>/repos?access_token=<generated token>

In body, pass this as a payload:

{
  "name": "<Repo Name>",
  "description": "<Some message>",
  "homepage": "https://github.com",
  "private": false,
}

You can pass other details.
For more details: click here


There is a new solution official from cli.github.com. The other answers are deprecated.

First Install GitHub CLI. Run:

brew install gh

Then to create a repository with a specific name. Enter the following command:

gh repo create --private my-project

Here is the official documentation link to create a repo.


Based on the two above answers, here's a bash >=4.2 script to create a clone a repo.

#!/usr/bin/env bash
TOKEN=0000000000000000000000000000000000000000

if [[ $# < 3 ]]; then
    echo "arguments: $0 <RepoName> <RepoDescription>"
    exit 1
fi

REPO_NAME=$1; shift
REPO_DESCRIPTION="$@"

echo "Name: $REPO_NAME"
echo "Description: $REPO_DESCRIPTION"
echo "Calling GitHub to create repo"

read -r -d '' PAYLOAD <<EOP
{
  "name": "$REPO_NAME",
  "description": "$REPO_DESCRIPTION",
  "homepage": "https://github.com/$REPO_NAME",
  "private": false
}
EOP

shopt -s lastpipe
curl -H "Authorization: token $TOKEN" --data "$PAYLOAD" \
    https://api.github.com/user/repos | readarray output

url=''
for line in "${output[@]}"; do
    echo -n "$line"
    #   "html_url": "https://github.com/sfinktah/vim-hex-sum",
    if [[ $line == *html_url* ]]; then
        l=${line#*\"}; l=${l#*\"}; l=${l#*\"}; url=${l%\"*}
    fi
done

count=0
[[ $url == http*://*github.com/* ]] &&
    until git clone $url; do 
        sleep 10; (( count++ > 5 )) && break; echo Waiting...
    done