ActiveCampaign agency partners have a powerful tool at their disposal: our Agency API allows you to create your own interface for managing your agency accounts. Combined with our standard API, this allows you to extend your own products and services to take advantage of agency-specific functionality.
This article will provide a brief tutorial on how to use our API to establish a simple interface for managing your agency accounts, which you can add to your own website or application.
We are going to demonstrate a step-by-step form that allows you to add new accounts, add a template for each account, and provide a link that logs-in the user automatically.
Requirements
- Valid ActiveCampaign agency account with a billing profile set up. (Sign up today!)
- A web server where you can run PHP code.
- Our ActiveCampaign PHP wrapper added to your application environment.
- Our custom script is available to download.
Custom Domain
This tutorial assumes you have your own custom domain that you use for each account. For example, let’s say your custom domain is:
yourdomain.com
For each account created, it will be assumed to map to a sub-domain of the above. For example:
yourclient1.yourdomain.com
yourclient2.yourdomain.com
Getting Started
We are providing the entire script that you can use on your own site. Feel free to download it and compare with it as we walk through things below.
Start by defining your custom domain, ActiveCampaign API URL, and API Key towards the top of the script:
<?php $your_domain = "yourdomain.com"; $reseller_api_url = "https://www.activecampaign.com"; $reseller_api_key = ""; ?>
Agency partners can find their API URL and key in the partner administration interface:
Our HTML form (for the first step) will look like this:
When submitted, we first check if the account name exists, and if not, add the account:
$account_name = $_POST["account_name"]; // check if account name already exists $name_exists = $ac->api("account/name_check?account={$account_name}"); if (!(int)$name_exists->success) { $alert = $name_exists->error; } else { // name is available. proceed with adding account. // add account $account = get_object_vars(json_decode('{ "account": "' . $account_name . '", "cname": "' . $account_name . '.' . $your_domain . '", "name": "' . $client_name . '", "email": "' . $client_email . '", "notification": "' . $client_email . '", "plan": "' . $plan . '" }')); $account = $ac->api("account/add", $account); }
If adding the account is successful, it will inherit your default branding settings that you have set up. For example:
Next, we’ll need to pause the script for a minute or two (sometimes it can take a few minutes to fully create an account in the system).
sleep(120);
Then we can obtain that account’s API Key (so we can add data to the account on their behalf).
// obtain this account's API URL and key $ac2 = new ActiveCampaign(ACTIVECAMPAIGN_URL, null, "admin", $account->password); $user_me = $ac2->api("user/me"); $_SESSION["account_api_url"] = $user_me->apiurl; $_SESSION["account_api_key"] = $user_me->apikey;
Next we’ll add an email template to the system (so it is ready for your client to use immediately):
Here is the code to add the template:
// add template $template = array( "name" => $name, "html" => htmlspecialchars($html), "template_scope" => "all", ); $template = $ac->api("message/template_add", $template);
What we’ll also do is generate a log-in link that you can send to the user, which will log-in them in automatically. You can also share the username and password with them.
// get SSO link $username = $_SESSION["username"]; $ip = $_SERVER["REMOTE_ADDR"]; $sso = $ac->api("auth/singlesignon?sso_user={$username}&sso_addr={$ip}&sso_duration=20"); $sso_url = $_SESSION["account_name"] . ".{$your_domain}/admin/main.php?_ssot={$sso->token}";
The user can log-in here with their username and password:
http://yourclient1.yourdomain.com/admin/
Or, using the automatic log-in link above will look something like this:
http://yourclient1.yourdomain.com/admin/main.php?_ssot=e09d6dc6df3bf91fe2b2f663cc8d0b2a
We also provide a sample email message that you could send to your user, informing them that their account was created, and containing useful links:
Again, grab our custom script and let us know if we can improve it, clarify anything, or help get this set up for you!