19

I am trying to use github api to create repositories under a particular organization. I was looking at this site which talks about how to create repositories under a particular organization.

Create a new repository in this organization. The authenticated user must be a member of the specified organization.
POST /orgs/:org/repos

Now I am not able to understand what will be my full URL that I need to use to create the repository under a particular organization? I have my username and password which I can use to create the repository under an organization through https url.

My github instance url is like this - https://github.host.com

And I want my repository to be like this after getting created -

https://github.host.com/Database/ClientService

What will be my curl command look like to create the repository under an organization?

3
  • 1
    All the information you need is here, describes in the gihub help pages developer.github.com/v3/guides/getting-started/… Commented Feb 7, 2015 at 18:42
  • Thanks for your suggestion. What is token here? And how I will get the token? Sorry for asking stupid question, working for the first time so not sure. Commented Feb 7, 2015 at 18:44
  • 1
    Read here how to do it: help.github.com/articles/… Commented Feb 8, 2015 at 0:27

7 Answers 7

26

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
Sign up to request clarification or add additional context in comments.

1 Comment

What is the priviledge to create a repo?
15

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

2 Comments

this method is no longer supported - cannot add access tokens in the url. you will have to use Authorization headers
If using Insomnia, one can still use this kind of url and JSON payload - just put the token inside as Bearer type auth.
3

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.

Comments

2

To create a new repo in github for Enterprise this worked for me:

Please change the value in angular brackets before executing. Put username:token after -u

curl -u https://github.com/api/v3/orgs/<org-name>/repos -d '{"name":"<reponame>"}

1 Comment

In my case, our company has a custom github domain; for sake of argument pretend our github domain is "stuff.tinycorp.localdomain". I had to replace "github.com" above with "stuff.tinycorp.localdomain". Otherwise, quite a helpful answer.
1

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

Comments

0

Checkout this answer this is an old method, without installing github cli

create repo in terminal directly with api

replace userName and repoName

curl -u "userNameHere" https://api.github.com/user/repos -d > '{"name":"repoNameHere","private":true}'

enter password and done

for more info refer new official doc here

Comments

0

Recommend Github SDK Octokit. To create a repository:

const octokit = new Octokit({ auth: `personal-access-token123` });

// Create a repo for user.
octokit.rest.repos.createForAuthenticatedUser({
  name,
});

// Create a repo in an organization.
octokit.rest.repos.createInOrg({
  org,
  name,
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.