Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Using python API to get branch_restrictions / pullrequests

Or Shalom
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
July 7, 2022


I am trying to use the Python API to automate some processes in the bitbucket cloud.

I am establishing a connection via: = Cloud(username=self.username, password=self.password, token=self.token)


I am able to perform the following:

1.Get all the workspaces.

2. Get all the projects inside a workspace.

3. Get all the repos inside a project.

4. Get all the branches inside a repo.

5. Create new branches (based on that hash of the mainbranch read from the repo using get_data("mainbranch")["name"]


What I couldn't figure out yet:

1. When trying to get a list of branch_restrictions for a repo (by I get an empty string {}

2. When trying to get a list of branch_restrictions using repo.branch_restrictions.each() I get error 403 (I am admin for the workspace, project and repo).

3. When trying to get a list of pullrequests for a repo ( I also get a blank string {}.


Can you share an example on how to get branch_restrictions / pullrequests for a given repo?


Thank you!


1 answer

1 accepted

2 votes
Answer accepted
Patrik S
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
July 12, 2022

Hello @Or Shalom ,

Welcome to Atlassian Community!

I'm assuming you are talking about the following python API wrapper : 

I went ahead and tried to build a script on my side using this wrapper, and was able to get it working by following below steps : 

  1. Make sure you have the latest version of the python wrapper installed :
    pip install atlassian-python-api
  2. Use the below code as an example: 
    from textwrap import indent
    from atlassian.bitbucket import Cloud

    cloud = Cloud(url="", username="bitbucket_username", password="app_password")

    w = cloud.workspaces.get("workspace_name")

    for p in w.projects.each():

        p = w.projects.get(p.key)

        print("Project key " + p.key)

        for r in p.repositories.each():

            print("   Repository name " +

            for pr in r.pullrequests.each():

                print("   PR ID: " + str(

                print("      Title: " + str(pr.title))

                print("      Source branch: " + str(pr.source_branch))

                print("      Destination branch: " + str(pr.destination_branch))

            for br in r.branch_restrictions.each():

                print("   Branch restriction ID: " + str(

                print("      Kind: " + br.kind)

                print("      Pattern: " + br.pattern)

This example will print all the projects for a given workspace, the repositories within each project,  and all the pull requests and branch permissions if any. For the script to run, you will have to provide the values for : 

  • username: the bitbucket username (not the e-mail address), which you can find in the personal settings of your account
  • password: an App Password ( not your account password) with required privileges. If you get 403 from the API call, I would suggest creating a new app password with full permissions checked and test how it goes. 
  • workspace_name: the name of your workspace.

You can use this example and adapt it to your use case. I would also recommend checking the atlassian-python-api documentation for more examples of usage.

Also, please note that this python wrapper is developed by a third party and is not officially supported by Atlassian, so in case you find any issue, we suggest reporting it directly in their git hub repository.

Alternatively, you also have the option to call the API endpoints manually from your python script, without using a wrapper. Please find below the Bitbucket Cloud API reference documentation : 

Hope that helps! Let me know in case you have any questions.

Thank you, @Or Shalom .

Kind regards,

Patrik S

Or Shalom
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
July 14, 2022

Thank you, it did work.

Suggest an answer

Log in or Sign up to answer
Site Admin
AUG Leaders

Atlassian Community Events