Monitor Prices
Three approaches to tracking TRON energy prices: REST API polling, WebSocket streaming, and the embeddable widget. Choose based on your latency needs and architecture.
When to Use Each Approach
| Approach | Latency | Best For |
|---|---|---|
| REST polling | 30-60s | Cron jobs, serverless functions, simple scripts |
| WebSocket | Real-time | Dashboards, trading bots, live applications |
| Price widget | 60s | Website embeds, informational pages |
Approach 1: REST API Polling
Call GET /api/v1/prices on a timer. The endpoint is public and rate-limited to 60 requests per minute. Prices update every 30 seconds on the server side, so polling more frequently than that provides no benefit.
async function pollPrices() {
const res = await fetch('https://merx.exchange/api/v1/prices')
const data = await res.json()
for (const provider of data.prices) {
const cheapest = provider.energy_prices[0]
console.log(provider.provider, cheapest.price_sun, 'SUN')
}
}
// Poll every 30 seconds
setInterval(pollPrices, 30_000)
pollPrices()Approach 2: WebSocket Streaming
Connect to wss://merx.exchange/ws for real-time price updates pushed by the server. No authentication required. You receive a message each time any provider price changes.
const ws = new WebSocket('wss://merx.exchange/ws')
ws.addEventListener('open', () => {
// Optional: filter to specific providers
ws.send(JSON.stringify({ subscribe: ['catfee', 'itrx'] }))
})
ws.addEventListener('message', (event) => {
const price = JSON.parse(event.data)
console.log(price.provider, price.energy_prices)
})See the WebSocket reference for heartbeat handling and reconnection logic.
Approach 3: Price Widget
For websites that just need to display current prices, embed the widget with two lines of HTML. It auto-refreshes every 60 seconds.
<div id="merx-prices"></div>
<script src="https://merx.exchange/widget.js"></script>See the widget reference for customization options.
Example: Price Alert Bot
A practical example that monitors prices via WebSocket and sends a notification when the cheapest energy price drops below a threshold. This pattern is useful for buying energy at optimal prices.
const WebSocket = require('ws')
const THRESHOLD_SUN = 80
const ALERT_URL = 'https://hooks.slack.com/services/YOUR/WEBHOOK/URL'
function connect() {
const ws = new WebSocket('wss://merx.exchange/ws')
ws.on('message', async (raw) => {
const price = JSON.parse(raw)
const cheapest = price.energy_prices?.[0]
if (cheapest && cheapest.price_sun < THRESHOLD_SUN) {
const msg = [
price.provider,
': ',
cheapest.price_sun,
' SUN (below ',
THRESHOLD_SUN,
')',
].join('')
await fetch(ALERT_URL, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ text: msg }),
})
}
})
ws.on('close', () => {
// Reconnect after 5 seconds
setTimeout(connect, 5000)
})
}
connect()Combine price monitoring with the prices.best() SDK method to automatically place an order when the price hits your target.