Skip to main content

Plugin SDK

The main SDK class providing access to system capabilities.

Class: PluginSDK

class PluginSDK {
constructor(context: PluginContext)
}

The PluginSDK class is the primary interface for interacting with the MuluTime platform from within your plugin.

Properties

logger

readonly logger: PluginLogger

Structured logging interface for debugging and monitoring.

Example:

sdk.logger.info('Plugin initialized');
sdk.logger.error('Failed to process booking', error);

storage

readonly storage: PluginStorage

Persistent key-value storage for plugin data.

Example:

await sdk.storage.set('user-preferences', { theme: 'dark' });
const prefs = await sdk.storage.get('user-preferences');

api

readonly api: IPluginAPIClient

MuluTime API client for platform integration.

Example:

const bookings = await sdk.api.get('/bookings');

config

readonly config: Record<string, any>

Plugin configuration values set by the user.

permissions

readonly permissions: PluginPermission[]

Array of permissions granted to the plugin.

http

readonly http: AxiosInstance

HTTP client for external API calls.

Methods

getContext()

Returns the current plugin context.

getContext(): PluginContext

Returns: The current PluginContext object.

Example:

const context = sdk.getContext();
console.log(context.pluginId);

getUserId()

Gets the current user ID if available.

getUserId(): string | undefined

Returns: User ID or undefined if not available.

Example:

const userId = sdk.getUserId();
if (userId) {
// User-specific logic
}

getOrganizationId()

Gets the current organization ID if available.

getOrganizationId(): string | undefined

Returns: Organization ID or undefined if not available.

hasPermission()

Checks if the plugin has a specific permission.

hasPermission(permission: PluginPermission): boolean

Parameters:

  • permission - The permission to check

Returns: true if permission is granted, false otherwise.

Example:

if (sdk.hasPermission(PluginPermission.READ_USER_DATA)) {
// Safe to read user data
}

requirePermission()

Requires a permission, throwing an error if not granted.

requirePermission(permission: PluginPermission): void

Parameters:

  • permission - The required permission

Throws: Error if permission is not granted.

Example:

// Throws error if permission not granted
sdk.requirePermission(PluginPermission.EXTERNAL_API_ACCESS);
await sdk.http.get('https://api.external.com/data');

validateSchema()

Validates data against a JSON schema.

validateSchema(data: any, schema: object): ValidationResult

Parameters:

  • data - The data to validate
  • schema - JSON schema object

Returns: ValidationResult with validation outcome.

Example:

const result = sdk.validateSchema(userData, {
type: 'object',
properties: {
email: { type: 'string', format: 'email' },
age: { type: 'number', minimum: 0 }
},
required: ['email']
});

if (!result.valid) {
console.log('Validation errors:', result.errors);
}

Action System Integration

The SDK automatically integrates with the Action System for event handling, scheduling, and API routing.

Accessing Action Registry

const actionSystem = sdk.getActionSystem();

Event Emission

// Emit custom events
await sdk.emitEvent('custom.event', { data: 'value' });

HTTP Utilities

// Make external API calls with automatic retry
const response = await sdk.http.get('https://api.example.com/data', {
timeout: 5000,
retry: 3
});

Error Handling

The SDK provides structured error handling:

try {
await sdk.api.post('/bookings', bookingData);
} catch (error) {
if (error.code === 'PERMISSION_DENIED') {
sdk.logger.warn('Insufficient permissions for booking creation');
} else {
sdk.logger.error('Failed to create booking', error);
}
}

Best Practices

  1. Always check permissions before accessing protected resources
  2. Use structured logging for better debugging and monitoring
  3. Validate configuration in your plugin's onInstall method
  4. Handle errors gracefully and provide meaningful error messages
  5. Use async/await for better error handling and code readability