Skip to main content
Question

Create beforeRequest that performs a GET request


craibuc
Forum|alt.badge.img

I am accessing an API that uses basic auth for its endpoints.  One of the challenges, however, is that the vendor requires changing the account’s password every 7 days.

To handle this, I have a Zap that makes the change, then stores the updated credentials in Zapier Storage.  The storage_secret and storage_key have been added to the authentication.js script as fields.

In my application, I need a way to retrieve these credentials before each call to the vendor’s endpoint.  To achieve this, I wrote this function:

const addAuthHeader = (request, z, bundle) => {

  console.log('***** addAuthHeader *****')

  const options = {
    url: 'https://store.zapier.com/api/records?key={{bundle.authData.storage_key}}',
    method: 'GET',
    headers: {
      'accept': 'application/json',
      'X-Secret': bundle.authData.storage_secret
    },
  }

  return z.request(options).then((response) => {

    const results = response.json;

    const username = results[bundle.authData.storage_key]['Username']
    const password = results[bundle.authData.storage_key]['Password']

    const authorization =  "Basic " + Buffer.from(`${ username }:${ password }`).toString('base64')
    request.headers.Authorization = authorization

    return request;

  });

};

In the application’s index.js file, I reference the function:

module.exports = {
  version: require('./package.json').version,
  platformVersion: require('zapier-platform-core').version,
  authentication: authentication,
  beforeRequest: [addAuthHeader],
  searches: { ... },
  creates: { ... },
};

Unfortunately, when I run one of the searches, I get an error in the addAuthHeader function when it attempts to make the request:

 TypeError: z.request is not a function

 

Am I misunderstanding how the z object works in this situation?

Did this topic help you find an answer to your question?
This post has been closed for comments. Please create a new post if you need help or have a question about this topic.