How to write fetch in "Code by Zapier"?
Asked Answered
E

2

9

In zapier I use an action of Code By Zapier. It's based on node.js. I need to use fetch for implementing REST-API of my CRM.

Here is the code I wrote, which runs well when I tried it with VS Code (outside Zapier):

// the code by zapier includes already the require('fetch')

var api_token = "..."; // my api
var deal_name = "Example"; // a string

fetch("https://api.pipedrive.com/v1/deals/find?term="+deal_name+"&api_token=" + api_token)
  .then(function(res) {
    return res.json();
  }).then(function(json) {
     var deal_id = json.data[0].id;
     console.log("deal_id="+deal_id);
  }).catch(function(error) {
     console.log("error");
  });

output = {id: 1, hello: "world"}; // must include output...

The error I got from Zapier is:

If you are doing async (with fetch library) you need to use a callback!

Please help me with solving it.

Erogenous answered 17/8, 2015 at 19:53 Comment(0)
I
14

This is a classic mistake when coding in Node.js/callback environments.

You are using console.log which prints to your console, but doesn't return data to the parent (Zapier in this case).

Here is an example of bad and good code:

// bad code
fetch(url)
  .then(function(res) {
    return res.json();
  }).then(function(json) {
    // when i run this in my node repl it works perfect!
    // the problem is this doesn't return the data to zapier
    // it just prints it to the system output
    console.log(json);
  });

// good code
fetch(url)
  .then(function(res) {
    return res.json();
  }).then(function(json) {
    // but if i swap this to callback, this works perfect in zapier
    callback(null, json);
  });

I hope this helps!

Isoline answered 18/8, 2015 at 16:4 Comment(2)
Hi there, could I have many fetchs statement in one zap. Thank you.Needlecraft
@ hungndv, I understood how this mechanism work. You can write fetch inside fetch. You always need the procedure will be finished with callback. Hence, you can't write fetch after fetch! since callback will terminate the ZAP action. However, Bryan recently enabled several actions for one trigger which is done one by one, so you can do code-by-zapier twice and you may succeed to filter or change the 2nd code as result of the first, but I didn't try it yet.Erogenous
S
4

These days, you can also use async/await, as noted by the default comment at the top of the sample code block:

// this is wrapped in an `async` function
// you can use await throughout the function

const response = await fetch('http://worldclockapi.com/api/json/utc/now')
return await response.json()

See further examples in the docs: https://zapier.com/help/create/code-webhooks/javascript-code-examples-in-zaps#step-2

Note that the free-tier has a 1 second timeout (especially relevant if you use Promise.all() to execute multiple fetches!)

Scabious answered 19/8, 2021 at 7:39 Comment(1)
cool, it looks like i can save const json = await response.json() then proceed to parse the json without using await again, and set up a pile of useful variables in my object{} returnBioclimatology

© 2022 - 2024 — McMap. All rights reserved.