Download Zip Archives with the Box API

Alex Novotny
Box Developer Blog
Published in
4 min readFeb 28, 2023

--

A common question we get at Box Platform is how to download large swaths of content that exist on Box. It could be to archive the data on a separate server, or directly integrate content with on premise application system. While the Box API does have the capability to directly download a single file, in order to download more than that, you will have to create and download a zip archive.

What are the options for a developer to do this? Let’s take a look!

Prerequisites

In order to use the Box API, you have to create a custom application in the developer console. For the authentication method, you can select any of the options — JWT, OAuth 2.0, or CCG.

Box Custom Application Creation

If you select JWT or CCG, you will have to have an administrator approve the application before you can actually use it to make API calls. The administrator will also need to re-authorize the application if you make any changes to the application scopes.

Depending on the authentication method chosen, you will want to confirm the service account (or yourself in the case of OAuth 2.0) has access to the content you are trying to download. The zip download process will not download content if you or the service account has not been collaborated into the content. Find out more about service accounts here.

cURL

You can directly call the Box API without doing any other install requirements via cURL. If you do this, you will want to grab a developer token from the application you created. You’ll place that token in the place labeled access token below.

Then, in a terminal or console you can run the below command. The download file name can be anything you choose. Also, you can list as many file and folder ids as you wish up to 10,000 files or the account’s upload limit.

curl -i -X POST "https://api.box.com/2.0/zip_downloads" \
-H "Authorization: Bearer <ACCESS_TOKEN>" \
-d '{
"download_file_name": "<insert_download_name",
"items": [
{
"type": "file",
"id": "<file_id>"
},
{
"type": "folder",
"id": "<folder_id>"
}
]
}'

If successful, you’ll get a response with something like the below data.

{
"download_url":"https://dl.boxcloud.com/2.0/zip_downloads/25gvaXcIE4QJlinNiw2oHAQ==ZFs3Q2Xpd7pKBz7OyzXNrUaoW3aJxQRN5znAvyM-KpdEEPdWcQDKU-Dl85Ew/content",
"status_url":"https://api.box.com/2.0/zip_downloads/25gvaXcIE4QJlinNiw2oHAQ==ZFs3Q2Xpd7pKBz7OyzXNrUaoW3aJxQRN5znAvyM-KpdEEPdWcQDKU-Dl85Ew/status",
"expires_at":"2023-02-28T10:23:54Z",
"name_conflicts":[]
}

In the download url field, grab the download id that appears between the slashes. In the above example, it would be: 25gvaXcIE4QJlinNiw2oHAQ==ZFs3Q2Xpd7pKBz7OyzXNrUaoW3aJxQRN5znAvyM-KpdEEPdWcQDKU-Dl85Ew .

Place that id where it says zip_download_id below, as well as place the same developer token you used in the first call in the place where it says access token. The -o stands for output, and you can name this anything you wish, just make sure it has the .zip at the end. After hitting enter, a popup will appear asking you where you want to save the download.

This step will need to be done quickly. As you can see, the link will expire at the time shown in the response.

curl -L "https://dl.boxcloud.com/2.0/zip_downloads/<zip_download_id>/content" \
-H "Authorization: Bearer <ACCESS_TOKEN>" \
-o sample_curl.zip

And that’s it! Please note, that if you are downloading large amounts of content, the process will not complete instantly. You will be limited to the speed of your internet. If you wish, you can check the status of a long downloading zip via the check status endpoint- including any items that may have been skipped.

Postman

You can also do the above using our awesome Postman Collection! If you haven’t set this up before, checkout the YouTube video and blog for more information!

The below screenshots show using the create a zip download and download zip archive endpoints we used with the cURL commands above.

Postman Box Create Zip Download

With Postman, you’ll want to click the down arrow next to send in order to actually download the zip file. Otherwise, the program will just show a 200 and not actually download anything.

Postman Box Download Zip Archive

Box CLI

But wait!!!! Like Postman, you can also use the Box CLI to further automate this process! If you haven’t set up it up before, you can find more information on that process in this YouTube video and blog.

Using the CLI you can just make one call vs two! Pretty awesome, huh?

box files:zip sample.zip --item=file:1090181431874 --item=folder:186279475438 --destination=/Users/anovotny/Desktop

You can find this example in the docs section of the CLI GitHub repository.

Box CLI Zip Request Call

Box SDKs

In addition to cURL, Postman, and the Box CLI, all of the SDKs we offer have sample code for downloading zip archives in their respective docs section. Find the links here: Node, Python, Java, .NET

We hope you enjoyed this blog. Feel free to reach out to us on the developer forum for support, or via Box Pulse to make suggestions on how to improve Box Zip Downloads.

--

--

Alex Novotny
Box Developer Blog

I’m a Box Developer Advocate, helping others learn how to maximize their investment through Box Platform.