How to add a contact to ActiveCampaign using API v1 contact_sync/contact_add

631 Views Asked by At

I am having trouble adding a contact to ActiveCampaign. I read a post in here: How to add a contact to a list in ActiveCampaign API v3 and am using v1 of the API. I used their contact_sync documentation to the best of my availability.

I'm developing using Gatsby/React --> GitHub --> Netlify, using a lamda 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 protected]',
    first_name: 'John'
  }
}

And received the following response:

{
  result_code: 0,
  result_message: 'Could not add contact; missing email address',
  result_output: 'json'
}

I'm talking to their endpoint. I just can't figure out how to feed the endpoint the email address?

Does anyone have a working example they would be kind enough to share? Guidance of any kind would be greatly appreciated!

1

There are 1 best solutions below

0
On

I wanted to make sure to close this and share my answer.

Thanks so much to @reza jafari for his comment in this post 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 don't have enough reputation to upvote @reza's response so wanted to acknowledge it here.)

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),
    }
  }
}