# Client/External API Integration

Once the main script is up and running we can now integrate LicenseBox into your PHP application using the external/client helper file (which you can generate from your LicenseBox admin panel)

Let's first understand what exactly is the external/client helper file, in simple words it is a wrapper/helper file for LicenseBox External API which has all the requirements needed for your application to connect to your LicenseBox server (which you have installed in the last step) to activate, verify, deactivate user provided licenses and check/download any available updates (released by you from your LicenseBox admin panel).

Note: Helper files are currently only available for PHP but you can study the API endpoints and parameters to make a POST request from your non-PHP application as well.

### Getting Started

1. Log in to LicenseBox and add a new product from the menu.

2. Create a new API Key of type 'External' from the API Settings Page.

3. Go to Client Helper File generator from the Generate dropdown on the menu.

4. Select Product, API Key, API Language, Verification Period (if you want to automatically verify license at choosen interval) and choose if you want to allow Envato Purchase codes or not, then click Generate and copy the generated code.

5. Create a new folder in your php application root and save the generated code as whatevar name you want to use, we are going to use `lb_helper.php` and the folder name as `includes`. So when you are done with this step you should have an includes folder with the lb_helper.php file.

6. Some things you would want to take a look at in the generated helper file are $root_path and $license_file path, the root_path should be pointed to your application root. If your includes folder which has the lb_helper.php file is already in your application root then you don't have to make any changes.

7. We are now done with the lb_helper.php file, It should now be included in your application.

8. If you want to only use the included pre-built PHP application installer, updater, activator or deactivator. Simply copy the `/install`, `/update`, `/activate` or `/deactivate` folder respectively from the `licensebox_integration_samples` folder and add it in your application root and you are good to go. You don't need to do anything else (except from adding the background license check function in your main application entry file to do the actual license verification), you can still use all the available functions of lb_helper.php file in your application but you would need to require it at the top on every page where you wish to use it.

   Note: In the `licensebox_integration_samples` folder, you will also find a sample wordpress plugin (useful if you are integrating licensebox in your wordpress plugin/theme) in which you can add your generated wordpress client/external helper file code as `lb_helper.php` file in the `includes` folder and test it out. It has both the license and updates system integrated. You can easily port the license and updates system code from it into your wordpress plugin/theme or use it as a base.

9. Now, If you want to use all or any LicenseBox API helper functions in your complete application, you should go to your application entry file (index.php file) or (innit.php file) or any other file which all other files of your application require/include.

10. At the very top paste the following code:

        require_once 'includes/lb_helper.php';
        $api = new LicenseBoxExternalAPI();

    Note: lb_helper.php file on its own does nothing but it contains all the required function and you can call them however you want.

11. Now you can call any available LicenseBox Helper file function from anywhere in your application, to learn more about all the available functions and what they do with live examples, add your generated Client/External helper file code as `lb_helper.php` file in the `includes` folder present inside `licensebox_api_usage_examples` folder and view the `external_api_examples.php` file.

12. For example you can verify license using `verify_license()` function and exit your application if the license is invalid. `verify_license()` can be directly called without any parameters when a local `.lic` license file exist, this file is created on successful activation by the `activate_license()` function.

        $res = $api->verify_license();
        if($res['status']!=true){
          die('Your license is invalid, please contact support.');
        }

13. You can also verify licenses based on the choosen verification period.

        $res = $api->verify_license(true);
        if($res['status']!=true){
          die('Your license is invalid, please contact support.');
        }

14. Now lets see an example of checking for new application updates.

        $res = $api->check_update();
        if($res['status']){
          echo "New ".$res['version']." update available.";
        }

There are a lot more ways in which you can use LicenseBox Client/External API in your application, see the `external_api_examples.php` file inside `licensebox_api_usage_examples` folder to learn more. If you are facing issues getting the integration to work, enable the Debug mode in the helper file by changing the `LB_API_DEBUG` value to `true`.

15. Finally, when you are done the the integration you should obfuscate your application as you may know readable/editable PHP code is not protected (as the user can modify the code) so you should obfuscate both your script entry file and the lb_helper file (if not, your full application with license and updates system integrated), You can use the in-built PHP obfuscator for that or you can also use paid softwares like ioncube but there are a lot of free PHP obfuscation tools available out there too.

### External API Documentation and Usage Examples

#### External API & helper file example (external_api_examples.php)

You can check out the external_api_examples.php file present in the `licensebox_api_usage_examples` folder, it has all the LicenseBox external/client API functions documented with their usage and live examples. Add your generated client/external helper file as `lb_helper.php` file in the `includes` folder to test it out.

### Using Included Integration Samples

To test the included integration samples, first add your generated external helper file as `lb_helper.php` inside the `includes` folder present in the `licensebox_integration_samples` folder

Note: As the name suggest these are only integration samples and may require additional changes to be used in a production environment.

#### 1. Activator (/activate folder)

This script creates a local `.lic` license file based on the provided valid license code and client name, once the user has activated the script or the `.lic` license file is created you can check the license using the `verify_license()` function in your application entry file as explained above.

#### 2. Installer (/install folder)

You can use a pre-made script installer, if you don't have a installer in your application, just add your SQL schema dump in the `database.sql` file and enjoy the all new script installer for your application with a built-in license activation system.

#### 3. Updater (/update folder)

This script will act as an updater for your application, just make sure you have the $root_path in `lb_helper.php` pointing to your application root, you can also pass your database details to automatically import update `.sql` file.

Note: Whenever you release any new update for your application you should also include the updated `lb_helper.php` file in the main update (.zip) file with the $current_version value changed to the new version otherwise the updater won't recognize if the script has already been updated or not. LicenseBox will extract/replace all the update files in your application root so make sure you push updates with the correct file structure.

#### 4. Deactivator (/deactivate folder)

This script deactivates and removes the locally stored `.lic` license file and it marks it's activation as inactive on the LicenseBox server, so that the client can activate the same license on some other domain in the future. This is a must have feature if you are planning to have single use licenses.

#### 5. Wordpress Sample Plugin (/licensebox-wp-sample folder)

You can also checkout the included sample wordpress plugin to see how licensebox can be integrated with a wordpress plugin, add your Wordpress External Helper File code as `lb_helper.php` file in the `includes` folder and create a plugin .zip file to install on your wordpress site. This plugin simply displays a dummy text in all posts footer when the license is active. When any new updates are available it will also show relevant notifications in the WordPress plugins page. You can easily port the license and updates system from it to your wordpress plugin/theme or you can use this plugin as a base for your next project.

#### ❮ Previous (Overview) Next (Internal API Integration) ❯

Please mail us your feedback at support@licensebox.app
Follow us on twitter at @LicenseBoxApp for future product updates