Account API
Account SDK provides user information, subscription status, and quota management capabilities for plugins that need to offer differentiated features based on user identity or subscription tier.
Quick Start
import { accountSDK, SubscriptionPlan } from '@talex-touch/utils'
// Check if user is logged in
const isLoggedIn = await accountSDK.isLoggedIn()
// Get current subscription plan
const plan = await accountSDK.getPlan()
// Check if paid user
if (await accountSDK.isPaidUser()) {
// Show premium features
}
Subscription Plans
| Plan | Enum Value | Description |
|---|---|---|
| Free | SubscriptionPlan.FREE | Basic features with quota limits |
| Pro | SubscriptionPlan.PRO | More quota, custom models |
| Plus | SubscriptionPlan.PLUS | Priority support, advanced analytics |
| Team | SubscriptionPlan.TEAM | Team collaboration, management |
| Enterprise | SubscriptionPlan.ENTERPRISE | Unlimited quota, dedicated support |
API Reference
Get SDK Instance
import { accountSDK } from '@talex-touch/utils'
// Singleton, use directly
await accountSDK.getProfile()
Note: When using in Prelude script (index.js), you need to call accountSDK.setChannelSend(send) to inject the communication function first.
User Information
getProfile()
Get complete user profile.
const profile = await accountSDK.getProfile()
// {
// id: 'user_xxx',
// displayName: 'John Doe',
// email: 'john@example.com',
// avatarUrl: 'https://...',
// emailVerified: true,
// createdAt: 1702123456789,
// twoFactorEnabled: false,
// socialConnections: [...]
// }
| Return | Type | Description |
|---|---|---|
profile | UserProfile | null | User profile, null if not logged in |
isLoggedIn()
Check if user is logged in.
if (await accountSDK.isLoggedIn()) {
// Logged in
}
getUserId()
Get user ID.
const userId = await accountSDK.getUserId()
// 'user_xxx' or null
getDisplayName()
Get display name.
const name = await accountSDK.getDisplayName()
// 'John Doe'
getEmail()
Get user email.
const email = await accountSDK.getEmail()
// 'john@example.com'
getAvatarUrl()
Get avatar URL.
const avatar = await accountSDK.getAvatarUrl()
// 'https://...'
Subscription Checks
getPlan()
Get current subscription plan.
const plan = await accountSDK.getPlan()
// SubscriptionPlan.PRO
getSubscription()
Get full subscription details.
const subscription = await accountSDK.getSubscription()
// {
// id: 'sub_xxx',
// plan: 'pro',
// status: 'active',
// billingCycle: 'monthly',
// quota: { aiRequestsPerDay: 500, ... },
// usage: { aiRequestsToday: 123, ... },
// currentPeriodEnd: 1704067200000,
// autoRenew: true
// }
isPaidUser()
Check if user is a paid subscriber.
if (await accountSDK.isPaidUser()) {
// Show paid features
}
isProOrAbove()
Check if Pro tier or higher.
if (await accountSDK.isProOrAbove()) {
// Pro / Plus / Team / Enterprise
}
isPlusOrAbove()
Check if Plus tier or higher.
if (await accountSDK.isPlusOrAbove()) {
// Plus / Team / Enterprise
}
isTeamOrAbove()
Check if Team tier or higher.
if (await accountSDK.isTeamOrAbove()) {
// Team / Enterprise
}
isEnterprise()
Check if Enterprise tier.
if (await accountSDK.isEnterprise()) {
// Enterprise only
}
isTrialing()
Check if in trial period.
if (await accountSDK.isTrialing()) {
const days = await accountSDK.getTrialDaysRemaining()
console.log(`Trial ends in ${days} days`)
}
getDaysRemaining()
Get days remaining in current billing period.
const days = await accountSDK.getDaysRemaining()
// 28
Quota Checks
getQuota()
Get current plan quota limits.
const quota = await accountSDK.getQuota()
// {
// aiRequestsPerDay: 500,
// aiTokensPerMonth: 1000000,
// maxPlugins: 20,
// maxStorageBytes: 1073741824,
// customModelAccess: true,
// apiAccess: true,
// ...
// }
getUsage()
Get current usage statistics.
const usage = await accountSDK.getUsage()
// {
// aiRequestsToday: 123,
// aiRequestsThisMonth: 2456,
// aiTokensThisMonth: 456789,
// storageUsedBytes: 52428800,
// pluginsInstalled: 8
// }
checkAiRequestQuota()
Check AI request quota.
const result = await accountSDK.checkAiRequestQuota()
if (!result.allowed) {
console.log(result.reason) // 'Daily AI request limit reached'
console.log(`Resets at: ${new Date(result.resetAt)}`)
} else {
console.log(`Remaining: ${result.remaining}`)
}
checkAiTokenQuota(estimatedTokens)
Check AI token quota.
const result = await accountSDK.checkAiTokenQuota(1000)
if (!result.allowed) {
// Quota exceeded
}
| Parameter | Type | Description |
|---|---|---|
estimatedTokens | number | Estimated tokens to consume (optional, default 0) |
checkStorageQuota(additionalBytes)
Check storage quota.
const result = await accountSDK.checkStorageQuota(1024 * 1024) // 1MB
if (!result.allowed) {
console.log('Insufficient storage')
}
checkPluginQuota()
Check plugin installation quota.
const result = await accountSDK.checkPluginQuota()
if (!result.allowed) {
console.log('Plugin limit reached')
}
getUsagePercentage(type)
Get usage percentage for specific quota type.
const aiUsage = await accountSDK.getUsagePercentage('aiRequests')
console.log(`AI usage: ${aiUsage.toFixed(1)}%`)
const storageUsage = await accountSDK.getUsagePercentage('storage')
if (storageUsage > 80) {
console.warn('Storage running low')
}
| Parameter | Type | Values |
|---|---|---|
type | string | 'aiRequests', 'aiTokens', 'storage', 'plugins' |
Feature Access
hasApiAccess()
Check if user has API access.
if (await accountSDK.hasApiAccess()) {
// Allow API usage
}
hasCustomModelAccess()
Check if user can use custom models.
if (await accountSDK.hasCustomModelAccess()) {
// Show custom model options
}
hasPrioritySupport()
Check if user has priority support.
if (await accountSDK.hasPrioritySupport()) {
// Show priority support entry
}
hasAdvancedAnalytics()
Check if user has advanced analytics.
if (await accountSDK.hasAdvancedAnalytics()) {
// Show analytics dashboard
}
hasFeature(featureId)
Check if specific feature flag is enabled.
if (await accountSDK.hasFeature('beta-ai-v2')) {
// Enable beta feature
}
Team Management
getTeams()
Get all teams user belongs to.
const teams = await accountSDK.getTeams()
// [
// { id: 'team_xxx', name: 'Dev Team', role: 'admin', memberCount: 5 },
// { id: 'team_yyy', name: 'Design', role: 'member', memberCount: 3 }
// ]
isInTeam()
Check if user is in any team.
if (await accountSDK.isInTeam()) {
// Show team features
}
isTeamOwner(teamId?)
Check if user is team owner.
if (await accountSDK.isTeamOwner()) {
// User owns some team
}
if (await accountSDK.isTeamOwner('team_xxx')) {
// User owns specific team
}
isTeamAdmin(teamId?)
Check if user is team admin (includes owner).
if (await accountSDK.isTeamAdmin()) {
// Show admin features
}
Upgrade & Billing
getUpgradeOptions()
Get available upgrade options.
const options = accountSDK.getUpgradeOptions()
// [
// { plan: 'pro', name: 'Pro', priceMonthly: 9.99, features: [...] },
// { plan: 'plus', name: 'Plus', priceMonthly: 19.99, recommended: true, ... }
// ]
getPlanComparison()
Get plan comparison table.
const comparison = accountSDK.getPlanComparison()
// [
// { feature: 'AI Requests/Day', free: 50, pro: 500, plus: 2000, ... },
// { feature: 'Custom Models', free: false, pro: true, ... }
// ]
openUpgradePage(plan?)
Open upgrade page.
await accountSDK.openUpgradePage() // Open upgrade page
await accountSDK.openUpgradePage(SubscriptionPlan.PLUS) // Jump to Plus
openBillingPage()
Open billing management page.
await accountSDK.openBillingPage()
Account Actions
requestLogin()
Request user login (opens login dialog).
const success = await accountSDK.requestLogin()
if (success) {
// User logged in
}
logout()
Logout current user.
await accountSDK.logout()
openAccountSettings()
Open account settings page.
await accountSDK.openAccountSettings()
Best Practices
1. Premium Feature Gating
async function showPremiumFeature() {
if (!await accountSDK.isPaidUser()) {
// Show upgrade prompt
const options = accountSDK.getUpgradeOptions()
showUpgradeDialog(options)
return
}
// Show premium feature
}
2. Quota Pre-check
async function beforeAiRequest(estimatedTokens: number) {
const quotaCheck = await accountSDK.checkAiTokenQuota(estimatedTokens)
if (!quotaCheck.allowed) {
throw new Error(`Quota exceeded: ${quotaCheck.reason}`)
}
// Proceed with request
}
3. Team Feature Adaptation
async function initTeamFeatures() {
if (!await accountSDK.isTeamOrAbove()) {
return // Not team tier, skip
}
const teams = await accountSDK.getTeams()
for (const team of teams) {
if (team.role === 'owner' || team.role === 'admin') {
// Show admin features
}
}
}
Type Definitions
enum SubscriptionPlan {
FREE = 'free',
PRO = 'pro',
PLUS = 'plus',
TEAM = 'team',
ENTERPRISE = 'enterprise',
}
interface UserProfile {
id: string
displayName: string
username?: string
email: string
emailVerified: boolean
avatarUrl?: string
createdAt: number
twoFactorEnabled: boolean
socialConnections: SocialConnection[]
}
interface Subscription {
id: string
plan: SubscriptionPlan
status: SubscriptionStatus
quota: PlanQuota
usage: UsageStats
currentPeriodEnd: number
autoRenew: boolean
}
interface QuotaCheckResult {
allowed: boolean
reason?: string
remaining?: number
resetAt?: number
}
Related Documentation
- Intelligence API - AI capabilities
- Storage API - Plugin data persistence
- Plugin Context - Plugin global context