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

Is it possible to use an API to access the contents of a private repo?

Deleted user September 10, 2020

I am working on a VUE project where I'd like to be able to create a list of files which are in my private repo and then use that list to copy the matching files from my private repo into a zip folder for the user to download.

I have the create list and download zip folder functionality figured out so far.

However I am totally new to using APIs so I'm looking for advice on whether this is possible or not and what the best way to go about it would be.

1 answer

1 accepted

0 votes
Answer accepted
Theodora Boudale
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
September 11, 2020

Hi Christopher,

Do you need help with getting a list of files via API or the contents of these files? Or perhaps both?

Could you also please let us know whether this concerns repositories hosted in Bitbucket Cloud or Bitbucket Server?

Kind regards,
Theodora

Deleted user September 11, 2020

Both for different purposes.

I'm not sure what the difference between cloud and server is. It's a repository on bitbucket.org.

Theodora Boudale
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
September 14, 2020

Hi Christopher,

Thank you for your reply. If the repository is hosted on bitbucket.org, then this is Bitbucket Cloud (Bitbucket Server is a product you install on your own servers, so this is self-hosted).

For Bitbucket Cloud, you can use this endpoint to get a list of the root directory on the main branch of the repo (at the latest commit):

An example call could be the following:

curl -sL -X GET --user username:password http://api.bitbucket.org/2.0/repositories/repo-owner/repo-slug/src

where
username is the username of a Bitbucket account with access to the repo
password is this account's password or an app-password with appropriate permissions
repo-owner is the workspace ID of the workspace that owns the repo
repo-slug is the slug for this repository

If you want to get a list of the files/directories at a specific commit, you can specify the commit as follows:

curl -sL -X GET --user username:password http://api.bitbucket.org/2.0/repositories/repo-owner/repo-slug/src/commit-hash/

In order to get the contents of a specific file or a directory, you can use the following endpoint:

An example call would be the following:

curl -sL -u username:password -H "Content-Type: application/x-www-form-urlencoded" https://api.bitbucket.org/2.0/repositories/repo-owner/repo-slug/src/commit-hash/file_path

where
commit-hash replace with the hash of the specific commit you are interested in
file_path is either the path+name of a specific file, OR the path of a directory in the repo

In the call above, if file_path is the path of a directory in the repo, then the call will return the contents of this directory.

If file_path is the path+name for a specific file, you will get the contents of this file. You can then redirect the output to a file named e.g. file1 on your machine as follows:

curl -sL -u username:password -H "Content-Type: application/x-www-form-urlencoded" https://api.bitbucket.org/2.0/repositories/repo-owner/repo-slug/src/commit-hash/file_path > file1

I hope this helps, please feel free to let me know if you have any questions.

Kind regards,
Theodora

Like Deleted user likes this
Deleted user September 15, 2020

Thank you for your help.

I am looking forward to testing this myself.

Theodora Boudale
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
September 18, 2020

You are very welcome, please feel free to reach out if you have any questions!

Jana Kaszas
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!
June 15, 2023

Hi together,

it's possible to use the latest commit instead of the commit-hash ?

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events