Cannot create branch via bitbucket API 2.0
Asked by Arthur Gramiscelli Branco:

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](https://developer.atlassian.com/cloud/bitbucket/rest/api-group-refs/#api-repositories-workspace-repo-slug-refs-branches-post) 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"}}}
```

<br />

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](https://community.atlassian.com/t5/Bitbucket-discussions/Create-branch-BitBucket-REST-API-Support/td-p/610927):   

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


Answers:
author: Caroline R
body: Hi, [@Arthur Gramiscelli Branco](/t5/user/viewprofilepage/user-id/5023713)! 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

comments: author: Arthur Gramiscelli Branco
body: Hi [@Caroline R](/t5/user/viewprofilepage/user-id/3934006), 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')
```

<br />

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"
```


author: Arthur Gramiscelli Branco
body: [@Caroline R](/t5/user/viewprofilepage/user-id/3934006) 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


author: Caroline R
body: Awesome, [@Arthur Gramiscelli Branco](/t5/user/viewprofilepage/user-id/5023713)! I'm glad to know it worked!

Please feel free to reach out if you ever need anything else!  
Kind regards,  
Caroline


Tags: bitbucket-cloud

cloud