I'm trying to build an automation through Zapier to change an opportunity stage when I mark a meeting in Calendly as a no-show. The zap first finds the contact record ID, then I need to find the associated opportunity through the API (since Find Opportunity is not an out of the box function ). I keep getting an API error: 401 Unauthorized, so the output is missing the correct Opportunity RecordID.
Here's the input data: apiKey (my generated key) & contactRecordId (found in the previous zap step). This is the javascript that Zapier generated for me:
const contactRecordId = inputData.contactRecordId;
const apiKey = inputData.apiKey;
if (!contactRecordId || !apiKey) {
return {
opportunityId: null,
error: 'Missing contactRecordId or apiKey'
};
}
// Query opportunities by Contact RecordID
const url = `https://rest.method.me/api/v1/tables/Opportunity?filter=Contacts eq '${contactRecordId}'`;
try {
const response = await fetch(url, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`
}
});
if (!response.ok) {
return {
opportunityId: null,
error: `API error: ${response.status} ${response.statusText}`,
url: url
};
}
const responseText = await response.text();
if (!responseText) {
return {
opportunityId: null,
error: 'Empty response from API'
};
}
const data = JSON.parse(responseText);
// Return the first opportunity's RecordID
if (data && data.rows && data.rows.length > 0) {
return {
opportunityId: data.rows[0].RecordID,
rowsFound: data.rows.length
};
} else {
return {
opportunityId: null,
error: `No opportunities found`,
rowsFound: data.rows ? data.rows.length : 0
};
}
} catch (error) {
return {
opportunityId: null,
error: `Error: ${error.message}`
};
}