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

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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

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

Both for different purposes.

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

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 christopherduffy86 likes this

Thank you for your help.

I am looking forward to testing this myself.

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

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Bitbucket

Powering DevOps with Bitbucket Server & Data Center

Hi everyone, The Cloud team recently announced 12 new DevOps features that help developers ship better code, faster   ! While we’re all excited about the new improvements to Bitbucket ...

1,826 views 0 7
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you