首页 > 解决方案 > Zapier:在一个触发器中从两个 API 端点提取数据

问题描述

我正在开发一个触发器,我需要从两个 API 端点提取数据。第一个端点是数据库中检索电子邮件地址的联系人,然后要获取该联系人(电子邮件)的详细信息,我需要使用另一个端点。一个是 /Subscriber,另一个是 /Subsriber/ {email}/ Properties。

 

我想知道我是否可以使用一个变量来获取一个触发器中的所有数据,因为我现在已经在单独的触发器中设置了。

 

这是两者的代码

订户:

  url: 'https://edapi.campaigner.com/v1/Subscribers?PageSize=1',
  method: 'GET',
  headers: {
    'Accept': 'application/json',
    'X-API-KEY': bundle.authData.ApiKey
  },
  params: {
    'ApiKey': bundle.authData.ApiKey
  }
};


return z.request(options).then((response) => {
 response.throwForStatus();
 const result = z.JSON.parse(response.content);
 result.id = result.Items;
 return [result];
});

和订阅者属性

const options = {
  url: `https://edapi.campaigner.com/v1/Subscribers/${bundle.inputData.email_address}/Properties`,
  method: 'GET',
  headers: {
    'Accept': 'application/json',
    'X-API-KEY': bundle.authData.ApiKey
  },
  params: {
    'email_address': bundle.inputData.email_address,
    'ApiKey': bundle.authData.ApiKey
  }
}

return z.request(options).then((response) => {
 response.throwForStatus();
 const result = z.JSON.parse(response.content);
 result.id = result.CustomFields;
 return [result];
});

任何帮助表示赞赏。

​</p>

标签: zapier

解决方案


是的,绝对有可能!除非您的订阅者数据实际上需要是一个单独的触发器(这不太可能,因为您可能只是触发了新联系人),否则它可以只是一个函数。尝试类似:

const subscriberPerform = async (z, bundle) => {
  const emailResponse = await z.request({
    url: "https://edapi.campaigner.com/v1/Subscribers?PageSize=1",
    method: "GET",
    headers: {
      Accept: "application/json",
      "X-API-KEY": bundle.authData.ApiKey, // does this need to be both places?
    },
    params: {
      ApiKey: bundle.authData.ApiKey, // does this need to be both places?
    },
  });
  // requires core version 10+
  const email = emailResponse.data.email;

  const emailDataResponse = await z.request({
    url: `https://edapi.campaigner.com/v1/Subscribers/${email}/Properties`,
    method: "GET",
    headers: {
      Accept: "application/json",
      "X-API-KEY": bundle.authData.ApiKey,
    },
    params: {
      email_address: bundle.inputData.email_address, // lots of duplicated data here
      ApiKey: bundle.authData.ApiKey,
    },
  });

  return [emailDataResponse.data.SOMETHING];
};

这是一般的想法。这些 JS 函数可能根本不需要是触发器,这取决于您如何使用它们。

最后一点 - 您不想在每次轮询新联系人时都执行此额外查找;那很浪费。如果你这样做,请检查脱水


推荐阅读