Programmatically create Pull Request on BitBucket repo using REST API?
Asked by Al Pizano:

**Update**: Well, I was running this in a Bash script, but I wanted to see what error code I was getting, and now I can see I am getting a "401 Unauthorized". I am using my username and I created Personal Access Token with \`admin\` access bitbucket, so I should be able to create a PR right? I can do this through the web UI on the same repo?

I am running a bash script to create a pull request on Bitbucket. I already am programmatically cloning the repo, editing a file, doing a git add/commit, now I just need to use CURL to make the PR. It seems the bitbucket API exposes a endpoint to do this using a POST request:  
![Capture2.PNG](https://community.atlassian.com/t5/image/serverpage/image-id/111248i4DF90D460DEE3121/image-size/large?v=v2&px=999 "Capture2.PNG")![Capture2.PNG](https://community.atlassian.com/t5/image/serverpage/image-id/111249iE5C9FE70CDD262D1/image-size/large?v=v2&px=999 "Capture2.PNG")  

<https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Bworkspace%7D/%7Brepo_slug%7D/pullrequests#post>

<br />

Here's how my repo looks on Bitbucket, I blocked out the real names, but the left format the same (the first name is the Project name, "REACTOR2.0", while I believe the second name is the repository name, "dat-repo"):

![Capture1.PNG](https://community.atlassian.com/t5/image/serverpage/image-id/111246i8F64D31F4C5A1E04/image-size/large?v=v2&px=999 "Capture1.PNG")

I am trying many different variations, and am checking the remote bitbucket server for a new pull request, but I see _nothing_.

I'm certain my "title" and "branch" are correct. My only question is about the URL; I am inputting my username from bitbucket, if you go to "Manage Account" then "name", that is the field I'm using for the "my-username" part of the URL, and I'm adding the repository name for the "my-repository" part. However, I need to note that this is a repository on bitbucket nested inside a Project called "REACTOR2.0", so I wasn't sure if the project name needed to be specified in the URL somewhere.

Has anyone been successful with this API? I looked on google, but many questions were using the old 1.0 API and don't apply or people were doing GET request do simply get a list of pull requests....

<br />

\[1\]: <https://i.stack.imgur.com/VQnSd.png>


Answers:
author: Theodora Boudale
body: Hi Al,

I checked in our system the Bitbucket Cloud account linked with the email you use in this Community account, and I see that it doesn't own any repositories or have access to other repositories.

Do you perhaps have another Bitbucket Cloud account with a different email?

To answer your question: the "my-username" field in the URL should be the workspace ID of the workspace that owns the repo. While you are logged in <https://bitbucket.org/> to the account that has access to this repo, select your avatar (bottom left corner) \> **All workspaces** . Then select the workspace that owns this repo. After you do, you will find the workspace ID in the URL, which will have the format <https://bitbucket.org/workspace-id/>. This workspace-id should be in the place of "my-username" in the URL.

You don't need to specify the project for this API call.

I would recommend:  
1. Checking that you have generated an app password for the Bitbucket Cloud account that has access to this repo  
2. Make sure that the "my-username" field has the value of the workspace ID, as per my instructions above

An example call that works is the following:

```
curl -L -X POST --user username:password --header 'Content-Type: application/json' https://api.bitbucket.org/2.0/repositories/workspace-id/repo-slug/pullrequests/ --data '{"title": "My Title","source": {"branch": {"name": "feature-branch"}}, "destination": {"branch": {"name": "master"}}}'
```

where  
**username** is the username of your Bitbucket account  
**password** is the password of your Bitbucket account or an app-password  
**workspace-id** is as I explained above  
**repo-slug** is the repo-slug for the repo  
in the **data**, replace the values for title, source and destination branch

Please feel free to let me know how it goes.

Kind regards,  
Theodora

comments: author: Al Pizano
body: Thank you, yes it was a bitbucket Server account and I was able to figure out how to create the automated PR by another atlassian representative.

Thank you!


Tags: bitbucket-cloud

cloud

git

pull-request