Klaviyo: Find a Profile

Template
Function
1
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.
Overview
This is some text inside of a div block.
by
This is some text inside of a div block.

Heading 1

Heading 2

Heading 3

Heading 4

Heading 5
Heading 6

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.

Block quote

Ordered list

  1. Item 1
  2. Item 2
  3. Item 3

Unordered list

  • Item A
  • Item B
  • Item C

Text link

Bold text

Emphasis

Superscript

Subscript

Heading
Based in
This is some text inside of a div block.
Heading

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.