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?
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.