JavaScript Projects

SunLicense Integration Guide for JavaScript Projects

Prerequisites

  • Node.js environment (for backend) or modern web browser (for frontend)

  • npm or yarn package manager

  • SunLicense API credentials

Integration Steps

1. Backend Implementation (Node.js)

const axios = require('axios');

class SunLicenseValidator {
    constructor(licenseKey, productId, version = '1.0.0') {
        this.licenseKey = licenseKey;
        this.productId = productId;
        this.version = version;
        this.apiUrl = 'YOUR_API_URL/api/v1/validate';
    }

    async validate() {
        try {
            const payload = {
                licenseKey: this.licenseKey,
                productId: this.productId,
                productVersion: this.version,
                hwid: this.getHWID(), // Optional
                operatingSystem: process.platform,
                operatingSystemVersion: process.version
            };

            const response = await axios.post(this.apiUrl, payload, {
                headers: { 'Content-Type': 'application/json' }
            });

            return response.status === 200;
        } catch (error) {
            throw new Error(`License validation failed: ${error.message}`);
        }
    }

    getHWID() {
        // Implement your HWID generation logic here
        return 'YOUR-HWID';
    }
}

// Usage example
const validator = new SunLicenseValidator('YOUR-LICENSE-KEY', YOUR_PRODUCT_ID);
validator.validate()
    .then(() => console.log('License valid!'))
    .catch(error => console.error(error));

2. Frontend Implementation (Browser)

Express.js Middleware

React Component

Best Practices

  1. Security

    • Never expose license key in frontend code

    • Implement server-side validation

    • Use secure communication (HTTPS)

  2. Error Handling

    • Implement proper error handling

    • Show user-friendly error messages

    • Log validation failures

  3. Performance

    • Cache validation results

    • Implement retry mechanisms

    • Handle offline scenarios

Common Issues and Solutions

  1. CORS Issues

    • Configure proper CORS headers

    • Use proxy for API requests

    • Handle preflight requests

  2. Network Problems

    • Implement timeout handling

    • Add retry logic

    • Cache validation results

Last updated