Using fetch with callback in Zapier Code actions

I’m working with a Code by Zapier action that runs on node.js. I want to make HTTP requests to connect with my CRM’s REST API using the fetch library.

My current code works fine when I test it locally, but Zapier gives me an error saying I need to use a callback for async operations with fetch.

Here’s what I’m trying to do:

// zapier already includes fetch

var auth_key = "my_token_here";
var contact_name = "John Smith";

fetch("https://api.mycrm.com/v2/contacts/search?query="+contact_name+"&token=" + auth_key)
  .then(function(response) {
    return response.json();
  }).then(function(data) {
     var contact_id = data.results[0].id;
     console.log("Found contact: "+contact_id);
  }).catch(function(err) {
     console.log("Request failed");
  });

output = {status: "complete", message: "processed"};

Zapier keeps telling me to use a callback for async operations. How do I modify this code to work properly in their environment? What’s the right way to handle promises in Code by Zapier?

Zapier’s Code action doesn’t handle promises the way you’d expect. It uses a callback pattern instead, so you need to call the callback function when your async stuff finishes.

Here’s the fix:

var auth_key = "my_token_here";
var contact_name = "John Smith";

fetch("https://api.mycrm.com/v2/contacts/search?query="+contact_name+"&token=" + auth_key)
  .then(function(response) {
    return response.json();
  }).then(function(data) {
     var contact_id = data.results[0].id;
     console.log("Found contact: "+contact_id);
     
     callback(null, {status: "complete", message: "processed", contact_id: contact_id});
  }).catch(function(err) {
     console.log("Request failed");
     callback(err);
  });

Basically, use callback(null, result) when things work and callback(error) when they don’t. Don’t try to set output variables directly - the callback tells Zapier you’re done and passes the data to the next step.

Zapier’s execution environment needs you to explicitly signal when async operations finish. When you use fetch without proper callback handling, Zapier thinks your code is still running and never completes. I’ve hit this exact issue before. Zapier Code actions require you to signal completion through callbacks, even with promise-based APIs like fetch. Your code sets the output variable but never tells Zapier you’re done. Watch out for error handling - your callback needs to run even if the API returns weird data. I learned this when my CRM API sometimes returned empty result arrays, causing undefined errors that blocked the callback. Always validate data structure before accessing nested properties like data.results[0].id. Also, Zapier has timeout limits. If your API is slow, add timeout handling to your fetch request so the callback fires within Zapier’s execution window.

yeah, the callback thing’s annoying but that’s just how zapier works. you can’t return stuff like normal node.js - you’ve gotta use the callback pattern or it’ll timeout. make sure you’re calling callback in both your .then and .catch blocks, otherwise zapier thinks your code’s still running.