JavaScript SDK
The official Merx JavaScript client for Node.js 18+ and modern browsers. Ships with full TypeScript type definitions. Version 0.1.0.
Installation
npmbash
npm install @merx/sdkyarnbash
yarn add @merx/sdkInitialization
Setuptypescript
import { MerxClient } from '@merx/sdk'
const merx = new MerxClient({
apiKey: 'sk_live_your_key_here',
// Optional: override base URL for testing
// baseUrl: 'https://staging.merx.exchange',
})Prices
Query live energy prices across all providers. The prices endpoint is public and does not require authentication.
Prices moduletypescript
// All provider prices
const prices = await merx.prices.list()
// Cheapest price for given params
const best = await merx.prices.best({
resourceType: 'ENERGY',
amount: 65000,
durationSec: 86400,
})
// Historical price data
const history = await merx.prices.history({
provider: 'catfee',
hours: 24,
})
// Aggregated statistics
const stats = await merx.prices.stats()
// Preview order cost without placing it
const preview = await merx.prices.preview({
resourceType: 'ENERGY',
amount: 65000,
durationSec: 86400,
})Orders
Create and manage energy and bandwidth orders. All order endpoints require authentication.
Orders moduletypescript
// Place an order
const order = await merx.orders.create({
resourceType: 'ENERGY',
amount: 65000,
durationSec: 86400,
targetAddress: 'TYourTargetAddressHere',
})
// List orders with optional filters
const orders = await merx.orders.list({
status: 'FILLED',
limit: 20,
})
// Get a specific order by ID
const detail = await merx.orders.get('ord_abc123')Balance
Balance moduletypescript
// Current balances
const balance = await merx.balance.get()
// Deposit address and instructions
const deposit = await merx.balance.depositInfo()
// Withdraw TRX
const withdrawal = await merx.balance.withdraw({
amount: 1000000, // in SUN
toAddress: 'TYourWithdrawAddress',
})
// Transaction history
const history = await merx.balance.history({ limit: 50 })
// Balance summary with totals
const summary = await merx.balance.summary()Webhooks
Webhooks moduletypescript
// Register a webhook endpoint
const webhook = await merx.webhooks.create({
url: 'https://example.com/webhooks/merx',
events: ['order.filled', 'deposit.received'],
})
// List all registered webhooks
const webhooks = await merx.webhooks.list()
// Delete a webhook
await merx.webhooks.delete('wh_abc123')Error Handling
All SDK methods throw a MerxError on failure. The error includes a machine-readable code, a human-readable message, and optional details.
Error handlingtypescript
import { MerxClient, MerxError } from '@merx/sdk'
try {
await merx.orders.create({ /* ... */ })
} catch (err) {
if (err instanceof MerxError) {
console.log(err.code) // e.g. 'INSUFFICIENT_BALANCE'
console.log(err.message) // human-readable description
console.log(err.status) // HTTP status code
console.log(err.details) // additional context, if any
}
}TypeScript Support
Every method returns a fully typed response. Import types directly from the package if needed.
Typestypescript
import type { Order, Price, Balance } from '@merx/sdk'
const order: Order = await merx.orders.get('ord_abc123')
const prices: Price[] = await merx.prices.list()TIP
The SDK automatically retries failed requests once on network errors and 5xx responses. Disable with { retry: false } in the client options.