Question

'API is not defined' in Code mode, 'Invalid API Response' in Form mode

  • 4 November 2021
  • 3 replies
  • 22 views

Not sure what I’m doing wrong, but I’m unable to correctly Poll an API endpoint when setting up a trigger on the Zapier Platform.


In Code Mode, I’ve put the following:

const options = {
url: 'https://api.vouchfor.com/v1/vouches',
method: 'GET',
headers: {
'X-Api-Key': bundle.authData.X-API-TOKEN
},
params: {
},
}

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];
});

Whilst in Form Mode I’ve put:

 

The response I’m getting is:

Code Mode

API is not defined
What happened (You are seeing this because you are an admin):
Executing triggers.new_vouch.operation.perform with bundle
API is not defined

Form Mode

Invalid API Response:
- Results must be an array, got: object, ({"vouches":[{"id":"vWqCoyHzAA","status":"RESPONDED)
- Got a result missing the "id" property ([{"id":"vWqCoyHzAA","status":"RESPONDED","url":"https://app.vouchfor.com/vWqCoyHzAA"},{"id":"2Yk92vHCUg","status":"OPENED","url":"https://app.vouchfor.com/request/2Yk92vHCUg"},{"id":"auxP1iDLfF","status":"RESPONDED","url":"https://app.vouchfor.com/au)
What happened (You are seeing this because you are an admin):
Executing triggers.new_vouch.operation.perform with bundle
Invalid API Response:
- Results must be an array, got: object, ({"vouches":[{"id":"vWqCoyHzAA","status":"RESPONDED)
- Got a result missing the "id" property ([{"id":"vWqCoyHzAA","status":"RESPONDED","url":"https://app.vouchfor.com/vWqCoyHzAA"},{"id":"2Yk92vHCUg","status":"OPENED","url":"https://app.vouchfor.com/request/2Yk92vHCUg"},{"id":"auxP1iDLfF","status":"RESPONDED","url":"https://app.vouchfor.com/au)

 

What Zapier returns tin the HTTP response content / what I get in say Postman:

{
"vouches": [
{
"id": "vWqCoyHzAA",
"status": "RESPONDED",
"url": "https://app.vouchfor.com/vWqCoyHzAA"
},
{
"id": "2Yk92vHCUg",
"status": "OPENED",
"url": "https://app.vouchfor.com/request/2Yk92vHCUg"
}...

 

I’ve tried a few different tweaks with the code, however I am having no luck.  Any help anyone can provide would be greatly appreciated!


3 replies

Userlevel 7
Badge +9

dashes aren’t legal in JavaScript identifiers. Try changing 

 

bundle.authData.X-API-TOKEN

 

to 

 

bundle.authData['X-API-TOKEN']

Userlevel 7
Badge +9

And also, rather than returning 

 

return [results];

 

You probably want

 

return results.vouches;

 

As that’s actually the array that contains your items.

Thank you @Zane.  Really appreciate your help!

Reply