init static method

Future<void> init({
  1. required String apiKey,
  2. String? baseUrl,
  3. bool strict = false,
})

Initialize the SDK with the purchased API key.

When strict is true, the SDK will throw an IntyxLicenseException if the license cannot be validated (no offline fallback). Use this in production builds where you want to enforce valid licenses.

await IntyxDynamicWidget.init(apiKey: 'intyx_pro_abc123...');

Implementation

static Future<void> init({
  required String apiKey,
  String? baseUrl,
  bool strict = false,
}) async {
  _apiKey = apiKey;
  _strictMode = strict;
  if (baseUrl != null) _baseUrl = baseUrl;

  try {
    final response = await http.post(
      Uri.parse('$_baseUrl/api/licenses/validate'),
      headers: {'Content-Type': 'application/json'},
      body: json.encode({'api_key': apiKey}),
    );

    if (response.statusCode == 200) {
      final data = json.decode(response.body) as Map<String, dynamic>;
      if (data['valid'] == true) {
        _plan = data['plan'] as String?;
        _widgetLimit = data['widget_limit'] as int? ?? 0;
        _initialized = true;
        return;
      }
    }

    // Validation failed
    if (strict) {
      throw const IntyxLicenseException(
        'License validation failed. Check your API key.',
      );
    }

    // Allow offline/demo usage
    _initialized = true;
    _plan = 'offline';
    _widgetLimit = 3;
  } catch (e) {
    if (e is IntyxLicenseException) rethrow;

    // Network error
    if (strict) {
      throw IntyxLicenseException(
        'Could not reach license server: $e',
      );
    }

    // Still allow the app to run with limited features
    _initialized = true;
    _plan = 'offline';
    _widgetLimit = 3;
  }
}