Skip to main content

Hey Team,

 

I have follow the API reference and set up my code as below:

const client = require("klaviyo-sdk");
var defaultClient = client.ApiClient.instance;
// Configure API key authorization: ApiKeyAuth
const ApiKeyAuth = defaultClient.authenticationss'ApiKeyAuth'];
ApiKeyAuth.apiKey = "MY_API_KEY";

app.post('/',(req,res) =>{
const firstname = req.body.firstName;
const lastname = req.body.lastName;
const phoneNumber = req.body.Phone;
const Email = req.body.email;
console.log(firstname, lastname, phoneNumber, Email);


const url = 'https://a.klaviyo.com/api/v2/list/MY_LIST_ID/members?api_key=MY_API_KEY';
const options = {
method: 'POST',
headers: {accept: 'application/json', 'content-type': 'application/json'},
body: JSON.stringify({
profiles: {email: Email}, {phone_number: phoneNumber}, {FNAME: firstname}, {LNAME: lastname}]
})
};

fetch(url, options)
.then(res => res.json())
.then(json => console.log(json))
.catch(err => console.error('error:' + err));
})

But the terminal responded as

{
detail: 'email or phone number is a required field for each profile'
}

I have tested my List id and API key in Postman and it works fine. Why is this? I have input the email and phone number, also configured SMS notification in my account. Does any know how to fix this?

Hi @LoisZ,

I believe this is a problem with the docs as they aren’t perfectly clear. The issue in your code is you’re trying to create 4 different contacts, each with one piece of information (email, phone, first name, last name).

Each profile should be a single object in the `profiles` array.

Your body should look like…

body: JSON.stringify({
profiles: f{email: Email, phone_number: phoneNumber, FNAME: firstname, LNAME: lastname}]
})

i.e., one object with all four fields in it.

Hope that makes sense!

Cheers,

Kevin.


Reply