Jump to content

Custom API Actions


maren

Recommended Posts

Hi,

I'm currently working on connecting WHMCS to some of our other support platforms, and I would like to create some custom API actions to help facilitate this.I've already tried adding additional action files to includes/api as mentioned accross the internet, however I receive an error message telling me "blank is not a valid API action" whenever I try to call them.

 

Link to comment
Share on other sites

Somehow managed to submit the post early and now I can't edit it 😕
To whoever is approving this, please merge the two posts


I've seen mentioned in another community topic here that such a method is unsupported, which fits since there's no official documentation on it. In that post, it was suggested to instead use an addon module with client/admin area pages. I don't mind doing, and in fact have already done so, but how would I set up API authentication to go with it? I'm currently using an admin area page, do I need to create a robot admin user and somehow authenticate myself, or can I somehow allow a set of API credentials to access the page? Failing all that, does anyone know of a solution for creating API actions that still works?

Cheers

Link to comment
Share on other sites

On 1/4/2022 at 9:26 AM, maren said:

Somehow managed to submit the post early and now I can't edit it 😕
To whoever is approving this, please merge the two posts


I've seen mentioned in another community topic here that such a method is unsupported, which fits since there's no official documentation on it. In that post, it was suggested to instead use an addon module with client/admin area pages. I don't mind doing, and in fact have already done so, but how would I set up API authentication to go with it? I'm currently using an admin area page, do I need to create a robot admin user and somehow authenticate myself, or can I somehow allow a set of API credentials to access the page? Failing all that, does anyone know of a solution for creating API actions that still works?

Cheers

Hello,

In order to help you out, could you please tell me a little bit more about what you wanna do? From what I've seen (by far), you wanna a custom API action, is that correct? If yes, check this example out! It's a piece of extra code from one of my modules.

<?php
/**
 *	WHMCS Link Discord version 1.2
 *
 *	@package     WHMCS
 *	@copyright   Andrezzz
 *	@link        https://www.andrezzz.pt
 *	@author      André Antunes <andreantunes@andrezzz.pt>
 */

use WHMCS\Database\Capsule;

if (!defined('WHMCS')) {
  exit(header('Location: https://www.andrezzz.pt'));
}

$clients = Capsule::table('tblhosting')->where('domainstatus', 'Active')->groupBy('userid')->get(['userid']);

if (!$clients) {
  $error = 'No client found.';
}

if (!$error) {
  $apiresults = array("result" => "success", "clients" => $clients);
} else {
  $apiresults = array("result" => "error", "message" => $error);
}
?>

Edit: You'll need to create a file in the directory `/yourwhmcspath/includes/api/nameofaction.php` and change the `nameofaction` for your desired name.

Edited by andrezzz
Link to comment
Share on other sites

@andrezzz I have already tried that

On 1/4/2022 at 8:21 PM, maren said:

I've already tried adding additional action files to includes/api as mentioned accross the internet, however I receive an error message telling me "blank is not a valid API action" whenever I try to call them.

 

That was actually the exact method I was using.

 

As for what I want to do, I'm currently using a custom addon module to add some custom sidebar widgets. The data contained in these widgets needs to be accessible from an external application, and I am trying to create an API route to query it.

Link to comment
Share on other sites

  • 2 years later...

Here's a summarized approach to achieve this:

  1. 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.

  2. 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.

  3. 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
            )));
        }
    
    }
  4. 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.
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated