-
-
Notifications
You must be signed in to change notification settings - Fork 6
Node js Integration
Richard Fu edited this page Jan 3, 2026
·
2 revisions
Guide for using the Stake Engine Client in Node.js server environments.
The client works in Node.js environments but requires explicit configuration since URL parameters aren't available on the server.
npm install stake-engine-clientAlways provide explicit configuration in Node.js:
import { authenticate, play } from 'stake-engine-client';
const config = {
sessionID: 'player-session-123',
rgsUrl: 'rgs.stake-engine.com',
language: 'en'
};
// Authenticate
const auth = await authenticate(config);
// Place bet
const bet = await play({
...config,
currency: 'USD',
amount: 1.00,
mode: 'base'
});Use environment variables for configuration:
// .env file
SESSION_ID=player-session-123
RGS_URL=rgs.stake-engine.com
LANGUAGE=en
CURRENCY=USDimport 'dotenv/config';
import { authenticate, play } from 'stake-engine-client';
const config = {
sessionID: process.env.SESSION_ID!,
rgsUrl: process.env.RGS_URL!,
language: process.env.LANGUAGE || 'en'
};
async function serverSideBet(amount: number, mode: string) {
const auth = await authenticate(config);
console.log('Player balance:', auth.balance?.amount);
const bet = await play({
...config,
currency: process.env.CURRENCY || 'USD',
amount,
mode
});
return bet;
}Create API endpoints for your game:
import express from 'express';
import { authenticate, play, endRound } from 'stake-engine-client';
const app = express();
app.use(express.json());
// Authenticate endpoint
app.post('/api/authenticate', async (req, res) => {
try {
const { sessionID, rgsUrl } = req.body;
const auth = await authenticate({
sessionID,
rgsUrl,
language: 'en'
});
res.json(auth);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// Place bet endpoint
app.post('/api/bet', async (req, res) => {
try {
const { sessionID, rgsUrl, amount, mode, currency } = req.body;
const bet = await play({
sessionID,
rgsUrl,
currency,
amount,
mode
});
res.json(bet);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// End round endpoint
app.post('/api/end-round', async (req, res) => {
try {
const { sessionID, rgsUrl } = req.body;
const result = await endRound({
sessionID,
rgsUrl
});
res.json(result);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
app.listen(3000, () => {
console.log('Server running on port 3000');
});Using the client in Next.js API routes:
// pages/api/authenticate.ts
import type { NextApiRequest, NextApiResponse } from 'next';
import { authenticate } from 'stake-engine-client';
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
if (req.method !== 'POST') {
return res.status(405).json({ error: 'Method not allowed' });
}
try {
const { sessionID, rgsUrl } = req.body;
const auth = await authenticate({
sessionID,
rgsUrl,
language: 'en'
});
res.status(200).json(auth);
} catch (error) {
res.status(500).json({ error: error.message });
}
}// pages/api/bet.ts
import type { NextApiRequest, NextApiResponse } from 'next';
import { play } from 'stake-engine-client';
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
if (req.method !== 'POST') {
return res.status(405).json({ error: 'Method not allowed' });
}
try {
const { sessionID, rgsUrl, amount, mode, currency } = req.body;
const bet = await play({
sessionID,
rgsUrl,
currency,
amount,
mode
});
res.status(200).json(bet);
} catch (error) {
res.status(500).json({ error: error.message });
}
}Managing player sessions on the server:
import { authenticate, play } from 'stake-engine-client';
interface PlayerSession {
sessionID: string;
rgsUrl: string;
balance: number;
authenticated: boolean;
}
class SessionManager {
private sessions = new Map<string, PlayerSession>();
async authenticate(sessionID: string, rgsUrl: string) {
const auth = await authenticate({
sessionID,
rgsUrl,
language: 'en'
});
this.sessions.set(sessionID, {
sessionID,
rgsUrl,
balance: auth.balance?.amount || 0,
authenticated: true
});
return auth;
}
async placeBet(sessionID: string, amount: number, mode: string) {
const session = this.sessions.get(sessionID);
if (!session?.authenticated) {
throw new Error('Session not authenticated');
}
const bet = await play({
sessionID: session.sessionID,
rgsUrl: session.rgsUrl,
currency: 'USD',
amount,
mode
});
// Update cached balance
session.balance = bet.balance?.amount || session.balance;
return bet;
}
getSession(sessionID: string) {
return this.sessions.get(sessionID);
}
}
export const sessionManager = new SessionManager();Robust error handling for server environments:
import { play } from 'stake-engine-client';
async function safeBet(
sessionID: string,
rgsUrl: string,
amount: number,
mode: string
) {
try {
const response = await play({
sessionID,
rgsUrl,
currency: 'USD',
amount,
mode
});
if (response.status?.statusCode !== 'SUCCESS') {
return {
success: false,
error: response.status?.statusCode,
message: response.status?.statusMessage
};
}
return {
success: true,
data: response
};
} catch (error) {
console.error('Bet failed:', error);
return {
success: false,
error: 'NETWORK_ERROR',
message: error.message
};
}
}Mock the client for testing:
import { jest } from '@jest/globals';
// Mock the entire module
jest.mock('stake-engine-client', () => ({
authenticate: jest.fn(),
play: jest.fn(),
endRound: jest.fn()
}));
import { play } from 'stake-engine-client';
test('places bet successfully', async () => {
(play as jest.Mock).mockResolvedValue({
status: { statusCode: 'SUCCESS' },
round: { roundID: '123', payoutMultiplier: 2.5 },
balance: { amount: 5000000, currency: 'USD' }
});
const result = await myBetFunction(1.00, 'base');
expect(result.success).toBe(true);
expect(play).toHaveBeenCalledWith({
sessionID: expect.any(String),
rgsUrl: expect.any(String),
currency: 'USD',
amount: 1.00,
mode: 'base'
});
});Add logging for debugging:
import { play } from 'stake-engine-client';
import winston from 'winston';
const logger = winston.createLogger({
level: 'info',
format: winston.format.json(),
transports: [
new winston.transports.File({ filename: 'game.log' })
]
});
async function loggedBet(
sessionID: string,
rgsUrl: string,
amount: number,
mode: string
) {
logger.info('Placing bet', { sessionID, amount, mode });
try {
const response = await play({
sessionID,
rgsUrl,
currency: 'USD',
amount,
mode
});
logger.info('Bet placed successfully', {
roundID: response.round?.roundID,
payout: response.round?.payoutMultiplier
});
return response;
} catch (error) {
logger.error('Bet failed', { error: error.message });
throw error;
}
}Recommended tsconfig.json for Node.js:
{
"compilerOptions": {
"target": "ES2020",
"module": "commonjs",
"lib": ["ES2020"],
"moduleResolution": "node",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"resolveJsonModule": true,
"outDir": "./dist",
"rootDir": "./src"
},
"include": ["src/**/*"],
"exclude": ["node_modules"]
}-
Reuse connections - The client uses
fetchwhich pools connections automatically - Cache authentication - Don't authenticate on every request
- Use environment variables - Avoid hardcoding credentials
- Implement rate limiting - Protect against abuse
- Log errors - Monitor API failures
The package supports both CommonJS and ES modules:
// ESM
import { play } from 'stake-engine-client';
// CommonJS
const { play } = require('stake-engine-client');- Package Integration - Installation and setup
- Browser Integration - Browser-specific patterns
- Usage Patterns - Common examples
- Error Handling - Error handling strategies