How to pass data from authentication to trigger in Zapier app?

I’m building a Zapier app and I’m stuck on how to pass data from the authentication step to a trigger. Specifically, I need to use a tenant ID that I get during authentication in my trigger function.

I’ve tried a few things:

  1. Using global variables
  2. Adding the data to bundle.authData
  3. Storing the data in a module

But none of these seem to work consistently. With global variables, it works sometimes but not always. When I add the tenant ID to bundle.authData during authentication, it’s not there when I try to access it in the trigger.

Here’s a simplified version of what I’m trying to do:

// In authentication
function authenticate(z, bundle) {
  // Get tenant ID from API response
  let tenantId = apiResponse.tenantId;
  
  // Trying to save it
  global.tenantId = tenantId;
  bundle.authData.tenantId = tenantId;
}

// In trigger
function trigger(z, bundle) {
  // Trying to access tenant ID
  let tenantId = global.tenantId || bundle.authData.tenantId;
  
  // Use tenantId in API call
}

What’s the correct way to pass this data from authentication to trigger in Zapier? Any help would be great!

hey davidw, i’ve run into this before. the easiest way is to return the tenant ID in ur auth response like this:

function authenticate(z, bundle) {
return {
access_token: apiResponse.access_token,
tenant_id: apiResponse.tenant_id
};
}

then u can grab it in ur trigger with bundle.authData.tenant_id. works like a charm!

Having worked on several Zapier integrations, I can confirm that returning the tenant ID as part of the authentication response is indeed the most reliable method. However, there’s another approach worth considering.

You can use Zapier’s built-in ‘Session Auth’ feature. This allows you to store session-specific data that persists across all triggers and actions. Here’s how you’d implement it:

In your authentication function, use z.console.log() to output the tenant ID. Then, in your app’s configuration, set up a ‘Session Auth’ step that captures this logged value.

This method is particularly useful when you need to store multiple pieces of data or when the data might change during the session. It’s a bit more complex to set up initially, but it provides greater flexibility in the long run.

I’ve faced a similar issue while developing a Zapier app, and I found that the most reliable method is to return the tenant ID as part of the authentication response. Here’s how I solved it:

In the authenticate function, instead of trying to set global variables or modify bundle.authData directly, return an object that includes both the access token and the tenant ID:

function authenticate(z, bundle) {
  // API call to get token and tenant ID
  return {
    access_token: apiResponse.access_token,
    tenant_id: apiResponse.tenant_id
  };
}

Then in your trigger function, you can access the tenant ID from bundle.authData:

function trigger(z, bundle) {
  const tenantId = bundle.authData.tenant_id;
  // Use tenantId in your API call
}

This approach ensures that the tenant ID is consistently available in all triggers and actions. It’s also more in line with Zapier’s recommended practices for handling authentication data. Hope this helps solve your problem!