FAQ and troubleshooting
Getting started
Section titled “Getting started”Do I need TypeScript?
Section titled “Do I need TypeScript?”No. The library works in plain JavaScript; every example has a JavaScript tab. It ships its types, so editors such as VS Code still show every option and field as you type.
Does it work with require()?
Section titled “Does it work with require()?”Yes. Every JavaScript example in these docs is CommonJS: const { Trading212 } = require("@kkkermit/trading212.js"),
with the code that waits for Trading 212 inside an async function main() { … }, because CommonJS has no top-level
await. Save it as a .cjs file and run it with node.
Can I use it in a browser, or in React?
Section titled “Can I use it in a browser, or in React?”Not directly. Trading 212 refuses requests from web pages, and a key in a page is public, so the client refuses to start in a browser. Call it from your server, such as an API route, and have the page call that.
Which Node version do I need?
Section titled “Which Node version do I need?”Node 22 or later. Bun and Deno work too.
Can I try it without a Trading 212 account?
Section titled “Can I try it without a Trading 212 account?”Yes. The fake Trading 212 is the real client talking to an in-memory account; the first steps of Getting started use it.
Errors you may meet first
Section titled “Errors you may meet first”ConfigurationError: T212_ENV is not set
Section titled “ConfigurationError: T212_ENV is not set”Trading212.fromEnv() reads T212_ENV, T212_API_KEY and T212_API_SECRET, and Node does not load .env by
itself. Run with node --env-file=.env app.cjs, or set the variables in your shell or host.
AuthenticationError
Section titled “AuthenticationError”Trading 212 refused the key and secret. The usual causes, in order:
- The key was made for the other environment: a practice-mode key works on demo only, and a real-money key on live only.
- The secret was pasted into
apiKey, or a space was copied with it. The client catches most of these and says so. - The key was deleted in the app.
npx @kkkermit/trading212.js doctor --env-file .env checks all of these at once.
MissingScopeError
Section titled “MissingScopeError”The key works but lacks a permission; error.scope names it. Make a new key in the app with that permission ticked.
API coverage lists which method needs which.
TradingDisabledError
Section titled “TradingDisabledError”The client only reads, which is the default on live. Create it with trading: "enabled" once you are ready, and
preview orders with { dryRun: true } first.
OrderRejectedError
Section titled “OrderRejectedError”Trading 212 refused the order and nothing was traded. error.reason says why, and error.hint what to do: not
enough cash, selling more than you can, a closed market, or more than 4 decimal places.
OrderOutcomeUnknownError
Section titled “OrderOutcomeUnknownError”The order may or may not have gone through: it timed out, or the connection dropped after it was sent. Check
orders.list() and history.orders.list() before trying again; the
safe resend recipe shows how.
RateLimitError
Section titled “RateLimitError”A call would have had to wait longer than maxRateLimitWaitMs (60 s by default) for its budget. Call less often,
or raise the limit. Remember every program using the same account shares one budget.
NotAvailableInDemoError
Section titled “NotAvailableInDemoError”The feature exists on live only; CSV exports are the expected case.
How do I…
Section titled “How do I…”…buy an amount of money’s worth, such as £50 of Apple?
Section titled “…buy an amount of money’s worth, such as £50 of Apple?”The API only takes a number of shares. You can work one out from a price you already know, and round it down with
truncateQuantity(50 / price).
…sell everything I hold of one stock?
Section titled “…sell everything I hold of one stock?”await t212.orders.sellAll("AAPL_US_EQ"). It sells what is available, leaving shares in pies alone.
…see what the client is doing?
Section titled “…see what the client is doing?”Pass logger: "debug", or set T212_LOG=debug when using Trading212.fromEnv(). See
Logging and hooks.
…know when an order fills?
Section titled “…know when an order fills?”orders.waitFor(order) waits for one order. To hear about every order, including ones placed in the app, use
t212.watch.orders(); see Watching the account.
…keep my key safe?
Section titled “…keep my key safe?”Keep it in .env or your host’s secret settings, never in code, and add .env to .gitignore. Give each program
its own key with only the permissions it needs, and restrict keys to fixed IP addresses where you can. If a key
leaks, delete it in the app straight away.
…find a ticker?
Section titled “…find a ticker?”await t212.instruments.resolve("Apple"). See Instruments and market hours.
…report a bug?
Section titled “…report a bug?”Open an issue on GitHub with the smallest code that shows it,
ideally using the fake Trading 212, and the error’s toLog() output. Never include your key or secret.