REST credentials

The Spotler MailPro REST API makes it possible to directly connect external systems with MailPro. This allows you to synchronize contacts, create and send mailings, add personalized content and include attachments. 

Creating REST Credentials

To work with the Spotler MailPro REST API, you first need to create REST credentials. This is a combination of a Client ID and Client Secret. These can be created by a user with access rights to the REST credentials section. This is how you create the REST credentials:

  1. Go to Admin REST credentials.
  2. Click Insert. The following screen will open:

  3. Choose a name that makes it easy to recognize what the credentials are for.
  4. Choose the Redirect URL. In most cases, this is not needed. You can enter https://localhost/client. When using an external application such as Zapier, you can enter that application's URL here.
  5. In most cases, the settings under Client Grants can remain unchanged.
  6. Click Ok at the bottom of the page.

The credentials have now been created and are ready for use in your codebase. You can also use them to test REST API calls in our Swagger.

Note: a user account with which the REST credentials were created cannot be deleted as long as the credentials exist.

Authorization & obtaining a token

With your REST credentials, you can make a POST request to receive a token. This token allows you to remain authorized for one hour. After that, you must request a new token.

Click here for a code snippet

This example performs a simple GET campaign call.

<?php

// Config settings

$clientId = '*********';
$clientSecret = '*********';
$licenseUrl = '<licenseUrl>.webpower.eu';
$cacheFile = __DIR__ . '/webpower-rest-access-token.cache';

// Haal access token op

$accessToken = null;

// Read access token from cache
if (file_exists($cacheFile)) {
$data = json_decode(file_get_contents($cacheFile), true);
if (isset($data['accessToken'], $data['expireTime']) &&
$data['expireTime'] > (time() + 30)
) {
$accessToken = $data['accessToken'];
}
}

// No access token or the cached access token expired
if (null === $accessToken) {
$params = http_build_query(
[
'grant_type' => 'client_credentials',
'client_id' => $clientId,
'client_secret' => $clientSecret,
'scope' => 'rest',
],
'',
'&'
);

$headers = [
'content-type: application/x-www-form-urlencoded',
];

$curl = curl_init('https://' . $licenseUrl . '/admin/oauth2/token.php');
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

$content = curl_exec($curl);
$error = curl_error($curl);
curl_close($curl);

$data = json_decode($content, true);
if (isset($data['access_token'])) {
$accessToken = $data['access_token'];
# Store in cache
$cache = json_encode([
'accessToken' => $data['access_token'],
'expireTime' => time() + $data['expires_in'],
]);

file_put_contents($cacheFile, $cache);
} else {
print 'Authentication error: ' . $data['error_description'] . "\n";
exit(1);
}
}

// Voer REST call uit met access token

$headers = [
'Authorization: Bearer ' . $accessToken,
];

$curl = curl_init('https://' . $licenseUrl . '/admin/api/index.php/rest/campaign');
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$content = curl_exec($curl);
$error = curl_error($curl);
curl_close($curl);

var_dump(json_decode($content, true));
?>

See the examples below for a command line curl instead of PHP curl:

Retrieve token:

curl -X POST -d "grant_type=client_credentials" -d "client_id=<client_id>" -d "client_secret=<client_secret>" -d "scope=rest" https://<licenseUrl>/admin/oauth2/token.php

To retrieve the token, the parameters must be included in the POST payload.

Perform REST call:

curl -H 'Authorization: Bearer <access_token>' https://<licenseUrl>/admin/api/index.php/rest/campaign

Documentation & testing via Swagger

You can find the Swagger documentation for the Spotler MailPro REST API at:

https://[license].webpower.eu/admin/api/swagger

[license] must be replaced with the name of your Spotler MailPro license. This is displayed in the URL while you are working in Spotler MailPro.

Testing via Swagger

To easily test whether your login details are correct and whether the API calls do what you expect, you can authorize yourself via Swagger and execute the calls. Note: This may affect your production environment. We recommend testing in a separate campaign or an acceptance environment.

To get started testing via Swagger:

  1. Click the Authorize button in the top-right corner.

    mceclip1.png

  2. Scroll up until you see clientCredentials.

  3. Enter your client_id and client_secret and click Authorize.
  4. Close the login window.

You can now try out the various calls by clicking the Try it out button.tmp.png

Frequently asked questions