ActiveCampaign is a powerful marketing automation platform that provides businesses with tools to streamline their customer engagement process. One of the key features of ActiveCampaign is its API, which allows developers to interact with the platform programmatically, enabling seamless integration with other applications and services. Getting ActiveCampaign Contact by Email API is a prime example of how the API can be utilized for specific tasks, such as fetching contact information based on email addresses.
Get Contacts by Email in ActiveCampaign API
In this article, we will demonstrate how to retrieve contact information from ActiveCampaign using a contact’s email address through the API with PHP code examples.
Get ActiveCampaign Contacts by Email PHP Code Example
function getContactByEmail(Client $client, string $email)
{
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
throw new InvalidArgumentException('Invalid email address.');
}
$response = $client->contacts->list(['search' => $email]);
if (isset($response['contacts'][0])) {
return $response['contacts'][0];
}
return null;
}
Article Highlights
- Retrieve ActiveCampaign contacts using their email address with the help of the ActiveCampaign API and PHP.
- Set up the ActiveCampaign PHP Library using Composer and authenticate with the ActiveCampaign API using the API key and URL.
- Create a getContactByEmail function to search for a contact by email and retrieve their information using the ‘contact_list’ endpoint.
- Test the getContactByEmail function, handle the results, and troubleshoot common errors.
- Apply the knowledge gained to develop custom integrations, automated workflows, and internal tools for managing ActiveCampaign contacts more efficiently.
- Explore further resources and documentation to expand your understanding of the ActiveCampaign API and PHP.

