Automation Blog

How to Get File Contents with a GET Request and Dehydrate a File in Zapier

How to fetch a file from a URL inside a Zapier Code step using an HTTP GET request, then use z.dehydrateFile() to produce a proper Zapier file object that downstream steps — like Gmail, Google Drive, or Dropbox — can use as an attachment or upload.

ZapierCode StepsFile Automation

By Troy Tessalone · · 5 minutes

Automation Guide

A practical field guide from Automation Ace.

How to Get File Contents with a GET Request and Dehydrate a File in Zapier

When a Zapier trigger provides a file URL — a PDF link from a form submission, an export URL from an API, a download link from a webhook payload — downstream steps that accept file uploads need a Zapier file object, not just a URL string. Many apps' Zapier integrations can resolve a URL directly, but when the URL is behind authentication, requires specific headers, or the file needs to be transformed before passing on, a Code step with z.dehydrateFile() is the right tool. This guide explains the dehydration pattern and when to use it.

What File Dehydration Means in Zapier

Zapier uses a concept called "dehydration" and "hydration" for files and other large data that shouldn't be stored inline in a Zap's data pipeline. When you call z.dehydrateFile(), Zapier stores a pointer — a dehydrated file reference — rather than the full file contents. When a downstream step actually needs the file (to attach it to an email, upload it to Google Drive, etc.), Zapier "hydrates" the reference by fetching the file at that moment.

This keeps the Zap's data pipeline lean and allows files larger than the inline data size limit to be passed between steps.

z.dehydrateFile(): Basic Usage

The z object is available as a global in Zapier JavaScript Code steps. z.dehydrateFile() takes a URL and optional options (headers, filename) and returns a dehydrated file pointer:

// Simple case: public URL, no auth required
const filePointer = z.dehydrateFile(inputData.file_url);

output = {
  file: filePointer,
  filename: inputData.filename || 'file'
};

Map the file output field to the attachment or upload field in the downstream step. Zapier will fetch the file from the URL when the downstream step runs.

z.dehydrateFile() with Authentication Headers

If the file URL requires authentication — a Bearer token, API key, or Basic Auth — pass the headers in the options object:

const filePointer = z.dehydrateFile(inputData.file_url, {
  method: 'GET',
  headers: {
    'Authorization': 'Bearer ' + inputData.api_token
  }
});

output = {
  file: filePointer,
  filename: inputData.filename || 'document.pdf'
};

The headers are stored as part of the dehydrated reference and used when Zapier hydrates the file for the downstream step.

Fetching File Contents Directly with GET

For cases where you need to inspect or transform the file contents in the Code step itself before passing them on — reading a CSV to extract a row count, parsing a JSON file, or checking the file's content type — use fetch directly:

const response = await fetch(inputData.file_url, {
  method: 'GET',
  headers: {
    'Authorization': 'Bearer ' + inputData.api_token
  }
});

if (!response.ok) {
  throw new Error(`Failed to fetch file: ${response.status} ${await response.text()}`);
}

const contentType = response.headers.get('content-type') || '';
const filename = inputData.filename || 'file';

// For text-based files (CSV, JSON, TXT): read as text
const text = await response.text();

// For binary files (PDF, images): dehydrate the URL instead
// const filePointer = z.dehydrateFile(inputData.file_url, {
//   headers: { 'Authorization': 'Bearer ' + inputData.api_token }
// });

output = {
  content_type: contentType,
  filename: filename,
  text_content: text.substring(0, 5000), // truncate for output field limits
  character_count: String(text.length)
};

Important: Zapier Code step output values must be strings or numbers — you cannot output raw binary file contents directly. For binary files (PDFs, images, Office documents), use z.dehydrateFile() to produce the file pointer rather than trying to output the binary content inline.

Full Pattern: Fetch, Inspect, and Dehydrate

A common workflow: validate the file is accessible and check its content type, then produce a dehydrated file pointer for the downstream upload step:

// First, verify the file is accessible
const checkResponse = await fetch(inputData.file_url, {
  method: 'HEAD',
  headers: {
    'Authorization': 'Bearer ' + inputData.api_token
  }
});

if (!checkResponse.ok) {
  throw new Error(`File not accessible: ${checkResponse.status}`);
}

const contentType = checkResponse.headers.get('content-type') || 'application/octet-stream';
const contentLength = checkResponse.headers.get('content-length') || '0';

// Produce a dehydrated file pointer for downstream steps
const filePointer = z.dehydrateFile(inputData.file_url, {
  method: 'GET',
  headers: {
    'Authorization': 'Bearer ' + inputData.api_token
  }
});

output = {
  file: filePointer,
  filename: inputData.filename || 'document',
  content_type: contentType,
  file_size_bytes: contentLength,
  file_accessible: 'true'
};

Reading a CSV File and Extracting Data

If the file is a CSV and you want to extract specific data from it:

const response = await fetch(inputData.csv_url, {
  headers: { 'Authorization': 'Bearer ' + inputData.api_token }
});
const csv = await response.text();

const lines = csv.trim().split('\n');
const headers = lines[0].split(',').map(h => h.trim().replace(/"/g, ''));
const rows = lines.slice(1).map(line => {
  const values = line.split(',').map(v => v.trim().replace(/"/g, ''));
  const row = {};
  headers.forEach((h, i) => { row[h] = values[i] || ''; });
  return row;
});

output = {
  row_count: String(rows.length),
  headers: headers.join(', '),
  first_row: JSON.stringify(rows[0] || {}),
  all_rows: JSON.stringify(rows)
};

For many CSV and document jobs, you can skip code: Files by Zapier converts files to text and extracts CSV rows directly.

When to Use z.dehydrateFile() vs. Passing a URL Directly

Many Zapier app integrations (Gmail attachments, Google Drive uploads, Dropbox uploads) accept either a direct URL or a dehydrated file pointer. Use z.dehydrateFile() when:

  • The file URL requires authentication headers that the downstream app action can't set
  • The file URL is temporary or expiring and should be fetched immediately when the step runs
  • You want to set a specific filename that differs from what the URL returns
  • The app's Zapier integration specifically requires a file object (hydrated reference) rather than a URL string

For publicly accessible URLs with no auth requirements, passing the URL directly to the downstream step is simpler and avoids an extra Code step.

z.dehydrateFile() is how Code steps bridge the gap between a file URL that requires authentication and a downstream step that expects a Zapier file object. The dehydration/hydration pattern keeps large file data out of the Zap's data pipeline until the moment it is actually needed — which is both efficient and required for binary files that can't be serialized as output strings.

For more on HTTP GET requests in Code steps, see HTTP GET requests in a Zapier Code step. For the z object and other JavaScript globals, see JavaScript libraries in Zapier Code steps. For help building a file processing workflow, talk to Automation Ace.

ZapierCode StepsFile Automation

Disclaimer: This article may include links to apps, products, or services. Some links may be affiliate links, which means Automation Ace may earn a commission at no extra cost to you.

Build Better Systems

Ready to automate with confidence?

Share your tools, process, and goals. Automation Ace can design the workflow, integration, AI assist, or code bridge that fits your business.

Start a Project →