How to manage authentication errors in Zapier JavaScript app

I’m starting to learn JavaScript while working on a Zapier app. I have an auth function for testing that fails to report an error when invalid credentials are provided.

My test configuration:

const should = require('should');
const zapier = require('zapier-platform-core');
const MyApplication = require('../../index');
const appTester = zapier.createAppTester(MyApplication);

describe('Authentication Validation', () => {
  zapier.tools.env.inject();

  it('should retrieve user information', done => {
    const requestConfig = {
      authData: { apiKey: process.env.AUTH_KEY },
      inputData: {}
    };

    appTester(MyApplication.triggers['retrieveUsers'].operation.perform, requestConfig)
      .then(response => {
        response.includes('id');
        done();
      })
      .catch(response);
  });
});

Expected successful response:

{"id":2950,"groupName":"Active Users Group","totalUsers":6}

Failure response for authentication issue:

{"STATUS":"ERROR","MESSAGE":"Invalid authentication token"}

My trigger function implementation:

const { replaceVariables } = require('../helpers');

const retrieveUserInfo = (z, bundle) => {
  let apiUrl = 'https://api.example.com/users?method=getUsers&apiKey={{apiKey}}';
  apiUrl = replaceVariables(apiUrl, bundle);

  const request = z.request({ url: apiUrl });
  return request.then(response => {
    response.throwForStatus();
    return z.JSON.parse(response.content);
  });
};

module.exports = {
  key: 'retrieveUsers',
  noun: 'Users',
  
  display: {
    label: 'Retrieve Users',
    description: 'Fetches a list of users when triggered.',
    hidden: true,
    important: false
  },
  
  operation: {
    inputFields: [
      {
        key: 'userGroup',
        label: 'User Group',
        type: 'string',
        required: false
      }
    ],
    outputFields: [
      {
        key: 'totalUsers',
        type: 'string'
      },
      {
        key: 'id',
        type: 'string',
        label: 'User ID'
      },
      {
        key: 'groupName',
        type: 'string',
        label: 'Group Name'
      }
    ],
    perform: retrieveUserInfo,
    sample: { totalUsers: 160, id: 18390, groupName: 'Active Users Group' }
  }
};

When validating the authentication on Zapier’s site, I need the auth process to fail properly and display the error MESSAGE. What steps should I take?

your API’s returning a 200 status even when auth fails - that’s why throwForStatus() isn’t catching it. you’ll need to check the response content in your retrieveUserInfo function and manually throw an error when STATUS shows ERROR.

Your API returns 200 status codes even when authentication fails. That’s why throwForStatus() doesn’t catch it - it only triggers on actual HTTP errors like 401 or 403, not successful responses with error data inside. I ran into this exact issue building my own Zapier integration last year. You’ll need to add error handling after parsing the JSON. In your retrieveUserInfo function, store the parsed response in a variable after z.JSON.parse(response.content). Then check if STATUS equals ‘ERROR’ and throw a new Error with the MESSAGE value. This’ll make authentication failures show up properly in Zapier’s testing instead of letting bad responses slip through.

This is a common issue with APIs that put error messages in the response body instead of using proper HTTP status codes. Your API sends back a 200 status even when there’s an error, so you need to manually check the response.

In your retrieveUserInfo function, parse the JSON response first, then check the STATUS field. Add something like if (parsedResponse.STATUS === 'ERROR') { throw new Error(parsedResponse.MESSAGE); } right after parsing.

This’ll make Zapier catch auth failures properly and show the actual error message during testing.