• Online Demo
  • 30-day Free Trial

Contents

API Actions


Overview


The Blesta API system allows local and remote access to the Blesta backend system. API calls are made through POST requests. A POST request consists of a single field-value pair, serialized and base64 encoded.

Request Format

API calls are made to the API controller located at http://yourdomain.com/blesta_install_dir/api.php.

The basic functionality is as follows:

  1. Serialize and base64 encode POST data
  2. Perform POST request to the Blesa API
  3. Base64 decode and unserialize the response

All responses are returned to the caller in the same format they were sent, or using PHP's serialize format if the received format was not supported.

Access Settings

API access settings can be configured from [Settings] > [API/Cron Settings] > [API Control Options]. All API requests require your Remote Access Key, found in [Settings] > [API/Cron Settings] > [API Control Options].

PHP serialization


When performing an API call from PHP it is preferred to use the PHP serialize function, as shown below:

Example using PHP serialize()

$post_data['base64'] = base64_encode(serialize($_POST));


Multi-platform serialization


As of version 2.0, Blesta's API also supports the JSON serialization method making it possible to perform API requests from any platform. Below is an example using PHP:

Example using JSON with PHP (requires PHP 5.2.0 or greater)

$post_data['json'] = base64_encode(json_encode($_POST));


Examples


The examples below demonstrate the Get Client request using the local access method as well as the remote access method, respectively.

Local Access

This method is preferred when the caller exists on the same server as Blesta.

  1. <?php
  2. include("/path/to/blesta/inc/init.php");
  3.  
  4. $api = new API();
  5.  
  6. $data = array();
  7. $data['key'] = "abcdefg1234567"; // Remote Access Key
  8. $data['getclient'] = "true"; // Action to perform
  9. $data['userid'] = "1514"; // Client ID (required for this action)
  10. $postdata = array();
  11. $postdata['base64'] = base64_encode(serialize($data));
  12.  
  13. $response = unserialize(base64_decode($api->processRequest($postdata['base64'], "base64"));
  14.  
  15. // NOTE: if using JSON the use the below line instead:
  16. //$response = unserialize(json_decode($api->processRequest($postdata['json'], "json"));
  17. ?>

Remote Access

This method is required whenever the caller does not reside on the same server as Blesta (i.e. creating an order form on a server other than the one on which Blesta is installed).

  1. <?php
  2. // The full URL to your Blesta installation and api.php file.
  3. // Use HTTPS whenever possible.
  4. $blesta_url = "https://yourdomain.com/api.php";
  5.  
  6. $data = array();
  7. $data['key'] = "abcdefg1234567"; // Remote Access Key
  8. $data['getclient'] = "true"; // Action to perform
  9. $data['userid'] = "1514"; // Client ID (required for this action)
  10.  
  11. $postdata = array();
  12. $postdata['base64'] = base64_encode(serialize($data));
  13.  
  14. $ch = curl_init();
  15. curl_setopt($ch, CURLOPT_URL, $blesta_url);
  16. curl_setopt($ch, CURLOPT_HEADER, 0);
  17. curl_setopt($ch, CURLOPT_POST, 1);
  18. curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
  19. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  20. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
  21. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
  22.  
  23. $response = curl_e xec($ch);
  24. curl_close($ch);
  25.  
  26. $response = unserialize(base64_decode($response));
  27.  
  28. ?>