How do I export contact data using the API?

There may be times when you need to export your contact data from outside of our application. Maybe you want to backup your data, or you need to analyze every contact in your account.

You can quickly export all contacts to a CSV file which can be easily imported to a spreadsheet program, or read through a programming language.

You can also create a webhook that will automatically send data to your application whenever a new contact is added or updated.

Despite these two features, there still might be times when your application needs to access the data on-demand. We’ll show you how to do this using PHP.

First, set up your API connection from your script:

define("ACTIVECAMPAIGN_API_KEY", "0c3b245df4785...005d2d3d7e1e386f");

Next, we’ll create a function that handles the API request and adds each contact to a global array. The function accepts a page number, as well as optional list ID’s (if you want to request contacts only from certain lists), and returns the total number of contacts found for the page requested.

$GLOBALS["contacts"] = array(); // Holds all contacts.

function run($page, $listids = 0) {

  // API method.
  $request = "contact/list";

  // Check for list filter. If none, request all contacts.
  if ($listids)
    $request .= "?filters[listid]={$listids}";
    $request .= "?ids=all";

  // Make request.
  $request .= "&full=0&sort=id&sort_direction=ASC&per_page=500&page={$page}";
  $response = $GLOBALS["ac"]->api($request);

  // Internal counter.
  $count = 0;

  // If request comes back successfully.
  if ((int)$response->success) {

    // Loop through each item.
    foreach ($response as $key => $value) {

      // If there is an email item, we know it's a contact.
      if (isset($value->email)) {

        $count++; // Increment the internal counter.

        // Store this contact in the global array.
        if (!isset($GLOBALS["contacts"][$value->id])) {
          $GLOBALS["contacts"][$value->id] = $value;



  else {

    // Request failed. Output the reason.
    echo $response->error;


  return $count;


As you can see above (highlighted), we add each contact to a global array ($contacts). Here is a sample contact record:

stdClass Object
  [id] => 29564
  [subscriberid] => 29564
  [cdate] => 2012-12-14 09:01:00
  [sdate] => 2012-12-14 09:01:00
  [first_name] => Mike
  [last_name] => Smith
  [email] =>
  [last_list] => Rewards List
  [avatar_url] =>

You’ll also see above that we limit the results to 100 per page. This is the maximum amount you can receive for each page. To request subsequent pages, we’ll add some more code:

$page_start = 1; // The page to start from.
$page_end = 1; // The page to end on.
$page_loop = 0;
$listids = 9; // Any list filters (use comma-separated format for more than one: 1,2)

while (($page_start + $page_loop) <= $page_end) {
  $page_current = $page_start + $page_loop;
  $count = run($page_current, $listids);

The loop above will automatically fetch the next page of contacts until it reaches the last page that you specify. You can easily find your last page by dividing the number of total contacts in your account with 100. For example, if you have ~1,000 contacts - you would need about 10 pages to fetch them all.

$page_start = 1; // The page to start from.
$page_end = 10; // The page to end on.

You could also change the loop to look only at the total number returned ($count):

while ($count == 100) ...

This way you are only fetching the next page if the current page has the maximum amount returned. If the current page has less than the maximum amount, you know it's the last page. We have found this approach a little less reliable and less flexible than our initial example, but it's up to you how you want to handle it.

Was this article helpful?

Have more questions? Submit a request

Start free trial