Files#

Zapier supports some limited file operations via both triggers and actions, though they behave a bit differently depending on what you want to do. At a high level, this is what you can expect:

Triggers via URLs/Dehydration#

This is how you'd surface a file hosted by your app, so other apps can consume it.

If your files aren't private (IE: they have a public route), just provide the URL in the normal payload (right alongside id, name, etc.). Our system is smart enough to download public URLs when they get used like a file!

However, if the files are behind authentication, you can define a route for us to retrieve the file at some time in the future and we'll attempt the normal style of authentication:

var Zap = {
  your_trigger_post_poll: function(bundle) {
    var records = z.JSON.parse(bundle.response.content);
    return _.map(records, function(record) {
      // if you just do url, we'll include any standard authentication headers
      record.file = z.dehydrateFile('https://yoursite.com/files/download/' + record.id);
      return record;
    });
  }
}

You can also define a more specific request if there is more data you need to provide to activate the download, like a special key or checksum:

var Zap = {
  your_trigger_post_poll: function(bundle) {
    var records = z.JSON.parse(bundle.response.content);
    return _.map(records, function(record) {
      // if you provide the full request, we will NOT include
      // any standard authentication headers
      var url = 'https://yoursite.com/files/download/' + record.id;
      record.file = z.dehydrateFile(url, {
        method: 'post',
        headers: {
          'X-Download-Key': record.key
        }
      }, {
        name: record.fileName, // if blank we will guess/inspect!
        length: record.size // if blank we will guess/inspect!
      });
      return record;
    });
  }
}

And we'll handle the rest!

Actions via Multipart#

This is how you'd accept binary data to be uploaded to your app, the data is coming from some other app.

Downloading files is a little simpler than uploading them, so files in actions is a bit more involved. By default we attempt multipart uploading and include the original JSON right alongside, but you can (of course) tweak this with the developer platform.

Let's assume your API can accept JSON as well as JSON + multipart for attachments, if you set up this action:

You can expect this request to be sent by default (no scripting required at all):

POST https://yoursite.com/files/upload

Content-Type: multipart/form-data; boundary=f94636b7375c4a37862029d4dc8bafe7


--f94636b7375c4a37862029d4dc8bafe7
Content-Disposition: form-data; name="data"
Content-Type: application/json; charset=utf-8

{"path": "/user/provided/path", "delete_date": "2014-10-10T13:59:36"}
--f94636b7375c4a37862029d4dc8bafe7
Content-Disposition: form-data; name="file_to_upload"; filename="user-provided-file.pdf"
Content-Type: application/pdf

<BINARY DATA HERE>
--f94636b7375c4a37862029d4dc8bafe7--

The name="data" key for the JSON subtype in multipart/form-data is the default, though the rest of the multipart files will respect your action field keys.

Limited Customization Via Scripting#

If you have some other method of uploading files, you'll need to break out the scripting platform. Probably the most common way to tweak the upload is with the classic pre_write method.

A popular alternative pattern is likely just a pure multipart/form-data, no JSON at all. We support that just fine, just remove the Content-Type and return an object as request.data:

var Zap = {
  your_action_pre_write: function(bundle) {
    // bundle.request.files is an object of strings: arrays
    // bundle.request.files.file_to_upload is an array:
    // * first item is the filename, if any
    // * second item is a zapier.com endpoint that will stream the file
    // * third item is the mimetype, if any

    bundle.request.headers['Content-Type'] = 'application/x-www-form-urlencoded';
    // leave request data as object, not string!
    bundle.request.data = z.JSON.parse(bundle.request.data);
    // we will mix request.data and request.files together
    return bundle.request; // let zapier complete it
  }
}

That pure multipart/form-data raw request will now look like this:

POST https://yoursite.com/files/upload

Content-Type: multipart/form-data; boundary=0c61d2f9bd1a4675a2db6afe21f230f1


--0c61d2f9bd1a4675a2db6afe21f230f1
Content-Disposition: form-data; name="path"

/user/provided/path
--0c61d2f9bd1a4675a2db6afe21f230f1
Content-Disposition: form-data; name="delete_date"

2014-10-10T13:59:36
--0c61d2f9bd1a4675a2db6afe21f230f1
Content-Disposition: form-data; name="file_to_upload"; filename="user-provided-file.pdf"
Content-Type: application/pdf

<BINARY DATA HERE>
--0c61d2f9bd1a4675a2db6afe21f230f1--

All name keys respect the keys provided.

Advanced Streaming via Scripting#

At this time we do no support advanced streaming of files via the scripting platform (for example, uploading a file to receive an attachment ID that gets mixed into the normal JSON for POSTing). We may support that in the future, please send us an email if you have questions!

↑ Was this documentation useful? Yes No