@glitchmarkets/sdk
v0.4.0
Published
The Glitch API for TypeScript and JavaScript: typed requests, safe retries, trade helpers and a resuming stream.
Maintainers
Readme
@glitchmarkets/sdk
The official JavaScript and TypeScript client for the Glitch API. Use it to read MetaTrader 5 accounts, place and manage trades, and follow prices and positions live, from Node.js, Deno, Bun or the browser.
npm install @glitchmarkets/sdkNeeds Node.js 22 or later. The package is ESM only and ships its own types.
Place a trade
Create an API key on the Developers page in Glitch. A sandbox key trades demo accounts only, so start with one.
import { Glitch, GlitchError } from "@glitchmarkets/sdk";
const glitch = new Glitch({ apiKey: process.env.GLITCH_API_KEY! });
try {
const result = await glitch.trading("acct_...").market({
symbol: "EURUSD",
side: "buy",
volume: "0.10",
stopLoss: "1.08340",
wait: true, // wait up to 10 seconds for the broker's answer
});
console.log(result.status);
} catch (error) {
if (error instanceof GlitchError) console.error(error.code, error.detail);
else throw error;
}Prices and volumes are strings, so no decimal is ever rounded on the way.
glitch.trading(accountId) also has preview, close, modify and
cancel.
Call any endpoint
glitch.api has one typed call for every documented endpoint, with
editor completion for paths, parameters and answers:
const { data, error } = await glitch.api.GET("/accounts/{account_id}", {
params: { path: { account_id: "acct_..." } },
});The types for every request and answer are exported as components and
paths.
Follow events live
const stream = glitch.stream(["account.acct_....positions"]);
for await (const event of stream) {
if (event.type === "resync_required") {
// reload this channel over the REST API, then carry on
} else {
console.log(event.channel, event.seq, event.data);
}
}The stream reconnects by itself and picks up after the last event it gave
you, so nothing is missed or repeated. stream.close() ends it.
Safe retries
Network errors and answers 429, 502, 503 and 504 are sent again, up to
maxRetries times (3 by default). Every change carries one
Idempotency-Key across its retries, so a trade is placed at most once. Pass
your own idempotencyKey to a trade to keep it safe across restarts of your
program too.
Errors
A refused request throws GlitchError with the HTTP status, the API's
stable code (such as INSUFFICIENT_MARGIN), a readable detail and the
requestId to quote to support. Branch on code, never on detail.
Let traders use your app
If your app acts for other traders, ask each one to allow it. First get a client ID: in the Glitch app, open Developers, choose your app and fill in Let traders use your app. If your app has its own server, pick "It has its own server" to get a client secret too, shown once; your server sends it when it swaps the code for tokens. Add the page traders come back to as a redirect address.
In the browser, allowOnGlitch opens Glitch's allow screen in a popup, where the
trader signs in and picks what your app may do. Your page never sees their
password.
import { allowOnGlitch } from "@glitchmarkets/sdk";
button.onclick = async () => {
const allowed = await allowOnGlitch({
clientId: "appc_...",
redirectUri: "https://your.app/glitch-allowed",
scope: ["trade"],
});
// Send allowed.code, allowed.codeVerifier and allowed.redirectUri to your
// backend, which trades them for tokens at POST /oauth/token.
};Call it from a click, or the browser blocks the popup. For React Native, use
@glitchmarkets/connect-react-native.
More
- API reference: https://developers.glitchmarkets.com/reference
- Licence: MIT
