Skip to main content

Hi! I’m building an Action and trying to use `fetch` via code mode. However, when doing so I receive a “ReferenceError: fetch is not defined”. In doing some research, it appears that Node.js needs to be v18 in order to use this. Is this correct? Is there a workaround?

const options = {method: 'GET', headers: {accept: 'application/json'}};

fetch('https://api.portal.scanifly.com/api/v1/designs/', options)
.then(response => response.json())
.then(response => console.log(response))
.catch(err => console.error(err));

 I’ve also tried the below courtesy of @Todd Harper 

const Project_Id = ""; // Include your own project ID
const Access_Token = ""; // Include your own access token
let appData = "";

try {
const appResp = await fetch(
`https://api.portal.scanifly.com/api/v1/designs/${Project_Id}?access_token=${Access_Token}`,
{
method: 'GET', // Method
headers: {
'accept': 'application/json'
}
}
);

appData = await appResp.text();
appData = JSON.parse(appData);
} catch (e) {
console.log(e.message);
}

output = /{ appData }]

And received the following error response.

When I ran this is npm.runkit.com/supertest I got the following result.

 

 

Ultimately, I’d like to get to where I can use the `params` to pass the projectId and access_token, but I just hardcoded my projectId and access_token to test the code. Here are the screenshots.

params: {
'projectId': bundle.inputData.projectId,
'access_token': bundle.authData.access_token
}

 

 

 

I’ve found a solution to my problem. The API did not like the `?projectId=` being a part of the variable param in the URL. To resolve this, I used the updated code below.

  const options = {
url: `https://api.portal.scanifly.com/api/v1/designs/${bundle.inputData.projectId}`,
method: 'GET',
headers: {
'X-ACCESS-TOKEN': bundle.authData.access_token
},
params: {
'access_token': bundle.authData.access_token
}
}

return z.request(options)
.then((response) => {
response.throwForStatus();
const results = =response.json];

// You can do any parsing you need for results here before returning them

return results;
});