Back to Blog

Use Cases / F06

Integracion multiplataforma: MERX en su stack existente

Every technology stack is different. Your backend might be Node.js, Go, Python, Ruby, or Java. Your architecture might be monolithic or microservices. Your communication patterns might be synchronous, event-driven, or a combination. The question is not whether MERX can fit into your stack -- it is which integration point gives you the most value with the least friction.

MERX offers five integration methods: REST API, WebSocket, webhooks, language SDKs, and an MCP server for AI agents. This article explains when to use each, how they interact, and how to choose the right approach for your specific architecture.

Integration Methods Overview

MethodBest ForDirectionLatencyLanguage
REST APIRequest-response operationsClient -> MERX~200msAny
WebSocketReal-time flujo de preciossMERX -> ClientReal-timeAny
WebhooksAsync notificationsMERX -> ClientEvent-drivenAny
JS SDKNode.js / browser appsBidirectional~200msJavaScript/TypeScript
Python SDKPython backends, scriptsBidirectional~200msPython
MCP ServerAI agent integrationBidirectional~500msAny (via MCP protocol)

REST API: The Universal Integration

The REST API works from any programming language with HTTP support. It is the lowest-common-denominator integration -- if your language can make HTTP requests, it can talk to MERX.

When to Use REST

Implementation

The API follows standard REST conventions with JSON payloads:

# Check prices
curl -X POST https://merx.exchange/api/v1/prices \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"energy_amount": 65000, "duration": "1h"}'

# Place an order
curl -X POST https://merx.exchange/api/v1/orders \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: unique-request-id-123" \
  -d '{
    "energy_amount": 65000,
    "duration": "1h",
    "target_address": "TYourAddress..."
  }'

# Check order status
curl https://merx.exchange/api/v1/orders/ORDER_ID \
  -H "Authorization: Bearer YOUR_API_KEY"

Go Example

package main

import (
    "bytes"
    "encoding/json"
    "fmt"
    "net/http"
)

type PriceRequest struct {
    EnergyAmount int    `json:"energy_amount"`
    Duration     string `json:"duration"`
}

type PriceResponse struct {
    Best struct {
        Provider string `json:"provider"`
        PriceSun int    `json:"price_sun"`
    } `json:"best"`
    Providers []struct {
        Provider string `json:"provider"`
        PriceSun int    `json:"price_sun"`
    } `json:"providers"`
}

func getBestPrice(amount int, duration string) (*PriceResponse, error) {
    body, _ := json.Marshal(PriceRequest{
        EnergyAmount: amount,
        Duration:     duration,
    })

    req, _ := http.NewRequest(
        "POST",
        "https://merx.exchange/api/v1/prices",
        bytes.NewBuffer(body),
    )
    req.Header.Set("Authorization", "Bearer "+apiKey)
    req.Header.Set("Content-Type", "application/json")

    resp, err := http.DefaultClient.Do(req)
    if err != nil {
        return nil, err
    }
    defer resp.Body.Close()

    var result PriceResponse
    json.NewDecoder(resp.Body).Decode(&result)
    return &result, nil
}

Error Handling

All API errors follow a consistent format:

{
  "error": {
    "code": "INSUFFICIENT_BALANCE",
    "message": "Account balance is insufficient for this order",
    "details": {
      "required": 1820000,
      "available": 500000
    }
  }
}

This consistency means your manejo de errores logic works the same regardless of which endpoint you call or which provider is involved behind the scenes.

WebSocket: Real-Time Price Feeds

WebSocket connections provide tiempo real actualizacion de precioss without polling. Prices stream to your application as they change across all seven providers.

When to Use WebSocket

Implementation

import WebSocket from 'ws';

const ws = new WebSocket(
  'wss://merx.exchange/ws',
  { headers: { 'Authorization': `Bearer ${API_KEY}` } }
);