Table of Contents
In this article, we will cover the following topics.
- The Importance of using APIs to interact with services.
- Prerequisites.
- Setting up the ActiveCampaign PHP Library.
- Authenticating with the ActiveCampaign API.
- Get Contact by Email Function.
- Testing the Function.
- Get ActiveCampaign Contact by Email through API Summary.
Why Use the ActiveCampaign API
APIs (Application Programming Interfaces) play a crucial role in modern software development, enabling applications and services to communicate and share data in a standardized way. Developers can easily integrate various tools and platforms using APIs to interact with services to create more powerful and flexible solutions.
In the case of ActiveCampaign, the API allows developers to interact with the platform programmatically, making it possible to automate tasks, retrieve data, and integrate ActiveCampaign with other services to enhance overall functionality.
ActiveCampaign API Requirements
A thorough understanding of the prerequisites is essential to ensure a smooth implementation of the tutorial. You will need an ActiveCampaign account, a PHP environment setup, an understanding of APIs, and the ActiveCampaign SDK installed.
- ActiveCampaign account
- PHP Environment
- Basic understanding of PHP & APIs
- Setup the ActiveCampagin API PHP SDK
Steps to Get ActiveCampaign Contacts by Email
To set up the ActiveCampaign PHP library, follow the following steps.
Install the ActiveCampaign API PHP client using Composer
To set up the ActiveCampaign PHP Library, you must install the ActiveCampaign API PHP client using Composer. The client is a wrapper around the ActiveCampaign API, making it easier to interact with the API using PHP.
Follow the steps below to install the client:
- Open a terminal or command prompt in your PHP project’s root directory.
- If you haven’t already, initialize a new Composer project by running the following command:
composer init
This command will prompt you to enter some basic information about your project and generate a composer.json file.
3. Install the ActiveCampaign API PHP client by running the following command:
composer require activecampaign/api-php
The composer will download the client library and its dependencies and update the composer.json and composer.lock files accordingly. The library will be placed in the vendor directory in your project.
Import the library into your project.
After installing the ActiveCampaign API PHP client using Composer, import the library into your PHP script to use its classes and functions. Follow these steps to import the library:
- Open the PHP script where you want to use the ActiveCampaign API PHP client.
- At the beginning of the script, add the following line to include Composer’s autoloader:
require_once 'vendor/autoload.php';
Composer’s autoloader ensures that the required classes from the ActiveCampaign API PHP client and its dependencies are automatically loaded when needed.
3. Import the ActiveCampaign Client class using a use statement. This makes it easier to reference the class throughout your script:
use ActiveCampaign\Client;
Authenticate with the ActiveCampaign API
Before interacting with the ActiveCampaign API, you need to authenticate your requests by providing your API key and API URL. The ActiveCampaign API uses API keys for authentication, ensuring only authorized users can access the platform’s resources.
Create a new instance of the ActiveCampaign API class
To create a new instance of the ActiveCampaign API class, use the Client class from the ActiveCampaign API PHP client library. The Client class serves as the main entry point for interacting with the API.
Follow these steps to create a new instance:
- In your PHP script, after importing the Client class, create a variable to store your API key and another to store your API URL. Replace the placeholders with your actual API key and API URL obtained from your ActiveCampaign account:
$apiKey = "YOUR_API_KEY";
$apiUrl = "YOUR_API_URL";
2. Create a new instance of the Client class, passing the API key and API URL as arguments to the constructor:
$client = new Client($apiKey, $apiUrl);
Now you have an instance of the Client class with your API credentials, which you can use to authenticate requests to the ActiveCampaign API.
Set the API key and URL
When creating a new instance of the Client class, you are already setting the API key and API URL, as demonstrated in the previous section. The Client class constructor accepts the API key and API URL as parameters and stores them internally to authenticate requests made using the instance.
The ActiveCampaign API PHP client library handles the authentication process for you, so you don’t need to set the API key and URL for each request manually. Simply use the methods provided by the Client class to interact with the API, and the library will take care of authenticating your requests using the API key and URL you provided.
Get Contact by Email Function
In this section, we will create a function called getContactByEmail to retrieve a contact’s information from ActiveCampaign using their email address.
Creating the function
- Function signature
The function signature for getContactByEmail will have two parameters: an instance of the Client class and an email address (string). The function will return an associative array containing the contact’s information if found or null if the contact is not found.
function getContactByEmail(Client $client, string $email)
2. Input validation
We must validate the provided email address inside the function to ensure it’s properly formatted before making the API request. We can use PHP’s built-in filter_var function with the FILTER_VALIDATE_EMAIL flag for this purpose:
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
throw new InvalidArgumentException('Invalid email address.');
}
Using the ActiveCampaign API’s ‘contact_list’ endpoint
- Preparing the request parameters
We will use the ‘contact_list’ endpoint of the ActiveCampaign API to search for a contact by email. We need to prepare the request parameters by setting the ‘search’ parameter to the email address:
$requestParams = ['search' => $email];
2. Handling the API response
Next, we will call the contacts->list method of the Client class, passing the request parameters. The method will return the API response as an associative array:
$response = $client->contacts->list($requestParams);
Parsing the API response
- Checking for errors
The ActiveCampaign API returns an array containing the matching contacts. If there are no matching contacts, the array will be empty. We can check if the ‘contacts’ key exists in the response array and if it has at least one element:
if (isset($response['contacts'][0])) {
// Contact found
} else {
// Contact not found
return null;
}
2. Extracting contact information
If a contact is found, we can extract their information by returning the first contact in the ‘contacts’ array:
return $response['contacts'][0];
Get ActiveCampaign Contact by Email PHP code example
Here’s the complete getContactByEmail function:
function getContactByEmail(Client $client, string $email)
{
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
throw new InvalidArgumentException('Invalid email address.');
}
$response = $client->contacts->list(['search' => $email]);
if (isset($response['contacts'][0])) {
return $response['contacts'][0];
}
return null;
}
The above PHP function can be used to search for a contact in ActiveCampaign by their email address and retrieve their information as an associative array.
Testing the Function
To ensure the getContactByEmail function works correctly, testing it with a sample email address and handling the results is essential.
Sample usage of the function
To test the getContactByEmail function, call it with a valid email address that you want to search for in your ActiveCampaign contact list:
$email = "example@example.com";
$contact = getContactByEmail($client, $email);
Handling and displaying the results
After calling the getContactByEmail function, you need to handle and display the results accordingly. You can display the contact’s information if the function returns a contact. If the function returns null, it means the contact was not found, and you should display an appropriate message:
if ($contact) {
echo "Contact found:\n";
echo "ID: " . $contact['id'] . "\n";
echo "Email: " . $contact['email'] . "\n";
echo "First Name: " . $contact['first_name'] . "\n";
echo "Last Name: " . $contact['last_name'] . "\n";
} else {
echo "Contact not found.\n";
}
Common errors and troubleshooting
While testing the function, you might encounter errors or unexpected results. Some common issues and their solutions include:
- Invalid email address: Ensure the email address provided to the getContactByEmail function is valid. The function will throw an InvalidArgumentException if an invalid email address is provided.
- API errors: If the ActiveCampaign API returns an error (e.g., due to invalid API credentials or API rate limits), the Client class might throw an exception. To handle these errors, wrap the function call in a try block and catch any exceptions:
try {
$email = "example@example.com";
$contact = getContactByEmail($client, $email);
// Handle and display results as described in section B.
} catch (Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
}
3. Incorrect API key or URL: Double-check that you’ve entered the correct API key and API URL obtained from your ActiveCampaign account. Incorrect API credentials will result in authentication errors.
By following these steps, you can effectively test the getContactByEmail function and handle any errors or unexpected results that may arise.
How to Get ActiveCampaign Contact by Email through API
This article provides a comprehensive tutorial on using the ActiveCampaign API with PHP to retrieve contact information based on email addresses. The key steps include setting up the ActiveCampaign PHP Library, authenticating the API, creating a getContactByEmail function, and testing the function for accuracy and error management.
Following this tutorial, you can develop custom integrations, automated workflows, and tools for efficient ActiveCampaign contact management while deepening your understanding of the API and PHP through additional resources and documentation.
Further resources and documentation
To learn more about the ActiveCampaign API and PHP, consider exploring the following resources:
- ActiveCampaign API Documentation: https://developers.activecampaign.com/reference
- ActiveCampaign API PHP Client GitHub Repository: https://github.com/activecampaign/api-php
- PHP Manual: https://www.php.net/manual/en/index.php
- Composer Documentation: https://getcomposer.org/doc/
These resources can help you dive deeper into the ActiveCampaign API, understand its capabilities, and discover additional features and endpoints that can be used to enhance your applications and workflows.
Our ActiveCampaign Article Series
This is one of many articles in our series on ActiveCampaign. Feel free to check out the entire series below to dig deeper into ActiveCampaign.
- What is the ActiveCampaign Email Marketing Service
- Using Lists in ActiveCampaign
- ActiveCampaign Tags vs Lists
- ActiveCampaign Custom Fields vs Tags
- Integrate ActiveCampaign with Laravel
- 3 Ways to Bulk Import ActiveCampaign Contacts
- Get Contacts by Email API Example