Skip to main content

Hey All,

I’m attempting to use a code step to make a GET request from a REST API, but I’m running into a 401 authorization error in the response section of the try/catch piece. 

 

However, the same code/credentials work when I test via Postman and via the testing page of the API documentation itself, so I know the credentials themselves are correct and are unchanged between Zapier/Postman. I really don’t know what other options I have to try at this point and am really at a loss as to where this piece is failing since it works fine elsewhere.

It seems like maybe the requestOptions piece isn’t actually getting properly added/pushed through with the Auth header, but I don’t know why that would be happening either and am just kind of at a loss. Does anyone have experience with the particulars of Zapier JS code steps that knows why this request could be failing to Authorize only within Zapier?

 

(I know the code after that needs work to properly parse the data, I’m not really worried about that and I know I can work out that piece once I have the actual GET request data to come through with a little effort and googling.)

 

Code Step

const ln = inputData.location_name;

const cf = inputData.customer_filter;

 

const myHeaders = new Headers();

myHeaders.append("Authorization", "Basic MYAPICREDENTIALS");

 

const requestOptions = {

  method: "GET",

  headers: myHeaders,

  redirect: "follow"

};

 

try {

  const response = await fetch("https://cloud.servicebridge.com/api/v4.5/Locations?includeContacts=True&locationNameFilter=" + ln + "&customerFilter=" + cf, requestOptions);

  console.log(response);

  const result = await JSON.parse(response);

  console.log(result);

  const contact = result.data.contacts;

  return { contact_role: contact };

 

  } catch (error) {

  console.error(error);

  return { output: { error: error.message } }

  }

 

Postman

 

Hi ​@Kyle P 

Is there a reason you are not trying to use one of these available Zap actions to make the API request?

  • Webhooks - GET
  • Webhook - Custom Request (GET)

Help: https://zapier.com/apps/webhook/integrations#help


TIP: Try asking ChatGPT to help you fix the Zap Code JavaScript.


Hey Troy,

 

Yes, the info I’m pulling contains Null Values that need to line up properly, and the Webhooks options do not allow for parsing through null values, the system automatically clears them out before you can access them via a code step, transform step, etc and you also can’t loop within a loop so I can’t go through the multiple “child” records I need to for each “parent” piece of data (multiple contacts under a location record) so I’m having to go the more manual route here of pulling via a code step.

If there was a way to get the raw code out from the Webhooks - GET option that I could then pass into the code step I feel like that would help, as I’m using the same Auth Header credentials in other places within the ZAP for Webhooks steps and that works fine.

And I’ve tried getting help via the ChatGPT route and the support team as well, but it results in the same error and hasn’t been helpful so far. I don’t think ChatGPT knows whatever small difference there is within the Zapier JS code setup vs standard JS so it’s running into the same issue as me.


@Kyle P 

What is the link to the app API endpoint documentation you reference to configure the API request? (url)


https://cloud.servicebridge.com/developer/index#!/Locations/Locations_Get


@Kyle P 

Help links for using Code in Zaps: https://zapier.com/apps/code/integrations#help


Try this Code generated from ChatGPT:

'use strict';

// Inputs
const ln = inputData.location_name || "";
const cf = inputData.customer_filter || "";

// Replace with your credentials
const userId = "YOUR_USERNAME";
const password = "YOUR_PASSWORD";

// Encode to base64 for Basic Auth
const auth = Buffer.from(`${userId}:${password}`).toString("base64");

// Construct URL
const url = `https://cloud.servicebridge.com/api/v4.5/Locations?includeContacts=true&locationNameFilter=${encodeURIComponent(ln)}&customerFilter=${encodeURIComponent(cf)}`;

try {
const response = await fetch(url, {
method: "GET",
headers: {
"Accept": "application/json",
"Authorization": `Basic ${auth}`
}
});

if (!response.ok) {
throw new Error(`HTTP ${response.status} - ${response.statusText}`);
}

const data = await response.json();

return {
count: data?.data?.length || 0,
first_location: data?.data?.t0] || null,
raw: JSON.stringify(data)
};

} catch (err) {
return { error: err.message };
}

 


Hey Troy

That does work, I’m still a bit baffled as to the why, but I won’t look a gift horse in the mouth.

Could it be something with Zapier’s environment not allowing a headers object and it having to be manually entered as part of the fetch request?


@Kyle P 

Try this…share your code with ChatGPT and share the working code with ChatGPT, and prompt it to explain the differences.