How to 'create or update contact' along with their custom field values?

I’m using Node js, and I can create/update a contact’s email, name, or phone number. But the custom fields I have never get updated. Any idea why? How to solve it?

Here’s what I got so far.

var data = JSON.stringify({
  "contact": {
		"email": "t@brady.com",
		"firstName": "Tom",
		"lastName": "Brady",
		"phone": "111122233",
        "myCustomField": "myValue"
	}
});

var options = {
  hostname: hostname,
  path: '/api/3/contact/sync',
  method: 'POST',
  headers: {
    'Api-Token': apiToken,
    'Content-Type': 'application/x-www-form-urlencoded',
    'Content-Length': data.length
  }
}

var req = this.https.request(options, function(res){
});
req.on('error', function(err){
  console.log('error: ' + err.message);
});
req.write(data);
req.end();

This will update the contact, but not myCustomField for some reason.

Hi,
I have the same problem, did you figure it out what’s the issue? Could you please provide which solution did you find?

Thanks
Best
Giosk

I have the same problem. I have done code based on this example: https://www.activecampaign.com/api/example.php?call=contact_add

I am not able to find the ID of the custom field. and even if i am adding as like in above link (https://www.activecampaign.com/api/example.php?call=contact_add), it is not working. any help will be appreciated.

Hi innovify,
I found the solution, use this code:

  const body = {
    api_key: “YOUR_KEY_HERE”,
    api_output: ‘json’,
    first_name: ’name here...’,
    last_name: ’last name here...’,
    email: ’email here...’, //<- you must set the email or you’ll get an error
    ‘field[1, 0]’: {
        “key1": “yourDataHere”
    },
    ‘field[2, 0]’: {
        “key2": {/* your data obj here */}
    },
    ‘field[3, 0]’: {
        “keyN”: [/* your array data here */]
    },
}

return await axios.post(
    `${url}/?api_action=contact_add`,
    qs.stringify(body), {
        headers: {
            ‘Content-Type’: ‘application/x-www-form-urlencoded’,
        },
    },
)

if you need a custom field you have to put it in the body in this form:

‘field[N, 0]’: your_value

‘N’ it has to be a number and it will represent your custom field number N.
‘your_value’ can be a text, a number, a map or an Array.

Don’t forget to use the function “qs.stringify(…)” for the body!
(“qs” is the ‘querystring’ library so add the import ’const qs = require(‘querystring’)’)

I hope this helps!
best
Giosk

P.S.
I used axios, but you can use the native nodejs fetch function.

I ran into a similar issue and struggled to find the answer. Our services also use NodeJS and I ended up using the FormData library to build my requests. Instead of using the 'field[N, 0]' syntax, I ended up using the personalization tag values for the custom fields, for example, if a custom field is named “Promo Code”, the syntax would look like 'field[%PROMO_CODE%], valueIPassIn'. I found this solution far more readable.