Skip to main content
Solved

passing profiles to the https://a.klaviyo.com/api/v2/list/{list_id}/members endpoint

  • April 28, 2023
  • 1 reply
  • 144 views

Forum|alt.badge.img+2

Hello

I need help sending data to https://a.klaviyo.com/api/v2/list/{list_id}/members using javascript.
getting error msg as  “Error sending event:”
 Please see my code below: 

 

 const apiKey = 'my api ';
   
    const url = `https://a.klaviyo.com/api/v2/list/{list_id}/members`;

    // The data to be sent as the request body
    const data = {
        api_key: apiKey,
        profiles: [
            {
                email: 'test@example.com',
                $first_name: 'Test',
                $last_name: 'Test',
                $phone_number: '206-234-2345',
                $response: "1"
            }
        ]
    };

    // Options for the fetch() function
    const options = {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify(data)
    };


    // Send the request using fetch()
    fetch(url, options)
        .then(response => {
            alert("response.json() =" + response.json());
            if (response.ok) {
                alert('Event sent successfully!');
            } else {
                alert('Failed to send event:', response.statusText);
            }
        })
        .then(data => console.log(data))
        .catch(error => console.error(error.message));

Best answer by Brian Turcotte

Hi @PFkitchen!

 

My intuition is that there’s likely some sort of formatting error, but it would be helpful if you could send the error that is being logged aside from your custom Error sending event, as it would allow myself and other Community members to gather some more context on the issue.

 

Separately, you may want to consider using our V3 endpoints since they have more detailed error messages:


Best,

Brian

1 reply

Brian Turcotte
Forum|alt.badge.img+37
  • Klaviyo Alum
  • 1393 replies
  • Answer
  • April 28, 2023

Hi @PFkitchen!

 

My intuition is that there’s likely some sort of formatting error, but it would be helpful if you could send the error that is being logged aside from your custom Error sending event, as it would allow myself and other Community members to gather some more context on the issue.

 

Separately, you may want to consider using our V3 endpoints since they have more detailed error messages:


Best,

Brian