title: Fetching and hashing a file from a URL in a Zapier code action in JavaScript
body: <p>I was looking for a way to hash files manipulated in a Zapier code action and since Zapier does not provide such crypto transforms by default, I went on to implement it in Javascript. </p>
  
  <p>The code below does this:</p>
  
  <ol>
  <li>fetch the file from a public URL </li>
  <li>hash said file using sha256</li>
  <li>return the hash in the output</li>
  </ol>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const crypto = require('crypto');
      
  return fetch(inputData.fileUrl)
    .then((res)=&gt;{
      console.log(res.ok);
      console.log(res.status);
      console.log(res.statusText);
      return res.buffer()
    })
    .then((buffer)=&gt;{
      const hash = crypto.createHash('sha256');
      hash.update(buffer);
      callback (null, {"hashValue":hash.digest('hex')});
    })
    .catch(callback)</code></pre>
  </div>
  </div>
  </p>
  
  <p>I am basically calling 'fetch' on the S3 URL, returning the result as a buffer() call on the response object. I then create a 'crypto' object from said buffer, from which I create a sha256 hash and hex digest.</p>
  
  <p>Note: 
  Javascript code actions in Zapier can only take strings as input parameters, so any files you want to hash need to transit through a storage space (e.g. AWS S3 bucket) that has a <strong>publicly accessible</strong> URL. If you manipulate private/sensitive data, you may want to delete the file in a subsequent action in your zap. Beware also of non ascii characters in the URL (e.g. fetching from AWS S3 returns a 403 Forbidden error if your URL includes such characters as '€')</p>
  
  <p>I hope Zapier users will find this useful, e.g. to automate workflows where you need to ensure file data integrity (accounting, invoicing ...)</p>