Configuration
Client options
Section titled “Client options”const t212 = new Trading212({ environment: "demo", apiKey, apiSecret, ...rest });| Option | Default | What it does |
|---|---|---|
environment |
required | "demo" or "live" |
apiKey, apiSecret |
required | Checked for whitespace, swaps and other copy-paste slips |
trading |
enabled on Trading 212’s demo host, otherwise disabled | Whether orders, cancellations and pie changes may be sent |
timeoutMs |
15000 |
Per request; override per call with { timeoutMs } |
retry |
{ maxAttempts: 3, baseDelayMs: 500, maxDelayMs: 10000 } |
Reads only; false turns retries off |
rateLimit |
"pace" |
"pace", "burst" or "off"; see Rate limits |
maxRateLimitWaitMs |
60000 |
Throw RateLimitError rather than wait longer than this |
logger |
silent | A logger, or a level such as "debug"; see Logging |
hooks |
none | onRequest, onResponse, onRetry, onRateLimitWait |
cache, metadataTtlMs |
memory, 1 hour | Where the instrument and exchange lists are kept, and for how long |
fetch |
the global fetch |
For proxies, tracing and tests |
baseUrl |
Trading 212’s host | Tests and proxies only; https://, or http://localhost |
userAgent |
none | Appended to the trading212.js/<version> user agent |
dangerouslyAllowBrowser |
false |
Lets the client start in a browser; see the warning below |
Durations are milliseconds. A fraction is rounded up to a whole millisecond; zero, a negative number or a value that
is not a number throws ConfigurationError.
Environment variables
Section titled “Environment variables”Trading212.fromEnv(overrides?) and the doctor read these. The library never reads a .env file itself; load one
with node --env-file=.env or your framework.
| Variable | What it is |
|---|---|
T212_ENV |
demo or live, required, with no default |
T212_API_KEY, T212_API_SECRET |
The key and secret for the environment T212_ENV names |
T212_DEMO_API_KEY, T212_DEMO_API_SECRET |
Used on demo when the plain variables are unset or blank |
T212_LIVE_API_KEY, T212_LIVE_API_SECRET |
Used on live when the plain variables are unset or blank |
T212_LOG |
Optional: debug, info, warn or error to log there |
fromEnv() takes any other option as overrides, such as Trading212.fromEnv({ trading: "enabled" }).
Options on every call
Section titled “Options on every call”Every method takes a last argument of call options:
| Option | What it does |
|---|---|
signal |
An AbortSignal that cancels the call. A trade aborted after it was sent throws OrderOutcomeUnknownError |
timeoutMs |
This call’s timeout, in place of the client’s |
import { Trading212 } from "@kkkermit/trading212.js";
const t212 = Trading212.fromEnv();
// Give up on one call after 5 seconds, whatever the client's own timeout.const summary = await t212.account.summary({ timeoutMs: 5_000 });console.log(summary.totalValue);
// Or cancel calls yourself, such as when your program shuts down.const controller = new AbortController();process.once("SIGINT", () => controller.abort());
for await (const order of t212.history.orders.all({ signal: controller.signal })) { console.log(order.order?.id);}const { Trading212 } = require("@kkkermit/trading212.js");
async function main() { const t212 = Trading212.fromEnv();
// Give up on one call after 5 seconds, whatever the client's own timeout. const summary = await t212.account.summary({ timeoutMs: 5_000 }); console.log(summary.totalValue);
// Or cancel calls yourself, such as when your program shuts down. const controller = new AbortController(); process.once("SIGINT", () => controller.abort());
for await (const order of t212.history.orders.all({ signal: controller.signal })) { console.log(order.order?.id); }}
main().catch((error) => { console.error(error); process.exitCode = 1;});The instrument and exchange lists take signal and refresh but not timeoutMs: one download is shared by every
caller, so it uses the client’s own timeout. orders.waitFor() and reports.waitFor() read timeoutMs as the
longest to wait in total, and also take intervalMs, the time between checks.