Guides & TutorialsTroubleshooting AdvancedTroubleshooting & Debugging API

Troubleshooting & Debugging API

While we have comprehensive API reference guides with examples for each API, you may also need to verify whether your API code is working correctly or not. You can use several methods to troubleshoot and debug this.

API Sample Code

You can verify your code using the sample code in our documentation (for example, GetUsers API). This will allow you to check whether it can run without any modifications.

Example Request (Local API)

$command = 'GetUsers';
$postData = array(
    'search' => '[email protected]',
    'responsetype' => 'json',
);
$adminUsername = 'ADMIN_USERNAME'; // Optional for WHMCS 7.2 and later

$results = localAPI($command, $postData, $adminUsername);
print_r($results);

Example Response JSON

{
    "result": "success",
    "totalresults": 1,
    "startnumber": 0,
    "numreturned": 1,
    "users": [
        {
            "id": 1,
            "firstname": "John",
            "lastname": "Smith",
            "email": "[email protected]",
            "datecreated": "2020-12-15 15:29:49",
            "validationdata": "",
            "clients": [
                {
                    "id": 1,
                    "isOwner": true
                }
            ]
        }
    ]
}

If the sample code runs successfully and the results match the sample response, but your custom code does not, then the issue will most likely be in your own custom code.

However, if the sample code does not run, then the issue may not be with your custom code and will require general troubleshooting.

You can also use one of the API code samples to begin writing your custom code. Once you modify the code, you will be able to track your changes and verify each addition. If the results begin to include errors, you can easily identify the line of code that is causing problems.

API Logging (Advanced)

If you want to debug API send or receive data calls at a transactional level, you can enable API debugging in the configuration.php file simply by adding the following line:

$api_enable_logging = true;

The data will then be logged in the tblapilog table of your WHMCS database.

For WHMCS v7.2 and earlier, create an apilog.txt file in the includes directory and set the permissions to 777.  to ensure that the log file is populated.

After you finish troubleshooting, you must remove the line from your configuration.php file and delete the log file.