Here's a summarized approach to achieve this:
Custom API File Creation: Start by creating custom API actions by adding new PHP files in the includes/api/ directory of your WHMCS installation. Name these files according to the action you want to perform.
Register Custom API Actions: To make sure these custom API actions are registered and can be called, you need to create a custom hook that registers these actions with WHMCS. This is necessary because WHMCS doesn’t automatically register actions just by placing them in the includes/api/ directory.
Example of a Working Solution:
<?php
add_hook('AdminAreaPage', 1, function ($vars) {
apiroles_checkGroupOrAdd();
apiroles_checkApiOrAdd();
});
function apiroles_checkGroupOrAdd()
{
$apiCatalogGroups = \WHMCS\Api\V1\Catalog::get()->getGroups();
if (!array_key_exists('GROUP_NAME', $apiCatalogGroups))
\WHMCS\Api\V1\Catalog::add([], ['GROUP_NAME' => array('name' => 'Custom Group')]);
}
function apiroles_checkApiOrAdd()
{
$apiCatalogActions = \WHMCS\Api\V1\Catalog::get()->getActions();
if (!array_key_exists('customapifunction', $apiCatalogActions)) {
\WHMCS\Api\V1\Catalog::add(array('customapifunction' => array(
'group' => 'GROUP_NAME',
'name' => 'CustomApiName',
'default' => 0
)));
}
}
Explanation of the Code:
The add_hook function is used to register a custom hook in WHMCS, which triggers on the AdminAreaPage event.
apiroles_checkGroupOrAdd function checks if a specific API group exists; if not, it creates one.
apiroles_checkApiOrAdd function registers new API actions (customapifunction) under the created group.