<aside> ⚠️ Note: This documentation is for the first version of our API which is now deprecated. You should probably be looking at the docs for our new API instead, which you can find here.
</aside>
The AddContactToGroup function is used to add a contact to one of the groups in your CRM. Before calling this function, make sure you have already created the group.
When calling this function, the Function parameter sent to the API should be AddContactToGroup.
ContactId - The Id of the contact that should be added to the group. Note: ContactId should be treated like a string, NOT an integer or any other number type.
GroupName - The name of the group. NOTE: If there are any spaces in the group name, you must replace them with underscores (_).
This code shows how you can add a contact to an existing group using the API. Before running this, make sure you've already set up the group in the CRM.
<?php
/************************************************************************************
This file demonstrates how to use the Less Annoying CRM API to add a contact to
a group
If you have any questions, feel free to email [email protected] for help
**************************************************************************************/
//Get your user UserCode and API token from <https://www.lessannoyingcrm.com/app/Settings/Api>
$UserCode = "ABCDE";
$APIToken = "111111111111111111111111111111111111111111111111";
$EndpointURL = "<https://api.lessannoyingcrm.com>";
//First we're going to create a contact
$Function = "CreateContact";
$Parameters = array(
"FullName" => "Tyler King",
);
$Result = CallAPI($EndpointURL, $UserCode, $APIToken, $Function, $Parameters);
//Get the new ContactId
$ContactId = $Result['ContactId'];
//Now we'll add the contact to a group. NOTE: If the GroupName has any spaces in it,
//you need to replace them all with underscores (_)
$Function = "AddContactToGroup";
$Parameters = array(
"ContactId"=>$ContactId,
"GroupName"=>"TestGroupName" //Make sure you replace any spaces with underscores (_)
);
$Result = CallAPI($EndpointURL, $UserCode, $APIToken, $Function, $Parameters);
function CallAPI($EndpointURL, $UserCode, $APIToken, $Function, $Parameters){
$PostData = array(
'UserCode' => $UserCode,
'APIToken' => $APIToken,
'Function' => $Function,
'Parameters' => json_encode($Parameters),
);
$Options = array(
'http' =>
array(
'method' => 'POST', //We are using the POST HTTP method.
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => http_build_query($PostData) // URL-encoded query string.
)
);
$StreamContext = stream_context_create($Options);
$APIResult = file_get_contents("$EndpointURL?UserCode=$UserCode", false, $StreamContext);
$APIResult = json_decode($APIResult, true);
if(@$APIResult['Success'] === true){
echo "Success!";
}
else{
echo "API call failed. Error:".@$APIResult['Error'];
exit;
}
return $APIResult;
}