@offcoin/sdk
TypeScript/JavaScript SDK for the Offcoin API. Manage members, tokens, XP, achievements, and leaderboards.
Installation
npm install @offcoin/sdk
pnpm add @offcoin/sdk
yarn add @offcoin/sdk
Quick Start
import { OffcoinClient } from '@offcoin/sdk';
const offcoin = new OffcoinClient({
baseUrl: 'https://your-offcoin-instance.com',
clientId: 'your_client_id',
clientSecret: 'your_client_secret'
});
const members = await offcoin.members.list();
const newMember = await offcoin.members.create({
name: 'John Doe',
aliases: ['discord:123456', 'github:johndoe']
});
const result = await offcoin.members.addTokens('discord:123456', 100, 'Welcome bonus');
console.log(`New balance: ${result.newBalance}`);
API Reference
Members
const members = await offcoin.members.list();
const member = await offcoin.members.create({
name: 'John Doe',
avatarUrl: 'https://example.com/avatar.png',
aliases: ['discord:123456', 'github:johndoe']
});
const member = await offcoin.members.get('discord:123456');
const updated = await offcoin.members.update('discord:123456', {
name: 'Jane Doe',
avatarUrl: null
});
const balance = await offcoin.members.getBalance('discord:123456');
console.log(`Balance: ${balance.balance}`);
const xp = await offcoin.members.getXp('discord:123456');
console.log(`XP: ${xp.xp}, Level: ${xp.level}`);
const result = await offcoin.members.addTokens('discord:123456', 100, 'Purchase reward');
const result = await offcoin.members.subtractTokens('discord:123456', 50, 'Item purchase');
const result = await offcoin.members.addXp('discord:123456', 25);
Achievements
const achievements = await offcoin.achievements.list();
const result = await offcoin.achievements.unlock('achievement-id', 'discord:123456');
console.log(`Unlocked: ${result.achievementName}`);
console.log(`Cascading achievements: ${result.cascadingAchievementsGranted}`);
Leaderboard
const leaderboard = await offcoin.leaderboard.get();
const tokenLeaderboard = await offcoin.leaderboard.get({
sort: 'tokens',
limit: 10
});
const levelLeaderboard = await offcoin.leaderboard.get({
sort: 'level',
limit: 25
});
Error Handling
The SDK throws typed errors for different scenarios:
import {
OffcoinClient,
AuthenticationError,
NotFoundError,
ValidationError
} from '@offcoin/sdk';
try {
await offcoin.members.get('unknown-alias');
} catch (error) {
if (error instanceof AuthenticationError) {
console.error('Invalid credentials');
} else if (error instanceof NotFoundError) {
console.error('Member not found');
} else if (error instanceof ValidationError) {
console.error('Validation failed:', error.message);
}
}
Error Types
AuthenticationError | UNAUTHORIZED | Invalid client_id or client_secret |
ForbiddenError | FORBIDDEN | Not authorized for this action |
NotFoundError | NOT_FOUND | Resource not found |
ValidationError | VALIDATION_ERROR | Invalid input data |
BadRequestError | BAD_REQUEST | Malformed request |
InternalError | INTERNAL_ERROR | Server error |
OffcoinError | Various | Base error class |
TypeScript Types
All types are exported for use in your application:
import type {
Member,
MemberBalance,
MemberXp,
Achievement,
LeaderboardEntry,
TokenOperationResult,
XpOperationResult,
UnlockResult
} from '@offcoin/sdk';
Authentication
The SDK uses Basic Authentication with your API credentials. You can find your clientId and clientSecret in the Offcoin admin dashboard under Settings > API Access.
const offcoin = new OffcoinClient({
baseUrl: 'https://your-offcoin-instance.com',
clientId: process.env.OFFCOIN_CLIENT_ID!,
clientSecret: process.env.OFFCOIN_CLIENT_SECRET!
});
Requirements
- Node.js 18+ (for native fetch support) or a browser environment
- TypeScript 5.0+ (for type definitions)
License
MIT