REST API 2.0 to merge a Pull Request is throwing write access error
Asked by Dhiraj Sittu:

Hi All,

I'm trying to hit the following curl:

curl --location --request POST '<https://api.bitbucket.org/2.0/repositories/>\<workspace\>/\<repo\>/pullrequests/1493/merge' \\  
--header 'Authorization: Bearer \<access_token\>' \\  
--header 'Accept: application/json' \\  
--header 'Content-Type: application/json' \\  
--data-raw '{  

}'

I have created the access_token from **Repository settings \> Access tokens** for this repo, on Bitbucket Cloud website. All the permissions are checked except **Delete repo**.

I'm getting the following error response:  
{  
"type": "error",  
"error": {  
"message": "newstatus: Merging a pull request requires write access to the destination branch.",  
"fields": {  
"newstatus": \[  
"Merging a pull request requires write access to the destination branch."  
\]  
}  
}  
}  
I'm getting the option to Merge the PR from the UI but API is failing.  
I've attached the permissions screenshot.  
![Screenshot 2023-03-01 at 7.52.02 PM.png](https://community.atlassian.com/t5/image/serverpage/image-id/243310i5212ED021EF10114/image-size/large?v=v2&px=999 "Screenshot 2023-03-01 at 7.52.02 PM.png")


Answers:
author: Theodora Boudale
body: Hi [@Dhiraj Sittu](/t5/user/viewprofilepage/user-id/5159577) and welcome to the community!

If the screenshot you shared is from the PR's destination branch, then the reason you are getting this error message is that merge of PRs is restricted only to certain users.

Repository access tokens are not associated with the user that generated them or with any other user. They also cannot be added to a branch restriction, so you won't be able to merge this PR with a Repository access token (RAT) as long as this branch restriction exists.

Merging a PR via API with a RAT is possible as long as PR merging is not restricted to certain users and everyone is allowed to merge.

If you would like to keep the branch restriction for PR merging, then the only way to merge the PR with an API call is by using the authentication details of a user who has permission to merge the PR:

```
curl -u BB_Username:App_Password -X POST 'https://api.bitbucket.org/2.0/repositories/<workspace-id>/<repo>/pullrequests/<pr_id>/merge'
```

where

**BB_Username** is the username of a Bitbucket Cloud user who has permission to merge PRs to the destination branch  
**App_Password** is an app password for that user's account, that has Pull requests: Write permissions

We have a feature request for allowing admins to add a Repository access token to branch restrictions:

* <https://jira.atlassian.com/browse/BCLOUD-22400>

I would suggest adding your vote (by selecting the **Vote for this issue** link) and commenting on the feature request to give it more weight and further express your interest. You can also add yourself as a watcher (by selecting the **Start watching this issue** link) if you'd like to get notified via email on updates. Implementation of new features is done as per our policy [here](https://confluence.atlassian.com/support/implementation-of-new-features-policy-201294576.html) and any updates will be posted in the feature request.

Please feel free to let me know if you have any questions.

Kind regards,  
Theodora


author: Ulrich Kuhnhardt _IzymesCo_
body: Hello Dhiraj, Welcome to the community!

I hope I can help: The request authorization header can be

1. "Bearer: \<bearer_token\>" or
2. "Basic: \<base64(\<username\>:\<access_token\>)\>"

If you try to send the same curl request with \`curl -u "\<username:accessToken\>" .....\` you should get the Auth header for free.

Good luck and let me know how you go!

Best, Ulrich

// [Izymes](https://marketplace.atlassian.com/vendors/1210584/izymes-pty-ltd)


Tags: bitbucket-cloud

cloud