How to set up Zapier action for saving API-fetched files?

Hey everyone! I’m struggling to create a Zapier action that grabs raw bytes from my API and uses them in Zaps. I’ve been through the docs but can’t seem to get it working. Any tips would be really helpful!

Here’s what I’ve tried so far:

const apiCall = {
  url: 'my-api-endpoint',
  method: 'GET',
  raw: true,
  headers: {
    'Authorization': `Bearer ${bundle.authData.token}`
  }
}

function fetchAndStashFile(z, bundle) {
  const fileRequest = z.request(bundle.inputData.apiCall);
  return z.stashFile(fileRequest);
}

const output = z.dehydrateFile(fetchAndStashFile, {
  apiCall: apiCall
});
return output;

I’m not sure if I’m on the right track here. What am I missing? Thanks in advance for any help!

I’ve been in a similar situation, and I can share what worked for me. Instead of using z.stashFile(), I found success with z.request() and z.stashFile() in separate steps.

Here’s a simplified version of what I did:

const response = await z.request({
  url: 'my-api-endpoint',
  method: 'GET',
  headers: {
    'Authorization': `Bearer ${bundle.authData.token}`
  }
});

const filePromise = z.stashFile(response.content, response.headers['content-type'], 'filename.ext');
return z.dehydrateFile(filePromise);

This approach lets you handle the API response directly, giving you more control over error handling and file naming. Make sure to adjust the content type and filename as needed.

Also, don’t forget to set up the appropriate output fields in your Zapier action to make the file available for subsequent steps in your Zap. Hope this helps!

hey alice45, i’ve dealt with this before. try using z.request() first to get the api response, then use z.stashFile() with that response. something like:

const response = await z.request(apiCall);
return z.stashFile(response.content, response.headers['content-type'], 'myfile.ext');

this worked for me. hope it helps!

I’ve encountered this issue before, and here’s what worked for me. Instead of using z.dehydrateFile(), try using z.request() followed by z.stashFile() directly in your perform function. Here’s a simplified example:

const perform = async (z, bundle) => {
  const response = await z.request({
    url: 'my-api-endpoint',
    method: 'GET',
    headers: {
      'Authorization': `Bearer ${bundle.authData.token}`
    }
  });

  const file = await z.stashFile(response.content, response.headers['content-type'], 'filename.ext');
  return { file };
};

This approach gives you more control over the process and allows for better error handling. Make sure to adjust the content type and filename according to your specific use case, and verify that your Zapier action is correctly set up with the necessary output fields.