Hire 
Luuk de Block
Submit
Note: Voiceflow is not responsible for the exchange of services between you and the partner.
Thank you! An email will be sent connecting you with the expert.
Oops! Something went wrong while submitting the form.

Klaviyo: Find a Profile

Template
Function
6
Template
Function
by
Luuk de Block

Get detailed information about a specific profile in Klaviyo.

Created:

Heading

Voiceflow APIs used:

Channels
No items found.
Created By
Luuk de Block
This is some text inside of a div block.

Function Code Snippet

 
export default async function main(args) {
const { klaviyoPrivateApiKey, userEmail } = args.inputVars;

// Validate input variables
if (!klaviyoPrivateApiKey || !userEmail) {
return {
next: { path: 'error' },
trace: [{ type: "debug", payload: { message: "Missing required input variables" } }]
};
}

// URL encode the email parameter
const encodedEmail = encodeURIComponent(userEmail);
const url = `https://a.klaviyo.com/api/profiles/?filter=equals(email,"${encodedEmail}")`;
const headers = {
'Authorization': `Klaviyo-API-Key ${klaviyoPrivateApiKey}`,
'accept': 'application/json',
'revision': '2024-05-15'
};

try {
// Send the GET request to Klaviyo
const response = await fetch(url, {
method: 'GET',
headers: headers
});

if (!response.ok) {
const errorBody = await response.text;
console.log("Raw error response body:", errorBody);
throw new Error(`HTTP error! status: ${response.status}, body: ${errorBody}`);
}

const responseBody = await response.json;

// Check if the response contains profiles
if (!responseBody.data || responseBody.data.length === 0) {
throw new Error("No profiles found with the given email.");
}

const profile = responseBody.data[0].attributes;

// Extract the required values
const userFirstName = profile.first_name;
const userLastName = profile.last_name;
const userPhoneNumber = profile.phone_number;
const userCompanyName = profile.organization;
const userFunctionTitle = profile.title;
const userProfilePicture = profile.image;
const userAddress = profile.location?.address1;
const userCity = profile.location?.city;
const userCountry = profile.location?.country;
const userZipCode = profile.location?.zip;

return {
outputVars: {
userFirstName,
userLastName,
userPhoneNumber,
userCompanyName,
userFunctionTitle,
userProfilePicture,
userAddress,
userCity,
userCountry,
userZipCode
},
next: { path: 'success' },
trace: [{ type: "debug", payload: { message: "Profile information retrieved successfully" } }]
};

} catch (error) {
return {
next: { path: 'error' },
trace: [
{ type: "debug", payload: { message: `Error: ${error.message}` } }
]
};
}
}
copy-icon

Explore More Templates

Build and submit a Template to have it featured in the community.

ghraphic
No items found.
No items found.