

Using the Adaptix API Library
Requirements
- PHP 7.2 or newer
- cURL support
Installing the API Library
You can install the API Library with the following command:
composer require adaptix/api-library:dev-main
Also install,
composer require psr/log:^1.1
Adaptix Setup
The API must be enabled in Adaptix. Within Adaptix, go to the Configuration page (located in the Settings menu) and under API Settings enable
Adaptix's API. If you intend to use Basic Authentication, ensure you enable it. You can also choose which OAuth protocol to use here. After saving the configuration, go to the API Credentials page (located in the Settings menu) and create a new client. Enter the callback/redirect URI that the request will be sent from. Click Apply, then copy the Client ID and Client Secret to the application that will be using the API.
Authorization
Obtaining an access token
The first step is to obtain authorization. Adaptix supports OAuth 1.0a and OAuth 2, however it is up to the administrator
to decide which is enabled. Thus it is best to have a configuration option within your project for the administrator
to choose what method should be used by your code.
<?php
include __DIR__ . '/vendor/autoload.php';
use Adaptix\Auth\ApiAuth;
session_start();
$publicKey = '';
$secretKey = '';
$callback = '';
$settings = [
'baseUrl' => '',
'version' => 'OAuth2',
'clientKey' => '',
'clientSecret' => '',
'callback' => '',
];
$initAuth = new ApiAuth();
$auth = $initAuth->newAuth($settings);
try {
if ($auth->validateAccessToken()) {
if ($auth->accessTokenUpdated()) {
$accessTokenData = $auth->getAccessTokenData();
}
}
} catch (Exception $e) {
}
Using Basic Authentication Instead
Instead of messing around with OAuth, you may simply elect to use BasicAuth instead.
Here is the BasicAuth version of the code above.
<?php
include __DIR__ . '/vendor/autoload.php';
use Adaptix\Auth\ApiAuth;
session_start();
$settings = [
'userName' => '',
'password' => '',
];
$initAuth = new ApiAuth();
$auth = $initAuth->newAuth($settings, 'BasicAuth');
Note: If the credentials are incorrect an error response will be returned.
[
'errors' => [
[
'code' => 403,
'message' => 'access_denied: OAuth2 authentication required',
'type' => 'access_denied',
],
],
];
Note: You can also specify a CURLOPT_TIMEOUT in the request (default is set to wait indefinitely):
$initAuth = new ApiAuth();
$auth = $initAuth->newAuth($settings, 'BasicAuth');
$timeout = 10;
$auth->setCurlTimeout($timeout);
API Requests
Now that you have an access token and the auth object, you can make API requests. The API is broken down into contexts.
Get a context object
<?php
use Adaptix\AdaptixApi;
$api = new AdaptixApi();
$contactApi = $api->newApi('contacts', $auth, $apiUrl);
Supported contexts are currently:
See the developer documentation.
Retrieving items
All of the above contexts support the following functions for retrieving items:
<?php
$response = $contactApi->get($id);
$contact = $response[$contactApi->itemName()];
$response = $contactApi->getList($filter, $start, $limit, $orderBy, $orderByDir);
$totalContacts = $response['total'];
$contact = $response[$contactApi->listName()];
Creating an item
<?php
$fields = $contactApi->getFieldList();
$data = array();
foreach ($fields as $field) {
$data[$field['alias']] = $_POST[$field['alias']];
}
$data['ipAddress'] = $ipAddress;
$response = $contactApi->create($data);
$contact = $response[$contactApi->itemName()];
Editing an item
<?php
$updatedData = [
'firstname' => 'Updated Name'
];
$response = $contactApi->edit($contactId, $updatedData);
$contact = $response[$contactApi->itemName()];
$response = $contactApi->edit($contactId, $updatedData, true);
$contact = $response[$contactApi->itemName()];
Deleting an item
<?php
$response = $contactApi->delete($contactId);
$contact = $response[$contactApi->itemName()];
Error handling
<?php
$response = $contactApi->delete($contactId);
if (isset($response['errors'])) {
foreach ($response['errors'] as $error) {
echo $error['code'] . ": " . $error['message'];
}
}