# Trade MetaTrader 5 from Node.js

There is no official MetaTrader 5 SDK for JavaScript. With the AlgoDeltaFX REST API you don't need one: Node.js 18+ ships `fetch`, which is all it takes to trade MT5 from a server, a Discord or Telegram bot, a Next.js API route or a serverless function.

## A typed client

```ts
const BASE = "https://api.algodeltafx.com/api/v2/forexaccount";

export enum OrderType { Buy, Sell, BuyLimit, SellLimit, BuyStop, SellStop, BuyStopLimit, SellStopLimit }

export function mt5(token: string) {
  async function call<T = any>(method: "GET" | "POST", path: string, body?: object): Promise<T> {
    const res = await fetch(`${BASE}/${path}`, {
      method,
      headers: { Authorization: `Bearer ${token}`, "Content-Type": "application/json" },
      body: body ? JSON.stringify(body) : undefined,
    });
    const json = await res.json();
    if (!res.ok || !json.status) throw new Error(json.msg ?? `HTTP ${res.status}`);
    return json.data;
  }

  return {
    profile: () => call("GET", "getprofile"),
    symbols: () => call("GET", "getsymbols"),
    positions: () => call("GET", "getopenpositions"),
    pendingOrders: () => call("GET", "getpendingorders"),
    place: (o: { symbol: string; type: OrderType; volume: number; price?: number; sl?: number; tp?: number; stop_limit?: number }) =>
      call<{ ticket: number }>("POST", "placeorder", o),
    modify: (ticket: number, f: { price?: number; sl?: number; tp?: number; stop_limit?: number }) =>
      call("POST", "modifyorderorposition", { ticket, ...f }),
    cancel: (ticket: number) => call("POST", "cancelorder", { ticket }),
    close: (ticket: number, volume?: number) => call("POST", "squareoffposition", volume ? { ticket, volume } : { ticket }),
  };
}
```

## Use it

```ts
const api = mt5(process.env.ACCOUNT_TOKEN!);

const { ticket } = await api.place({ symbol: "EURUSD", type: OrderType.Buy, volume: 0.1, sl: 1.075, tp: 1.095 });
await api.modify(ticket, { sl: 1.085 });   // break-even
await api.close(ticket, 0.05);             // partial close
```

## Turn a webhook into an MT5 order

```ts
import express from "express";
const app = express().use(express.json());

app.post("/signal", async (req, res) => {
  const { symbol, side, volume } = req.body;
  const order = await api.place({ symbol, type: side === "buy" ? OrderType.Buy : OrderType.Sell, volume });
  res.json(order);
});

app.listen(3000);
```

Need TradingView alerts specifically? AlgoDeltaFX has a built-in [TradingView to MT5 bridge](https://www.algodeltafx.com/products/tradingview-connection) that doesn't require any code.

## FAQ

### Does this work in the browser?

Don't call the API from browser code. It would expose your token. Call it from your backend and let the browser talk to your backend.

### Does it work with Deno, Bun and Cloudflare Workers?

Yes. Any runtime with `fetch` can use the client above unchanged.

---
Source: https://docs.algodeltafx.com/guides/mt5-api-nodejs/