ws.on('open', () => {
  // Subscribe to price updates for specific parameters
  ws.send(JSON.stringify({
    type: 'subscribe',
    channel: 'prices',
    params: {
      energy_amount: 65000,
      duration: '1h'
    }
  }));
});

ws.on('message', (data) => {
  const event = JSON.parse(data.toString());

  switch (event.type) {
    case 'price_update':
      console.log(
        `${event.provider}: ${event.price_sun} SUN`
      );
      break;

    case 'order_update':
      console.log(
        `Order ${event.order_id}: ${event.status}`
      );
      break;
  }
});

Combining WebSocket with REST

A common pattern: use WebSocket for monitor de preciosing and REST for order placement:

// WebSocket monitors prices
ws.on('message', async (data) => {
  const event = JSON.parse(data.toString());

  if (event.type === 'price_update' &&
      event.price_sun <= targetPrice) {
    // REST API places the order
    const response = await fetch(
      'https://merx.exchange/api/v1/orders',
      {
        method: 'POST',
        headers: {
          'Authorization': `Bearer ${API_KEY}`,
          'Content-Type': 'application/json'
        },
        body: JSON.stringify({
          energy_amount: 65000,
          duration: '1h',
          target_address: walletAddress
        })
      }
    );
  }
});

Webhooks: Async Event Notifications

Webhooks push notifications to your server when events occur. Unlike WebSocket (which requires a persistent connection), webhooks work with any server that can receive HTTP POST requests.

When to Use Webhooks

Implementation

import express from 'express';

const app = express();
app.use(express.json());

app.post('/webhooks/merx', (req, res) => {
  const event = req.body;

  switch (event.type) {
    case 'order.filled':
      handleOrderFilled(event.data);
      break;

    case 'order.failed':
      handleOrderFailed(event.data);
      break;

    case 'standing_order.triggered':
      handleStandingOrderTriggered(event.data);
      break;

    case 'auto_energy.purchased':
      handleAutoEnergyPurchased(event.data);
      break;
  }

  // Always respond 200 to acknowledge receipt
  res.status(200).json({ received: true });
});

async function handleOrderFilled(
  data: OrderFilledEvent
): Promise<void> {
  // Energy is available -- proceed with the transaction
  const pendingTx = await db.getPendingTransaction(
    data.order_id
  );
  if (pendingTx) {
    await executeTransaction(pendingTx);
  }
}

Webhook Reliability

MERX retries failed webhook deliveries with exponential backoff. Your endpoint should:

  1. Respond with 200 status quickly (within 5 seconds)
  2. Process the event asynchronously if processing takes time
  3. Handle duplicate deliveries idempotently (use the event ID for deduplication)
const processedEvents = new Set<string>();

app.post('/webhooks/merx', async (req, res) => {
  const event = req.body;

  // Idempotency check
  if (processedEvents.has(event.id)) {
    res.status(200).json({ received: true });
    return;
  }

  processedEvents.add(event.id);

  // Acknowledge immediately
  res.status(200).json({ received: true });

  // Process asynchronously
  processEvent(event).catch(console.error);
});

JavaScript SDK

The JavaScript/TypeScript SDK wraps the REST API and WebSocket connections with typed interfaces and convenience methods.

When to Use the JS SDK

Implementation

import { MerxClient } from 'merx-sdk';

const merx = new MerxClient({
  apiKey: process.env.MERX_API_KEY!
});

// Prices
const prices = await merx.getPrices({
  energy_amount: 65000,
  duration: '1h'
});

// Orders
const order = await merx.createOrder({
  energy_amount: 65000,
  duration: '1h',
  target_address: 'TAddress...'
});

// Standing orders
const standing = await merx.createStandingOrder({
  energy_amount: 65000,
  max_price_sun: 25,
  duration: '1h',
  repeat: true
});

// Energy estimation
const estimate = await merx.estimateEnergy({
  contract_address: 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t',
  function_selector: 'transfer(address,uint256)',
  parameter: [recipient, amount],
  owner_address: sender
});

