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));

