Trying to add contacts via ActiveCampain API (Laravel)

783 Views Asked by At

I´m trying to integrate the RESTFUL API of ActiveCampaing to my Laravel environment, but I haven’t been so luckier, I'm using GuzzleHttp to make the requests, this is the error image and my code:

 $client = new \GuzzleHttp\Client([‘base_uri’ => ‘https://myaccount.api-us1.com/api/3/’]);

$response = $client->request('POST', 'contacts', [
    'headers' => [
        'Api-Token' => 'xxx',
        'api_action' => 'contact_add',
    ],
    'json' => [
        'email' => '[email protected]',
        'first_name' => 'Julian',
        'last_name' => 'Carax',
    ]
]);

echo $response->getStatusCode(); // 200
echo $response->getBody(); 

Hope you could help me! :D

2

There are 2 best solutions below

0
On BEST ANSWER

you are not sending the data in correct format, from the docs https://developers.activecampaign.com/reference#contact

{
    "contact": {
        "email": "[email protected]",
        "firstName": "John",
        "lastName": "Doe",
        "phone": "7223224241",
        "fieldValues":[
          {
            "field":"1",
            "value":"The Value for First Field"
          },
          {
            "field":"6",
            "value":"2008-01-20"
          }
        ]
    }
}

So create an array with key contact.

$contact["contact"] = [
        "email" => "[email protected]",
        "firstName" => "John",
        "lastName" => "Doe",
        "phone" => "7223224241",
        "fieldValues" => [
            [
                "field"=>"1",
                "value"=>"The Value for First Field"
            ],
            [
                "field"=>"6",
                "value"=>"2008-01-20"
            ]
        ]
    ];

Use try catch blocks as then you can catch your errors

try{
    $client = new \GuzzleHttp\Client(["base_uri" => "https://myaccount.api-us1.com/api/3/"]);

    $response = $client->request('POST', 'contacts', [
        'headers' => [
            'Api-Token' => 'xxx',
            'api_action' => 'contact_add',
        ],
        'json' => $contact
    ]);
    
    if($response->getStatusCode() == "200" || $response->getStatusCode() == "201"){
        $arrResponse = json_decode($response->getBody(),true);
    }
} catch(\GuzzleHttp\Exception\ClientException $e){
    $error['error'] = $e->getMessage();
    if ($e->hasResponse()){
        $error['response'] = $e->getResponse()->getBody()->getContents();
    }
    // logging the request
    \Illuminate\Support\Facades\Log::error("Guzzle Exception :: ", $error);
    // take other actions
} catch(Exception $e){
    return response()->json(
            ['message' => $e->getMessage()],
            method_exists($e, 'getStatusCode') ? $e->getStatusCode() : 500);
}
0
On

You can check at the API docs that the fields email, first_name, last_name are under a contact node.

So make a contact array, put these fields inside and you should be fine.

The fields for first and last name are written line firstName and lastName - camelCase, not snake_case like you did.

Official php client

You should probably use the official ActiveCampaign php api client - that would make your life easier.