What is the api to be used to get list of all the files in a folder in a repository from bitbucket
Asked by malathi.pulakam:

I need list of all the files in a folder of a repository . what is the api to be used and what are all required to call that api properly (like to authenticate myself )


Answers:
author: Theodora Boudale
body: Hi [@malathi.pulakam](/t5/user/viewprofilepage/user-id/5157008) and welcome to the community!

You can use the following endpoint to find files in a folder, at a certain branch or commit of the repository:

* <https://developer.atlassian.com/cloud/bitbucket/rest/api-group-source/#api-repositories-workspace-repo-slug-src-commit-path-get>

As Aron mentioned, in Git, the files of a repository can be different for different branches and commits. It is therefore necessary to provide in the API call the commit hash or branch name that you want to retrieve the files of.

An example call is the following:

```
curl -u BB_Username:App_password -X GET -H "Content-Type: application/json" https://api.bitbucket.org/2.0/repositories/{workspace-id}/{repo-slug}/src/{branch_or_commit-hash}/{path}
```

where

**BB_Username** is the username of a Bitbucket account with access to the repo  
**App_password** is an [app password](https://support.atlassian.com/bitbucket-cloud/docs/app-passwords/) for that account, with at least Repositories: Read permissions  
**{workspace-id}** replace with the id of your workspace  
**{repo-slug}** replace with the slug of your repo  
**{branch_or_commit-hash}** replace with a commit hash or a branch name  
**{path}** replace with the path of the folder in the repo that you want to see the files of

<br />

If you want to get fewer fields in the results, you can use the **fields** parameter:

```
curl -u BB_Username:App_password -X GET -H "Content-Type: application/json" https://api.bitbucket.org/2.0/repositories/{workspace-id}/{repo-slug}/src/{branch_or_commit-hash}/{path}/?fields=values.path,values.type
```

This will show only the path for each result, and whether it's a file or another directory.

<br />

With regards to authentication, in these examples, I used basic authentication which is tied to a certain user. You could also use Access tokens instead that are not tied to a certain user. You can find more details about access tokens here:

* <https://support.atlassian.com/bitbucket-cloud/docs/access-tokens/>

If you have any questions, please feel free to reach out!

Kind regards,  
Theodora

comments: author: Fabio Fioramonti
body: Hi [@Theodora Boudale](/t5/user/viewprofilepage/user-id/818086) ,

there is a way to obtain the file SHA when listing content of a directory?

Something similar to what you might get with the command:  

git ls-tree main  

<br />

Thank you

Fabio


author: Theodora Boudale
body: Hi Fabio,

I'm afraid that the file SHA is not returned by our API.

We have a feature request about this in our issue tracker:

* <https://jira.atlassian.com/browse/BCLOUD-20424>

The request was closed due to inactivity, but you can provide your feedback there, as our product managers continue to monitor even closed requests.

Kind regards,  
Theodora


author: Aron Gombas _Midori_
body: I don't think there is such an API.

The folder/file hierarchy depends on a "snapshot of the repository", basically on a state identified by a hash (commit ID). Which means your client code should clone the repository using a given hash, then work with the cloned local repo with local filesystem operations I think.


Tags: bitbucket-cloud

cloud