<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 SearchContacts function is used to retrieve a list of contacts and companies that match a search.

When calling this this function, the Function parameter sent to the API should be set to SearchContacts.

If successful, this function will return an array with a list of matching contacts and companies.

Parameters

SearchTerms - The terms you want to search for (contact name, email, phone, etc.). This will search all fields for all contacts and companies in your CRM. If you want to return a list of all contacts and companies without a search filter, pass an empty string.

Note: If you want to search for contacts in a specific group, you can do that by entering "Group:GROUP_NAME" as a Search term. GROUP_NAME is the name of the group with spaces replaced by underscores.

NumRows (optional) - The maximum number of rows you want returned. Can be between 1 and 500.

Page (optional) - Use this if your results are limited by the number of Rows. For example, if your first search is capped at NumRows, you can then run a second search with Page = 2, and then another with Page = 3 in order to retrieve even more results.

Sort (optional) - The field you'd like to sort the results by. Can be FirstName, LastName, DateEntered, DateEdited, or Relevance (which uses an algorithm to guess which contacts you want to see most based on your search keywords, and the last time you edited the contact).

RecordType (optional) - By default, contacts and companies will be returned by this function. You can pass "Contacts" or "Companies" into this function if you want to filter to only get one record type or the other. Note: this is case sensitive, so "contacts" would not work.

Example Code

This code shows how to search the contacts and companies in your CRM:

<?php
/************************************************************************************
    This file demonstrates how to use the Less Annoying CRM API to search for a
    contact/company. You can also use it without any search terms to retrieve a list 
    of all contacts
 
    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>";
 
//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 = "SearchContacts";
$Parameters = array(
    "SearchTerms"=>"John Doe",
    "NumRows"=>25, //Optional (defaults to 25): Max number of rows returned (must be 1-500)
    "Page"=>1, //Optional (defaults to 1): use this to retrieve more rows if limited by NumRows
    "Sort"=>"Relevance" //Optional. Can be FirstName, LastName, DateEntered, DateEdited, or Relevance
);
 
$Result = CallAPI($EndpointURL, $UserCode, $APIToken, $Function, $Parameters);
 
var_dump($Result);
 
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;
}