Best answer

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

  • 4 November 2021
  • 4 replies
  • 243 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!

icon

Best answer by christina.d 26 January 2022, 00:48

View original

This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

4 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!

Userlevel 7
Badge +9

Hi there,

I wanted to pop in and consolidate Zane’s replies down into one reply for anyone following along:

 

dashes aren’t legal in JavaScript identifiers. Try changing 

 

bundle.authData.X-API-TOKEN

to 

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

 

And also, rather than returning 

 

return [results];

You probably want

return results.vouches;

 

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