Trying to create Pull Request on BitBucket through Rest API - "401 Unauthorized"
Asked by Al Pizano:

I'm using this API documentation to create a Pull request on bitbucket but its not working, I keep getting a **401 - Unauthorized** , even though I created a personal access token with **Admin** access and I "believe" I am using the correct username and access token (as my password) for the curl POST request:  
<https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Bworkspace%7D/%7Brepo_slug%7D/pullrequests#post>  

This is sample JSON:

    curl https://api.bitbucket.org/2.0/repositories/my-username/my-repository/pullrequests \    -u my-username:my-password \    --request POST \    --header 'Content-Type: application/json' \    --data '{        "title": "My Title",        "source": {            "branch": {                "name": "staging"            }        }    }'

What is my repository is listed under a "project name"? where the heck does the Project name go in the URL? and the "my-username" is just under the Bitbucket website "Manage accounts" -\> "Name" , correct? If so, then my JSON should be correct.

Can anyone help me? If I go to the repository on the Bitbucket GUI in web browser, I can definitely log in and create Pull Request manually - why cannot I do this through REST API? What is the most common cause for 401 - Unauthorized? Do I need to request special access from the repository owners, even though I have my Admin access token, and I can view \& make Pull requests on Bitbucket to this repo through web browser???


Answers:
author: Joao Sgreccia
body: Hi [@Al Pizano](/t5/user/viewprofilepage/user-id/4093774),  

Sorry for the late response.  

    curl https://api.bitbucket.org/2.0/repositories/my-username/my-repository/pullrequests \    -u my-username:my-password \    --request POST \    --header 'Content-Type: application/json' \    --data '{        "title": "My Title",        "source": {            "branch": {                "name": "staging"            }        }    }'

where  

my-username -\> <https://bitbucket.org/account/settings/> , get your **username** *, below bitbucket profile settings* my-password -\> an app password with *pullrequest -\> write* permission  
<https://support.atlassian.com/bitbucket-cloud/docs/app-passwords/>  

<br />

Dont hesitate to get in touch if you have any further questions.  
Cheers

comments: author: Al Pizano
body: [@Joao Sgreccia](/t5/user/viewprofilepage/user-id/3362925) We don't use the **<https://bitbucket.org>** website, which lets you create an app-password. We seem to use the **<https://bitbucket.company-name.com>** format, and I don't see that I can create an app-password there.

I can however create **Personal Access Tokens** or add **SSH keys** from my local git. I saw that the bitbucket 1.0 API seemed to allow Authentication using the Personal access token (Bearer), but I tried and still got a **"401 - Unauthorized"** :  

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

Also, what about the "Project" name , that comes before the repository name? Doesn't that need to be added to the URL, or branch name? i.e. "projectName/RepoName/BranchName" (below my project name is "Radio2.0-INFRA" with repository name "deployable-radio"?

Also, as you can see, my repository "" is actually in a workspace called **""**. How can I be able to create a Pull Request programmatically and bypass these 401 - Unauthorized errors???


author: Al Pizano
body: \[redacted\]


author: Al Pizano
body: sorry, duplicated post entries.


author: Joao Sgreccia
body: Hi [@Al Pizano](/t5/user/viewprofilepage/user-id/4093774) ,   

Sorry, for this, I thought you were talking about bitbucket cloud.

Please take a look at [bitbucket server docs](https://developer.atlassian.com/server/bitbucket/), and let me know if you have any questions.

Authentication:   
<https://developer.atlassian.com/server/bitbucket/how-tos/example-basic-authentication/>  

Rest API:   
<https://developer.atlassian.com/server/bitbucket/how-tos/command-line-rest/>  

PullRequest:  
<https://docs.atlassian.com/bitbucket-server/rest/7.6.0/bitbucket-rest.html#idp291>  

Cheers


author: Al Pizano
body: [@Joao Sgreccia](/t5/user/viewprofilepage/user-id/3362925) Thank you very much. I was FINALLY able to get it to work with your latest reply with information about the SERVER API and authentication.

Thank you!!!


Tags: api

bitbucket-cloud

cloud

git

pull-request

rest-api