Creating, Testing, and Scheduling Campaigns via the API

The ActiveCampaign API provides developers the ability to access functionality and data associated with their ActiveCampaign accounts, allowing possibilities like custom interfaces or white-labeled email marketing features.
One common scenario that resellers come across is being able to provide the ability to create, test, and schedule email campaigns – all via the API.
A typical flow might go something like this:

  1. Create a campaign.
  2. Preview/test the campaign.
  3. Schedule the campaign to be sent.

All of this can be done using our API, and here we’ll show you how.
We’ll be demonstrating with our PHP API wrapper to keep the code examples concise. First set up your account URL and key:

define("ACTIVECAMPAIGN_API_KEY", "fca40895042f...2802d85838cbc8a8b");

Let’s create the campaign. First we’ll need to add our email message:

$post_data = get_object_vars(json_decode('{
  "format": "mime",
  "subject": "TEST from API",
  "fromemail": "",
  "fromname": "Test Company",
  "htmlconstructor": "editor",
  "html": "<h1>My Test Message</h1>\n\n<p>This is a test message created via the API.</p>",
  "textconstructor": "editor",
  "text": "# My Test Message\n\nThis is a test message created via the API.",
  "p[25]": "25",
  "p[12]": "12"
$message = $ac->api("message/add", $post_data);

The result should look something like this:

stdClass Object
  [id] => 168
  [subject] => TEST from API
  [result_code] => 1
  [result_message] => Email Message added
  [result_output] => json
  [success] => 1

We’ll use the message ID from above (168) when creating the campaign:

$post_data = get_object_vars(json_decode('{
  "type": "single",
  "segmentid": "0",
  "name": "TEST from API",
  "status": "0",
  "public": "1",
  "tracklinks": "all",
  "trackreads": "1",
  "p[16]": "16",
  "m[' . $message->id . ']": "100"
$campaign = $ac->api("campaign/create", $post_data);

You’ll notice the value for “status” is 0, which means “draft.”
The API result will look like this:

stdClass Object
  [id] => 130
  [result_code] => 1
  [result_message] => Campaign saved
  [result_output] => json
  [success] => 1

We’ll need the campaign ID from above ($campaign->id) later.
If you check the admin interface you’ll see the campaign draft:
wzeoxzzgp screenshot2013 07 20at10.41.12am
With the campaign in “draft” status we can now test the campaign to any email address (if the email address is an existing contact, they’ll see personalizations as well):

$test_email = $ac->api("campaign/send?" . $campaign->id .

Once you’re ready to schedule the campaign, update the scheduled date (“sdate”) to the appropriate date/time:

$schedule = $ac->api("campaign/status?id=" . $campaign->id . "&status=1&sdate=" .
 urlencode("2013-07-25 12:00:00"));

Make sure the “sdate” is a date in the future otherwise it will send right away.
If you need to UN-schedule the campaign in order to send more email tests, just update the status back to “draft.”

$schedule = $ac->api("campaign/status?id=" . $campaign->id . "&status=0";

Happy coding! Let us know if we can help with anything.

Never miss an update