Skip to main content

Im still new to JS and web-dev in general, but I am trying to use Code by Zapier blocks to run a companies API to retrieve CSV data. The API POST’s an authentication request which returns an ID string as a JSON. You then use that preauthenticated ID, appended to their base URL to GET the CSV file once it is generated. I have checked with chatGPT, and JSHint.com and am not finding any errors. Furthermore, I took screenshots of network request activity after clicking test, and according to the companies intergrations team the traffic is consistent with a successful call to the server, and response from it. This leads me to believe I am messing up something with my implementation with respect to Zapiers environment specifically, but I cant identify where from Zapiers documentation for what I imagine is lack of experience. Any help or guidance would be greatly appreciated as I feel like I am maddeningly close to the solution but cant quite get there on my own.

The code is as follows: *note: code is currently paired down to only get the ID returned for troubleshooting purposes*

console.log("start");
const uuid = inputData.uuid; const sec = inputData.secret;
const base = *organization base url*
const sdate = inputData.startdate;
const edate = inputData.endate const authHeader = 'Basic ' + Buffer.from(`${uuid}:${sec}`).toString('base64');
const body = JSON.stringify({
'csv_type': 'paid_contributions',
'date_range_start': `${sdate}`,
'date_range_end': `${edate}`
}); 
console.log(body);
output = {id: "no data"};
async function getID() {
try {
   const response = await fetch(`${base}`, {
      method: 'POST',
      headers: {
      'Authorization': authHeader,
      'Accept': 'application/json',
      'Content-Type': 'application/json'
      },
      body: body
   });
   let obj = await response.json();
   console.log(obj);
   output = {id: obj};
   callback(null, obj);
   } catch (error) {
   console.error(error);

   callback(error);
   }
}

getID();

network request traffic:

 

Thank you!! Sometimes its the little things that you can miss so easily! I didnt think about the function call, as I had used await on the internal steps! Onward and upward!


Now i think I see the problem. It’s not waiting for your answer. at the bottom, use the await keyword:

await getID();


Revised code: Added empty console.log() at top of catch block and in try() after the fetch() and let obj = …

:
console.log("start");
const uuid = inputData.uuid;
const sec = inputData.secret;
const base = 'https://secure.actblue.com/api/v1/csvs/';
const sdate = inputData.startdate;
const edate = inputData.endate;
const authHeader = 'Basic ' + Buffer.from(`${uuid}:${sec}`).toString('base64');
const body = JSON.stringify({
'csv_type': 'paid_contributions',
'date_range_start': `${sdate}`,
'date_range_end': `${edate}` });
console.log(body);
output = {id: "no data"};
async function getID() {
   try {
      const response = await fetch(`${base}`, {
         method: 'POST',
         headers: {
            'Authorization': authHeader,
            'Accept': 'application/json',
            'Content-Type': 'application/json' },
         body: body });
   console.log();
   let obj = await response.json();
   console.log();
   console.log(obj);
   output = {id: obj};
   callback(null, obj);
   } catch (error) {
      console.log();
      console.error(error);
      callback(error); }
   }
getID();

 

Seems that its breaking down after the fetch() and skipping everything else?


Cool! Thank you for the clarificatino. My next question is whether we can insert a couple of simple console.logs -even without variables - right at the top of the catch block and after you completed the fetch but before you get the JSON. I’m wondering if we take it step by step, we can dial in on where the thing isn’t working right. I don’t see an obvious candidate for what’s wrong, so I try to let the code tell me. 


Sorry, here is the Zapier test output:
 

The network traffic picture was from the developer tools network tab opened up on the Zapier page while I ran the test. You can see here that I am getting 0 errors (which is consistent with the successful network traffic) but only the first two console.log statements are printing (and do demonstrate the POST body contains the correct information in the correct format), and the returned JSON is not being picked up by the script.


What error are you experiencing? Can you share a screenshot of what is going wrong? You’re showing code and a network history (presumably from running this code in some other context?) but not what Zapier is complaining about. If you share that, we might be abel to help faster!