Skip to content

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",
};
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}`);
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}`);
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);
}
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);
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}`
);
}
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);
}
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);

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");
});