How to Send Data to Webhook Using Zapier JavaScript Code Block

I’m struggling with sending data to a webhook through Zapier’s JavaScript code action. The reason I need to use a code action instead of a regular webhook step is because I want to send dynamic data to the same webhook endpoint.

Here’s the code I’m trying to use:

fetch('https://hooks.zapier.com/hooks/catch/123456/abcdef/', {
  method: 'POST',
  body: 'name=john, email: [email protected]'
})
.then(function(response) {
  return response.json();
})
.then(function(data) {
  console.log(data);
})
.then(function() {
  callback();
})
.catch(callback);

The problem is I keep getting this error message: “Error: You must return a single object or array of objects.” I feel like I’m missing something obvious but can’t figure out what it is. Any suggestions would be really helpful!

Had similar issues when I started using Zapier’s code blocks for webhook calls. The main problem is that you’re calling callback() inside the promise chain, which interferes with how Zapier expects the response. Remove the callback references entirely since Zapier handles that automatically when you return a value. Try this approach:

const response = await fetch('https://hooks.zapier.com/hooks/catch/123456/abcdef/', {
  method: 'POST',
  headers: {'Content-Type': 'application/x-www-form-urlencoded'},
  body: 'name=john&[email protected]'
});

const result = await response.json();
return {data: result};

The async/await syntax makes it cleaner and you avoid the callback confusion. Also fix your body format - use ampersands instead of commas for form data. This pattern has worked reliably for me across multiple Zaps.

ur code is almost there! just remember to return an object at the end to fix that error. like, add return {success: true}; right before the catch. that should do it!

The issue stems from Zapier’s requirement that JavaScript code actions must return data explicitly. Your fetch request executes asynchronously, but you’re not returning anything from the main function scope. You need to structure it differently - either use async/await pattern or return the promise chain directly. Here’s what worked for me:

return fetch('https://hooks.zapier.com/hooks/catch/123456/abcdef/', {
  method: 'POST',
  headers: {'Content-Type': 'application/json'},
  body: JSON.stringify({name: 'john', email: '[email protected]'})
})
.then(response => response.json())
.then(data => {
  return {webhook_response: data, status: 'sent'};
});

Also noticed you’re sending the body as a string - consider using JSON format instead. The key is returning the promise chain and ensuring the final then() returns an object that Zapier can process.