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,365,853
Community Members
 
Community Events
168
Community Groups

Cannot create branch via bitbucket API 2.0

Edited

I have looked through the documentations and Questions/Discussions on this forum, and yet wasn't able to create a bitbucket branch using the API

I've tried exactly what the documentation suggests:

curl -X POST -s \
--url "https://api.bitbucket.org/2.0/repositories/$WORKSPACE/$REPO_NAME/refs/branches" \
--header 'Accept: application/json' \
--header "Authorization: Bearer $ACCESS_TOKEN" \
-d '{ "name" : "test", "target" : { "hash" : "develop" } }'

and it returns me the error:

{"type": "error", "error": {"message": "Bad request", "fields": {"name": "required key not provided", "target": "required key not provided"}}}


and I've also tried:

curl -X POST -s \
--url "https://api.bitbucket.org/2.0/repositories/$WORKSPACE/$REPO_NAME/src/" \
-F "branch=test" \
--header 'Accept: application/json' \
--header "Authorization: Bearer $ACCESS_TOKEN"
as mentioned in this discussion

and got the errors:

{"type":"error","error":{"message":"Something went wrong","id":"db9155a6a32946e99dbc2d9201a9dd57"}}
{"type":"error","error":{"message":"Something went wrong","id":"153531f483bb4baf90ad8968311670da"}}
{"type":"error","error":{"message":"Something went wrong","id":"aa3bd7c29ad44b559a4c450e2e2db7f6"}}

Can anyone help me with this?
Confirmation that your end is working is also appreciated.

edit: Spelling

1 answer

1 accepted

1 vote
Answer accepted
Caroline R Atlassian Team Sep 07, 2022

Hi, @Arthur Gramiscelli Branco! Welcome to the community! 

You can run the following request to create a branch using API:

curl -X POST -s \
--url "https://api.bitbucket.org/2.0/repositories/{workspace}/{repository_name}/refs/branches" \
-H "Content-Type: application/json" \
-u username:AppPassword \
-d '{ "name" : "test", "target" : { "hash" : "develop" } }'

I tested this on my side and it's working fine. Could you please run this and confirm if it works for you? 

Kind regards,
Caroline 

Hi @Caroline R, Thank you for your answer!
Our client wants us to use the Oauth consumer. Can you try again passing a consumer token please?
ps: The consumer I'm using for testing has all available permissions checked.

To fetch my token I'm using:

ACCESS_TOKEN=$(curl -X POST -s https://bitbucket.org/site/oauth2/access_token -d grant_type=client_credentials -u "$consumer_key:$consumer_secret" | jq -r '.access_token')


And I've checked if the token was working using this example (it is):

curl -X GET -s \
--url "https://api.bitbucket.org/2.0/repositories/$WORKSPACE/$REPO_NAME/refs/branches/develop" \
--header 'Accept: application/json' \
--header "Authorization: Bearer $ACCESS_TOKEN"

@Caroline R I manually tried again using your example and it worked even with the consumer token, looks like I failed to correctly pass some of my variables in our script, thank you for the help.

What I was doing:

curl -X POST -s \
--url "https://api.bitbucket.org/2.0/repositories/$WORKSPACE/$REPO_NAME/refs/branches" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $ACCESS_TOKEN" \
-d "{ "name" : "$NEW_RELEASE", "target" : { "hash" : "develop" } }"

What worked:

curl -X POST -s \
--url "https://api.bitbucket.org/2.0/repositories/$WORKSPACE/$REPO_NAME/refs/branches" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $ACCESS_TOKEN" \
-d "{ \"name\" : \"$NEW_RELEASE\", \"target\" : { \"hash\" : \"develop\" } }"

I forgot to backslash my quotes  =X hahaha my bad, thank you again

Caroline R Atlassian Team Sep 09, 2022

Awesome, @Arthur Gramiscelli Branco! I'm glad to know it worked! 

Please feel free to reach out if you ever need anything else! 

Kind regards,
Caroline 

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events