JavaScript / Node.js · REST + WebSocket · NinjaTrader 8

JavaScript / Node.js NinjaTrader 8 API examples

Integrate NinjaTrader 8 into Node.js and browser apps. Pull positions, place orders, and stream live market data with fetch + ws.

Why JavaScript / Node.js

Using JavaScript / Node.js against the CrossTrade NinjaTrader API

JavaScript is the right fit when CrossTrade is one piece of a larger system (web dashboard, Discord bot, Electron tool). Node 18+ has native fetch — no axios needed. For WebSocket, the ws package is the de facto standard server-side; in the browser, the built-in WebSocket works directly.

REST fetch (native in Node 18+)
WebSocket ws (npm install ws)
REST · animated

Watch the request get written

A pseudo-developer types out a real GET GetPositions call against your NinjaTrader 8 account. The animation runs on a loop; the static snippet below is yours to copy.

getpositions.js
Copy this

Complete JavaScript / Node.js GetPositions example

const TOKEN = "your-bearer-token";
const url = "https://app.crosstrade.io/v1/api/accounts/Sim101/positions";

const res = await fetch(url, {
  headers: { "Authorization": `Bearer ${TOKEN}` },
});
const data = await res.json();

data.positions.forEach((p) => {
  console.log(p.instrument, p.quantity, p.unrealizedProfitLoss);
});
// ES 12-25 4 3287.5
WebSocket · animated

Streaming data with ws (npm install ws)

Same flow on a persistent connection: open the socket, send a subscribe message, receive market data or P&L pushes in real time. CrossTrade's WebSocket is at wss://app.crosstrade.io/ws/stream with the same Bearer token in the request header.

stream.js
Copy this

Complete JavaScript / Node.js WebSocket example

import WebSocket from "ws";

const ws = new WebSocket("wss://app.crosstrade.io/ws/stream", {
  headers: { Authorization: "Bearer YOUR_TOKEN" },
});

ws.on("open", () => {
  ws.send(JSON.stringify({
    action: "subscribe",
    instruments: ["ES 12-25", "NQ 12-25"],
  }));
});

ws.on("message", (raw) => {
  const msg = JSON.parse(raw);
  if (msg.type === "marketData") {
    msg.quotes.forEach((q) => console.log(q.instrument, q.last));
  }
});

The full JavaScript / Node.js-compatible API surface

Every endpoint documented with request/response schemas, error codes, rate-limit notes, and language-specific code samples.

FAQ

Common questions

Is JavaScript / Node.js a good choice for algo trading?

Yes, particularly when CrossTrade is one piece of a larger system: a web dashboard, a Discord/Slack bot, an Electron desktop tool, or a Cloudflare Worker. Node's event-loop model fits WebSocket streaming well, and the npm ecosystem covers every adjacent need (charting, persistence, scheduling).

Can I run this in a browser instead of Node?

WebSocket: yes, the browser's native WebSocket works directly with the same Bearer header. REST: depends on CORS — CrossTrade's REST endpoints currently expect server-side calls, so a browser-side fetch may be blocked. The standard fix is to proxy REST calls through your own backend.

Should I use axios or fetch?

Both work great. Node 18+ has built-in fetch (the example uses it for zero dependencies). axios is excellent if your codebase already uses it and you want interceptors, automatic JSON handling, and request cancellation. Either approach sends the same Authorization: Bearer header and JSON body.

Bun and Deno support?

Yes for both. Bun ships with built-in fetch + WebSocket that are drop-in compatible with the Node examples (just remove the ws import — Bun has WebSocket as a global). Deno is similar; use Deno.serve and the standard WebSocket API. Identical bearer header pattern.

What about TypeScript? Are there type definitions?

You can use TypeScript today by defining response shapes inline or generating types from CrossTrade's OpenAPI spec using openapi-typescript. Most trading bots get by with a small set of hand-written interfaces — the response shapes are stable and small.

WebSocket reconnect strategy?

Use exponential backoff: 1s, 2s, 4s, 8s, cap at 30s, reset after a connection lives more than 60s. Wrap the WebSocket in a class that handles "close" events and replays the subscribe message on reconnect. The docs/api/websocket-api page covers the recommended pattern.

Can this run on Cloudflare Workers or Vercel Edge?

Yes for REST-only workflows — place orders from a webhook handler and return immediately. For live WebSocket streaming, edge runtimes don't allow long-lived connections inside a request handler, so run Node on a VPS or a small container for the streaming side.

Parallel calls with Promise.all?

Yes — CrossTrade's 3 req/sec rate limit is shared per token but bursts up to 20. So Promise.all([listAccounts(), listPositions(), getJournal()]) is well within the budget for a typical dashboard load.

How fast is Node for trading?

Node's V8 engine is JIT-compiled and competitive with Java or Go for typical trading workloads. CrossTrade calls are network-bound (~50ms round-trip), so language overhead is essentially invisible. Node particularly shines when you need to fan one WebSocket out to many connected web clients.

Build your first NinjaTrader 8 trading bot in JavaScript / Node.js

One bearer token, one URL, every NinjaTrader endpoint. Ship to Sim101 first.