Streamline file collection with the file request API

It’s the first of the month and that means you’re about to be inundated with invoices from contractors. You could head it off with a bulk email, but then you’re wading through replies with attachments that could easily be lost in the shuffle of other activity. This is a perfect use case for Dropbox and our API to help automate tasks like file collection.

File requests help you structure these repeating duties. Whether it’s contractor invoices, student homework assignments, or new employee headshots, you can use file requests to collect and organize files from anyone, right in your Dropbox account.  

How file requests work

You can create file requests from the user interface or via the API. Before we show the automated method, it’s useful to see how it works manually. You can create a file request from the file requests page while logged into your Dropbox account. When creating a request, you can select the location in your Dropbox where the files should be saved as well as set collection deadlines. These requests can be sent to anyone, whether they have a Dropbox account or not, and are great for collecting large files.

To learn more about the process of manually creating Dropbox file requests, check out our help center article or business guide. If youre interested in the experience of the user you’re sending the request to, this help center article covers how to upload a file once you’ve been sent a file request. 

Automate file requests

Creating file requests manually from the Dropbox UI is a quick and simple process, but can be repetitive when performed often. The Dropbox API helps streamline these sorts of tasks, big and small.

Let’s see what it takes to create a file request via the API. We’ll use the Python SDK, but you can implement this in any Dropbox SDK or directly via HTTP calls.

To start, you’ll want to install Dropbox for Python

To create a file request in Python requires only a few lines of code:

import dropbox

dbx = dropbox.Dropbox('YOUR_ACCESS_TOKEN')
req = dbx.file_requests_create(title="August invoices", destination="/File requests/August")
print req.url
print req.id

You’ll want to include your access token, and change the title and destination. See our OAuth guide for more information on how to get an access token and build OAuth into your app.

Assuming your access token can use your entire Dropbox account, the destination can be any path, even if the folders don’t exist. It’s relative to your Dropbox account or folder, and should start with a slash. Since you’ll be creating many of these, it’s a good idea to create a folder to hold all of your file requests.

The code above prints out the request URL, which you can copy and paste to any destination. You’ll want to fully automate your file requests so you can send them monthly, weekly, or any cadence you need. You can include the URL in an automated email message, or a call to the Slack API (or other chat app).

The code also prints the request ID, which you will use to retrieve requested files in the next step.

Take a look at the documentation for dropbox.file_requests.FileRequest to find out what other properties are available to print.

Finally, you’ll want a way to run your script whenever it’s needed. You can use a cron job or a scheduled event service.

Now that you’ve created file requests, you want to also see their status.

Retrieve your requested files

Once you’ve sent your file request to contractors, students, or others, you can wait for the files to come in. They’ll come to your Dropbox folder, just as they did in the manual example. If you get desktop notifications, you’ll know whenever someone uploads on your file request page. There are a couple ways to use the Dropbox API to check in on your file requests.

You can retrieve a current count of files uploaded to a request with a few more lines of Python:

import dropbox

dbx = dropbox.Dropbox('YOUR_ACCESS_TOKEN')
req = dbx.file_requests_get("YOUR_REQUEST_ID")
print req.file_count

Again, you’ll need to include your access token. You’ll also need to know the ID of your file request from the prior step. 

Among the data you get back is the number of files that have been uploaded. The code above prints out the current `file_count` field.

Because file requests use a normal Dropbox folder, you can list the files using the primary Dropbox API. Here’s how I’d print a list of filenames for my August invoice example:

import dropbox

dbx = dropbox.Dropbox('YOUR_ACCESS_TOKEN')
req = dbx.files_list_folder("/File requests/August")
for f in req.entries:
  print f.path_display 

This approach will work up to 2,000 files, which covers most file request use cases. Keep in mind, multiple file requests can use the same folder, which could impact what files end up in this folder. Additionally, anyone with access to the folder could add files outside of the file request system.

Build Dropbox into your workflow

If Dropbox is already a part of your workflow, use the file requests API to simplify and organize how you collect files.

If you’re interested in more Dropbox tutorials, check out our previous blog posts, which contain guides to:

  1. Writing a script for a simple expense organizer app
  2. Writing a photo gallery Web Service from scratch with Node.JS and Dropbox with production deployment on Heroku
  3. Photo gallery tutorial with tags using the File Properties API
  4. OAuth code flow implementation using Node.JS and Dropbox JavaScript SDK

Have ideas for other guides you would like to see? Let us know by posting in the Dropbox Developer Community forums

If you have any questions about this or need help with anything else, you can always contact us here.

Build with Dropbox today at   www.dropbox.com/developers