merx-sdk
v2.1.2
Published
Official JavaScript/TypeScript SDK for the Merx TRON resource exchange
Readme
@merx/sdk
JavaScript/TypeScript SDK for the Merx TRON energy exchange.
Installation
npm install @merx/sdkRequires Node.js 18+ (uses native fetch). Zero dependencies.
Quickstart
import { MerxClient } from '@merx/sdk';
const merx = new MerxClient({ apiKey: 'sk_live_...' });
// Check prices
const prices = await merx.prices.list();
const best = await merx.prices.best('ENERGY');
// Preview cost before ordering
const preview = await merx.prices.preview({
resource: 'ENERGY',
amount: 65000,
duration: 3600,
});
// Create order
const order = await merx.orders.create({
resource_type: 'ENERGY',
amount: 65000,
duration_sec: 3600,
target_address: 'TYourAddress...',
});API Reference
Constructor
const merx = new MerxClient({
apiKey: string; // Required. API key from merx.exchange
baseUrl?: string; // Optional. Default: "https://merx.exchange"
});merx.prices
| Method | Returns | Description |
|--------|---------|-------------|
| list() | Promise<ProviderPrice[]> | All current prices from all providers |
| best(resource, amount?) | Promise<PriceHistoryEntry> | Cheapest available provider (server-side) |
| history(params?) | Promise<PriceHistoryEntry[]> | Historical price snapshots |
| stats() | Promise<PriceStats> | Market statistics (best/avg price, provider count) |
| preview(params) | Promise<OrderPreview> | Preview order cost with best and fallback options |
merx.orders
| Method | Returns | Description |
|--------|---------|-------------|
| create(params) | Promise<Order> | Place a new order |
| list(limit?, offset?, status?) | Promise<{ orders, total }> | List your orders with pagination |
| get(id) | Promise<OrderWithFills> | Order details with fill history |
CreateOrderParams:
{
resource_type: 'ENERGY' | 'BANDWIDTH';
order_type?: 'MARKET' | 'LIMIT' | 'PERIODIC' | 'BROADCAST'; // Default: MARKET
amount: number; // Min 65000 for ENERGY, 300 for BANDWIDTH
duration_sec: number; // Duration in seconds (3600 = 1h, 86400 = 1d)
target_address: string; // TRON address to receive resources
max_price_sun?: number; // Maximum price per unit in SUN
idempotency_key?: string; // Prevent duplicate orders
}merx.balance
| Method | Returns | Description |
|--------|---------|-------------|
| get() | Promise<Balance> | TRX balance, USDT balance, locked amount |
| depositInfo() | Promise<DepositInfo> | Deposit address and memo |
| withdraw(params) | Promise<Withdrawal> | Withdraw TRX or USDT |
| history(period?) | Promise<HistoryEntry[]> | Transaction history (7D/30D/90D) |
| summary() | Promise<HistorySummary> | Aggregated stats |
merx.webhooks
| Method | Returns | Description |
|--------|---------|-------------|
| create(params) | Promise<Webhook> | Register a webhook (secret shown once) |
| list() | Promise<Webhook[]> | List all webhooks |
| delete(id) | Promise<{ deleted }> | Delete a webhook |
Error Handling
All API errors throw MerxError:
import { MerxClient, MerxError } from '@merx/sdk';
try {
await merx.orders.create({ ... });
} catch (err) {
if (err instanceof MerxError) {
console.error(err.code, err.message);
}
}Types
All TypeScript types are exported:
ProviderPrice, PricePoint, PriceHistoryEntry, PriceStats, OrderPreview, PreviewMatch, Balance, DepositInfo, Order, OrderWithFills, Fill, CreateOrderParams, HistoryEntry, HistorySummary, Withdrawal, WithdrawParams, Webhook, WebhookParams, MerxClientOptions, MerxError
License
MIT