// WebSocket (built into the SDK)
const ws = merx.connectWebSocket();
ws.on('price_update', (data) => {
  console.log(`${data.provider}: ${data.price_sun} SUN`);
});

The SDK handles authentication, request serialization, error parsing, and type validation automatically.

Python SDK

The Python SDK provides the same functionality for Python backends, data analysis pipelines, and automation scripts.

When to Use the Python SDK

Implementation

from merx import MerxClient

merx = MerxClient(api_key="your-api-key")

# Get prices
prices = merx.get_prices(
    energy_amount=65000,
    duration="1h"
)
print(f"Best: {prices.best.price_sun} SUN "
      f"via {prices.best.provider}")

# Place order
order = merx.create_order(
    energy_amount=65000,
    duration="1h",
    target_address="TAddress..."
)

# Estimate energy for a contract call
estimate = merx.estimate_energy(
    contract_address="TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t",
    function_selector="transfer(address,uint256)",
    parameter=[recipient, amount],
    owner_address=sender
)
print(f"Energy needed: {estimate.energy_required}")

Data Analysis Example

import pandas as pd
from merx import MerxClient

merx = MerxClient(api_key="your-api-key")

# Pull price history for analysis
history = merx.get_price_history(
    energy_amount=65000,
    duration="1h",
    period="30d"
)

df = pd.DataFrame(history.prices)
print(f"Average price: {df['price_sun'].mean():.1f} SUN")
print(f"Min price: {df['price_sun'].min()} SUN")
print(f"Max price: {df['price_sun'].max()} SUN")
print(f"Std dev: {df['price_sun'].std():.1f} SUN")

Servidor MCP: AI Agent Integration

The MERX MCP (Model Context Protocol) server allows AI agents to interact with the TRON mercado de energia directly. This is the newest integration point and enables a fundamentally different interaction model.

When to Use MCP

How It Works

The MCP server at github.com/Hovsteder/merx-mcp exposes MERX capabilities as tools that AI agents can call:

An AI agent can use these tools to answer questions like "What is the cheapest energia disponible right now?" or execute commands like "Buy 65,000 energy for my wallet at el mejor precio."

Integration

{
  "mcpServers": {
    "merx": {
      "command": "npx",
      "args": ["merx-mcp"],
      "env": {
        "MERX_API_KEY": "your-api-key"
      }
    }
  }
}

Choosing the Right Integration

Decision Matrix

Your SituationRecommended Integration
Quick prototype, any languageREST API
Node.js backendJS SDK
Python backendPython SDK
Real-time price displayWebSocket
Event-driven architectureWebhooks
Serverless (Lambda, Cloud Functions)REST API + Webhooks
AI agent buildingMCP Server
Go / Java / Ruby backendREST API
Full-featured applicationJS/Python SDK + Webhooks

Combining Integration Points

Most production systems use multiple integration methods:

Migration Path

If you are currently using a single provider's API, migrating to MERX is straightforward:

  1. Add the MERX SDK to your project
  2. Replace price checks with merx.getPrices() -- same data, more providers
  3. Replace order placement with merx.createOrder() -- same flow, mejor precio routing
  4. Add webhooks for order notifications (if not already event-driven)
  5. Remove old provider code once MERX integration is verified

The migration can be done incrementally. Run both systems in parallel during testing, comparing prices and order results before fully switching.

Conclusion

MERX is designed to fit into any technology stack through the integration method that matches your architecture. REST for universality, WebSocket for tiempo real, webhooks for events, SDKs for developer experience, and MCP for AI agents.

The choice is not exclusive -- combine integration points to match your needs. Start with the simplest option that solves your immediate problem, and expand as your requirements grow.

API documentation at https://merx.exchange/docs. MCP server at https://github.com/Hovsteder/merx-mcp. Platform at https://merx.exchange.


All Articles