Skip to main content
Solved

Terminal: 'email or phone number is a required field for each profile'

  • October 10, 2022
  • 1 reply
  • 306 views

Forum|alt.badge.img+2

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.authentications['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?

Best answer by KeviSunshine

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: [{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.

View original
Did this topic or the replies in the thread help you find an answer to your question?

1 reply

KeviSunshine
Expert Problem Solver III
Forum|alt.badge.img+8
  • Expert Problem Solver III
  • 159 replies
  • Answer
  • October 10, 2022

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: [{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.