I am having trouble adding a contact to ActiveCampaign using the API. I used the the contact_sync documentation to the best of my availability but can’t figure it out.
Would someone be kind enough to post a working example.
I’m developing using Gatsby/React → GitHub → Netlify, using a serverless lambda function for the POST request.
Here is my axios POST:
{
method: 'post',
url: 'https://ACCOUNT.api-us1.com/admin/api.php?api_key=xxxxxxxxxxxx&api_action=contact_sync&api_output=json',
headers: { 'Content-Type': 'Content-Type: application/x-www-form-urlencoded' },
body: {
email: 'email@email.com',
first_name: 'John'
}
}
And here is the response:
{
result_code: 0,
result_message: 'Could not add contact; missing email address',
result_output: 'json'
}
If someone could help me get up and running I would be indebted.
I wanted to make sure to close this and share my answer.
Thanks so much to @reza jafari for his comment in this post on StackOverflow where he brought to my attention the code window on the right margin of Postman where you can choose the language/server from a dropdown and it provides the correctly formatted response.
I was able to get my post working in Postman, and this little trick squared me away. I’ll go ahead and post my solution to close this post.
const axios = require("axios")
const qs = require("qs")
exports.handler = async function (event) {
const { email, first_name } = JSON.parse(event.body)
const data = qs.stringify({
email: email,
first_name: first_name,
tags: '"api"',
"p[1]": "1",
})
const config = {
method: "post",
url: "https://ACCOUNT.api-us1.com/admin/api.php?api_key=xxxxxxxxx&api_action=contact_sync&api_output=json",
headers: {
"Api-Token":
"xxxxxxxxx",
"Content-Type": "application/x-www-form-urlencoded",
},
data: data,
}
try {
const response = await axios(config)
return {
statusCode: 200,
body: JSON.stringify(response.data),
}
} catch (err) {
return {
statusCode: 500,
body: JSON.stringify(err),
}
}
}
2 Likes
Hi @paulxavierinternatio,
Thank you for the update and solution! The v1 APIs generally require the data to be submitted in the x-www-form-urlencoded
format rather than JSON, so this is why the extra step using qs.stringify()
was necessary. If you end up using any of the v3 APIs in your application this step probably won’t be needed. Please let us know if you have any other questions or run into any further issues while you are exploring the APIs!
Thank you,
-Matt
1 Like
Thank you Matt for the clarification.
v/r,
John