| export default async function main(args) { |
| const { klaviyoPrivateApiKey, userEmail } = args.inputVars; |
| |
| |
| if (!klaviyoPrivateApiKey || !userEmail) { |
| return { |
| next: { path: 'error' }, |
| trace: [{ type: "debug", payload: { message: "Missing required input variables" } }] |
| }; |
| } |
| |
| |
| 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 { |
| |
| 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; |
| |
| |
| if (!responseBody.data || responseBody.data.length === 0) { |
| throw new Error("No profiles found with the given email."); |
| } |
| |
| const profile = responseBody.data[0].attributes; |
| |
| |
| 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}` } } |
| ] |
| }; |
| } |
| } |