How Download Repository as ZIP file via API 2.0
Asked by Corey Malcom:

I have read through all the relevant questions but did not see an answer to this specific question:

I need to download the repository (as an archive file) via the API v2.0 via node app. There will not be any user context for the request so I need to authenticate the request. I have been using Basic auth (using an app password) for the other api requests I have been using.

This url was suggested in one posting and it works from Postman:

```
?https://bitbucket.org/<workspace>/<repoSlug>/get/main.zip
```

but I need to make the request via **axios**, and am having a hard time passing the proper authentication.

This is the current attempt that returns a 401:

```
axios.request({ 
  method: 'get', 
  url:`https://bitbucket.org/${workspace}/${repoSlug}/get/${branch}.zip`, 
  responseType: 'stream', 
  headers: { auth: { username: secret.username, password: secret.password }}
 });
```

I would rather make the request to the API, but have not found this functionality in the version 2 API.


Answers:
author: Corey Malcom
body: I was finally able to get this to work in a Node.js app passing Basic auth parameters. I thought i would post this for people with the same question.

See code below:

```
 // This will download the repository as zip file returning data as stream.

 async _download (url, username, password) {
  // where 'url' is something like:
  //  /${workspace}/${repoSlug}/get/${whateverNameYouWant}.zip

  const auth = new Buffer.from(`${username}:${password}`).toString('base64'); 
  const fileResponse = await axios.request(
  { 
    method: 'get', 
    baseURL: 'https://bitbucket.org', 
    url: url, 
    responseType: 'stream', 
    headers: { 
      Authorization: `Basic ${auth}` 
    } 
  });  
  return fileResponse.data; 
}
```


author: Theodora Boudale
body: Hi [@Corey Malcom](/t5/user/viewprofilepage/user-id/4297611) and welcome to the community.

I'm afraid that we don't have any API endpoint that downloads a zip file of the repository.

We have a feature request about this in our issue tracker, that you can vote for and add yourself as a watcher:

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

The workaround is indeed to use the following:

```
curl -u username:app-password https://bitbucket.org/<workspace-id>/<repo-slug>/get/master.zip --output master.zip
```

I am not familiar with axios, so I'm not sure how to troubleshoot the error you are getting. A 401 response is usually associated with invalid authentication credentials, so I would suggest double checking if you're using an app-password that has appropriate permissions.

If you are able to successfully retrieve the zip via a curl command or Postman using the same app-password as in axios, I would recommend reaching out to the support team of axios. I believe they're better equipped to troubleshoot why the request is failing, when it succeeds with other tools using the same url and credentials.

Kind regards,  
Theodora

comments: author: Corey Malcom
body: [@Theodora Boudale](/t5/user/viewprofilepage/user-id/818086) Thank you for the response, but I had already found that curl command, and was looking for an API endpoint for this. I did however finally get this to work via a node app. I am going to post the answer below.


author: Theodora Boudale
body: Good to hear that you managed to get this to work, and thank you for posting the solution!


Tags: bitbucket-cloud

cloud

download