JavaScript SDK
Official JavaScript/TypeScript SDK for CoderspaE API integration
Installation#
# Using npm
npm install @coderspae/sdk
# Using yarn
yarn add @coderspae/sdk
# Using pnpm
pnpm add @coderspae/sdk
Initialization#
import { CoderspaE } from '@coderspae/sdk';
// Initialize with API key
const coderspae = new CoderspaE({
apiKey: 'your-api-key', // Get from dashboard
environment: 'production', // 'production' | 'sandbox'
timeout: 30000, // Request timeout in ms
retries: 3, // Number of retries
});
// TypeScript users get full type safety
const client: CoderspaE = new CoderspaE({
apiKey: process.env.CODERSPAE_API_KEY!,
environment: 'production',
});i
API Key RequiredYou need an API key to use the SDK. Get one from your dashboard.
Battle Management#
Create a Battle
// Create a practice battle
const battle = await coderspae.battles.create({
type: 'practice',
difficulty: 'medium',
language: 'javascript',
timeLimit: 3600, // seconds
problemId: 'two-sum', // optional specific problem
});
// Create a real-time battle
const liveBattle = await coderspae.battles.create({
type: 'realtime',
participants: 2,
difficulty: 'hard',
language: 'python',
timeLimit: 1800,
ranked: true, // affects ELO rating
});
console.log('Battle ID:', battle.id);
console.log('Join URL:', battle.joinUrl);Join a Battle
// Join an existing battle
const battleSession = await coderspae.battles.join('battle-id');
// Get battle details
const battleInfo = await coderspae.battles.get('battle-id');
console.log('Problem:', battleInfo.problem.title);
console.log('Participants:', battleInfo.participants);
console.log('Time Remaining:', battleInfo.timeRemaining);Submit Solutions
// Submit a solution
const submission = await battleSession.submit({
language: 'javascript',
code: `
function twoSum(nums, target) {
const map = new Map();
for (let i = 0; i < nums.length; i++) {
const complement = target - nums[i];
if (map.has(complement)) {
return [map.get(complement), i];
}
map.set(nums[i], i);
}
return [];
}
`,
});
console.log('Status:', submission.status); // 'accepted' | 'wrong-answer' | 'timeout'
console.log('Score:', submission.score);
console.log('Execution Time:', submission.executionTime);
console.log('Memory Used:', submission.memoryUsed);Real-time Events#
The SDK provides WebSocket support for real-time battle updates.
// Subscribe to battle events
const battle = await coderspae.battles.join('battle-id');
// Listen for submissions from other participants
battle.on('submission', (event) => {
console.log(`${event.participant.username} submitted a solution`);
console.log('Status:', event.submission.status);
});
// Listen for battle state changes
battle.on('stateChange', (event) => {
console.log('Battle state:', event.state); // 'waiting' | 'active' | 'finished'
});
// Listen for leaderboard updates
battle.on('leaderboardUpdate', (event) => {
console.log('Current rankings:', event.rankings);
});
// Listen for chat messages
battle.on('chat', (event) => {
console.log(`${event.user}: ${event.message}`);
});
// Send chat message
await battle.sendMessage('Good luck everyone!');
// Disconnect when done
battle.disconnect();Tournament Management#
// List available tournaments
const tournaments = await coderspae.tournaments.list({
status: 'open', // 'open' | 'active' | 'completed'
difficulty: 'medium',
limit: 10,
});
// Register for a tournament
const registration = await coderspae.tournaments.register('tournament-id');
// Get tournament details
const tournament = await coderspae.tournaments.get('tournament-id');
// Get tournament leaderboard
const leaderboard = await coderspae.tournaments.getLeaderboard('tournament-id');
console.log('Top 10:', leaderboard.slice(0, 10));Error Handling#
import { CoderspaEError, ApiError, NetworkError } from '@coderspae/sdk';
try {
const battle = await coderspae.battles.create({
type: 'practice',
difficulty: 'easy',
});
} catch (error) {
if (error instanceof ApiError) {
console.error('API Error:', error.message);
console.error('Status Code:', error.statusCode);
console.error('Error Code:', error.code);
} else if (error instanceof NetworkError) {
console.error('Network Error:', error.message);
} else if (error instanceof CoderspaEError) {
console.error('SDK Error:', error.message);
} else {
console.error('Unknown Error:', error);
}
}TypeScript Support#
The SDK is written in TypeScript and provides comprehensive type definitions.
import {
CoderspaE,
Battle,
Tournament,
Submission,
BattleType,
Difficulty,
Language
} from '@coderspae/sdk';
// All types are exported and available
const difficulty: Difficulty = 'medium';
const language: Language = 'typescript';
// Method responses are fully typed
const battle: Battle = await coderspae.battles.create({
type: 'practice' as BattleType,
difficulty,
language,
});
// Event callbacks have proper typing
battle.on('submission', (event: SubmissionEvent) => {
console.log(event.submission.score); // TypeScript knows this is a number
});