SDK Reference
Complete API documentation for the @billai/sdk package.
Installation
npm install @billai/sdk
# or
yarn add @billai/sdk
# or
pnpm add @billai/sdkAccessControl
The main class for checking user access to features. Create one instance and reuse it throughout your application.
Constructor
new AccessControl(config: AccessControlConfig)AccessControlConfig
apiKeystringappIdstringbaseUrl?stringtimeout?numbermaxRetries?numberdebug?booleanimport { AccessControl } from '@billai/sdk';
const access = new AccessControl({
apiKey: process.env.BILLAI_API_KEY!,
appId: process.env.BILLAI_APP_ID!,
timeout: 3000,
maxRetries: 2,
debug: process.env.NODE_ENV === 'development',
});check()
Check if a user has access to a specific feature. Optionally increment usage for usage-based features.
async check(
userId: string,
feature: string,
options?: CheckOptions
): Promise<CheckResult>Parameters
userIdstringfeaturestringoptions?CheckOptionsCheckOptions
increment?numbermetadata?Record<string, unknown>CheckResult
grantedbooleanupgradeUrl?stringreason?stringusage?numberlimit?numberExamples
Boolean feature check
const result = await access.check('user_123', 'premium_export');
if (result.granted) {
// User has access
await performExport();
} else {
// Show upgrade prompt
console.log('Upgrade at:', result.upgradeUrl);
}Usage-based feature with increment
const result = await access.check('user_123', 'api_calls', {
increment: 1,
});
console.log(`API calls: ${result.usage}/${result.limit}`);
if (!result.granted) {
throw new Error('API limit exceeded');
}Check without incrementing
// Just check remaining usage without counting
const result = await access.check('user_123', 'api_calls');
console.log(`Remaining: ${(result.limit || 0) - (result.usage || 0)}`);Error Handling
The SDK exports several error classes for handling different error scenarios.
BillAIErrorBase error classBase class for all SDK errors. Contains code,statusCode, and retryable properties.
InvalidApiKeyErrorcode: INVALID_API_KEYThrown when the API key is invalid or missing.
InvalidAppIdErrorcode: INVALID_APP_IDThrown when the app ID is invalid or the app doesn't exist.
RateLimitErrorcode: RATE_LIMITEDThrown when rate limit is exceeded. Contains retryAfter property.
NetworkErrorcode: NETWORK_ERRORThrown when a network error occurs (e.g., no internet connection).
TimeoutErrorcode: TIMEOUTThrown when a request times out.
ServerErrorcode: SERVER_ERRORThrown when the server returns a 5xx error.
Error Handling Example
import {
AccessControl,
InvalidApiKeyError,
RateLimitError,
NetworkError,
} from '@billai/sdk';
try {
const result = await access.check(userId, feature);
// Handle result
} catch (error) {
if (error instanceof InvalidApiKeyError) {
console.error('Invalid API key - check your configuration');
} else if (error instanceof RateLimitError) {
console.error(`Rate limited. Retry after ${error.retryAfter}s`);
} else if (error instanceof NetworkError) {
console.error('Network error - check your connection');
} else {
console.error('Unexpected error:', error);
}
}Fail-Open Design
By default, the SDK is designed to fail open. If an unexpected error occurs during an access check, consider allowing access rather than blocking users due to service issues.
TypeScript Types
All types are exported from the package for use in TypeScript projects.
import type {
AccessControlConfig,
CheckOptions,
CheckResult,
ErrorCode,
} from '@billai/sdk';
// ErrorCode type
type ErrorCode =
| 'INVALID_API_KEY'
| 'INVALID_APP_ID'
| 'RATE_LIMITED'
| 'SERVER_ERROR'
| 'NETWORK_ERROR'
| 'TIMEOUT'
| 'UNKNOWN';Environment Variables
BILLAI_API_KEYstringBILLAI_APP_IDstringDEBUG?boolean# BillAI Configuration
BILLAI_API_KEY=sk_live_xxxxxxxxxxxx
BILLAI_APP_ID=app_xxxxxxxxxxxx
# Optional: Enable debug logging
DEBUG=true