Our API sends responses in JSON headers. You can easily use these response in your app to activate/deactivate functions.
Here’s an example scenario:
- Your app integrates a form to collect license keys.
- Your customer pastes in their key and hits the submit button.
- The form sends a request to our API, which responds with the following based on the results:
- If the key is valid the response will look like: {“valid” : “1”}
- If the key is invalid the response will look like: {“valid” : “0”}
- If the process produces an error the response will look like: {“ERROR”:”Error message will be here”}
See the documentation on our JSON responses, including examples. We also have documentation on error messages.
Using JSON Responses In Your Code #
After the API returns a message, you could craft your code to do the following:
- Look for “valid” and see if it set to “1”
- If found, save a variable $activated to “true” to a local file or database. This represents the confirmation the key was activated.
- If not found or “valid” is “0”, display the returned error message or a custom message
Here is an example in PHP of how to look for “valid” in the response data and what to do if it is “1” or “0”. You can find more information in our documentation on JSON responses.
... rest of code
// Assume $licenseKey is defined earlier in the code
// Initialize variables
$activated = false;
$message = '';
// Process the response
$responseData = json_decode($response, true);
// Check if the API call was successful based on the valid field
if (isset($responseData['valid']) && $responseData['valid'] == "1") {
// Set activated to true and prepare a success message
$activated = true;
$message = 'Activation successful.';
// Save $licenseKey and activated = true to a local file
$fileContent = "Key = $licenseKey\nactivated = true";
file_put_contents('activation_status.txt', $fileContent);
} else {
// Set activated to false and prepare a failure message
$activated = false;
$message = 'Activation failed.';
}
// Return the results
return [
'activated' => $activated,
'message' => $message,
];
- Next, add a simple if statement to the critical functions of your app to check the status inside the local file.
- If $activated is “true”, then run normally
- If not, show an error message such as “Please activate your license”, or it could redirect to the license key form
Here is an example in PHP of how to lock your app down until the key is successfully validated.
// Function to check activation status
function checkActivationStatus() {
$activationFile = 'activation_status.txt';
if (file_exists($activationFile)) {
$contents = file_get_contents($activationFile);
if (strpos($contents, 'activated = true') !== false) {
return true;
}
}
return false;
}
// Example usage in a critical function
function criticalFunction() {
if (checkActivationStatus()) {
// Run the function normally
echo "Function is running normally.";
... rest of code
} else {
// Show an error message or redirect to the license key form
echo "Please activate your license.";
exit; // Stop further execution if license is not activated
}
}
// Call the critical function
criticalFunction();
Making It Even Better #
From there you also have the possibility to check the key every time the app starts or once a day in the case of web apps. We recommend adding this check to make refunds easier. See our documentation on refund best practices.
You can also provide offline key support by checking if the user has an internet connection when the app starts and, if true, check the key using our API. If there is no internet it can skip the live API check and use the previously saved results stored locally in a file or database. This is the best of both worlds.
Handling Errors #
Error messages are also sent via the JSON header and can be displayed to the end user. However if you wish to show custom messages you can.
See our full documentation on handling error messages.