Back to all functions

Klaviyo: Find a Profile

Get detailed information about a specific profile in Klaviyo.

Created By
Luuk de Block
Community
download-icon
INPUT VARIABLES
{
userEmail
}
{
klaviyoPrivateApiKey
}
{
}
{
}
{
}
{
}
share-icon
OUTPUT VARIABLES
{
userFirstName
}
{
userLastName
}
{
userPhoneNumber
}
{
userCompanyName
}
{
}
{
}
paths-icon
PATHS
{
success
}
{
error
}
{
}
{
}
{
}
{
}

Function Walkthrough

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

Have something to share?

Share your creation with over 250,000 other global Voiceflow users.

ghraphic
No items found.