Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,293,780
Community Members
 
Community Events
165
Community Groups

How do I create a new repo via 2.0 API and powershell?

I've read the documentation, and I've formatted my request body just like in the example with 1 addition: 

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add('Authorization' , "$apiKey")
$headers.Add('accept', 'application/json')
$body=@"
{
"scm": "git",
"project": {
"key": "$MYKEY",
},
"is_private":$true
}
"@

try {
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Invoke-RestMethod -Method PUT -Headers $headers -Body $body -Uri https://api.bitbucket.org/2.0/repositories/$workspace/$repoName -Verbose -UseBasicParsing
} catch {
echo "Something occured"
echo $_.Exception
echo $_
}

No matter what I put in the body, regardless of whether or not it's valid JSON, I always get a 200 response, the project KEY is wrong (defaults to oldest project in account), and it's not marked as a private repo. 

How am I supposed to format this request so that it works in powershell? 

1 answer

0 votes
Syahrul Atlassian Team Jan 12, 2022

Hey @ithryeandeus 

G'Day!

Have you tried adding the key manually for example as below:

{
    "scm": "git",
    "project": {
        "key": "TST"
    } 

Let me know if the scripts works and if it's still returning an error please share with us the full error. 

Additionally, if you wish to make your repositories private when created add the following additional parameter which should make it private:

{
    "scm": "git",
    "is_private": true,
    "project": {
        "key": "MARS"
    } 

Also, do check our newly update API page for more information on recent changes at:

I would also want to share the full curl commands so you have an idea of how the JSON data works in Bitbucket API:

curl -X POST -u "$BITBUCKET_USERNAME:$BITBUCKET_APPPASSWORD" "https://api.bitbucket.org/2.0/repositories/<Workspacename>/<Repositoriesname>" -H "Content-Type: application/json" -d '{"scm": "git", "is_private": true, "project": {"key": "TST"}}'

I hope this helps.

Cheers,
Syahrul

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Bitbucket

Git push size limits are coming to Bitbucket Cloud starting April 4th, 2022

Beginning on April 4th, we will be implementing push limits. This means that your push cannot be completed if it is over 3.5 GB. If you do attempt to complete a push that is over 3.5 GB, it will fail...

2,112 views 2 9
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you