JavaScript Examples
Examples for interacting with the XBTFX Trading API using JavaScript. These examples use the built-in fetch API (Node.js 18+) and the ws library for WebSocket.
const BASE_URL = "https://interface.xbtfx.com";const API_KEY = "your_api_key_here";
const headers = { Authorization: `Bearer ${API_KEY}`, "Content-Type": "application/json",};Check Auth Status
Section titled “Check Auth Status”const response = await fetch(`${BASE_URL}/v1/auth/status`, { headers });const auth = await response.json();
console.log(`Login: ${auth.login}`);console.log(`Status: ${auth.status}`);console.log(`Mode: ${auth.margin_mode}`);Get Account Info
Section titled “Get Account Info”const response = await fetch(`${BASE_URL}/v1/account`, { headers });const account = await response.json();
console.log(`Balance: ${account.balance}`);console.log(`Equity: ${account.equity}`);console.log(`Free Margin: ${account.margin_free}`);Open a Trade
Section titled “Open a Trade”const response = await fetch(`${BASE_URL}/v1/trade`, { method: "POST", headers, body: JSON.stringify({ symbol: "EURUSD", side: "buy", volume: 0.1, sl: 1.08, tp: 1.095, }),});
const result = await response.json();
if (result.retcode === 10009 || result.retcode === 10008) { console.log(`Trade opened: deal=${result.deal}, price=${result.price}`);} else { console.error("Trade failed:", result);}Open a Trade with Idempotency Key
Section titled “Open a Trade with Idempotency Key”import { randomUUID } from "node:crypto";
const response = await fetch(`${BASE_URL}/v1/trade`, { method: "POST", headers: { ...headers, "Idempotency-Key": randomUUID(), }, body: JSON.stringify({ symbol: "BTCUSD", side: "sell", volume: 0.01, }),});
const result = await response.json();console.log(result);Get Open Positions
Section titled “Get Open Positions”const response = await fetch(`${BASE_URL}/v1/positions`, { headers });const positions = await response.json();
for (const pos of positions) { console.log( `#${pos.ticket} ${pos.symbol} ${pos.side} ${pos.volume} lots P&L: ${pos.profit}` );}Close a Position
Section titled “Close a Position”const response = await fetch(`${BASE_URL}/v1/close`, { method: "POST", headers, body: JSON.stringify({ ticket: 12345678, }),});
const result = await response.json();
if (result.retcode === 10009 || result.retcode === 10008) { console.log(`Closed at ${result.price}, deal=${result.deal}`);} else { console.error("Close failed:", result);}Modify SL/TP
Section titled “Modify SL/TP”const response = await fetch(`${BASE_URL}/v1/modify`, { method: "POST", headers, body: JSON.stringify({ ticket: 12345678, sl: 1.082, tp: 1.098, }),});
const result = await response.json();console.log(result);WebSocket: Stream Quotes
Section titled “WebSocket: Stream Quotes”Requires the ws library (npm install ws).
import WebSocket from "ws";
const API_KEY = "your_api_key_here";const ws = new WebSocket("wss://interface.xbtfx.com/v1/ws");
ws.on("open", () => { ws.send(JSON.stringify({ type: "auth", api_key: API_KEY, }));});
ws.on("message", (data) => { const msg = JSON.parse(data);
if (msg.type === "auth" && msg.status === "ok") { console.log("Authenticated"); ws.send(JSON.stringify({ type: "subscribe", symbols: ["EURUSD", "GBPUSD", "BTCUSD"], })); }
if (msg.type === "quote") { console.log(`${msg.symbol}: ${msg.bid} / ${msg.ask}`); }});
ws.on("error", (err) => { console.error("WebSocket error:", err.message);});
ws.on("close", () => { console.log("Connection closed");});