{"id":17028413,"url":"https://github.com/bennycode/trading212-api","last_synced_at":"2025-04-12T12:10:42.450Z","repository":{"id":246196886,"uuid":"810760008","full_name":"bennycode/trading212-api","owner":"bennycode","description":null,"archived":false,"fork":false,"pushed_at":"2025-03-01T01:13:59.000Z","size":1207,"stargazers_count":3,"open_issues_count":4,"forks_count":2,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-26T06:51:09.021Z","etag":null,"topics":["api","exchange","sdk","stocks","trading","trading212","typescript"],"latest_commit_sha":null,"homepage":"https://bennycode.com/trading212-api","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bennycode.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"bennycode","ko_fi":"bennycode"}},"created_at":"2024-06-05T09:57:00.000Z","updated_at":"2025-03-01T01:13:21.000Z","dependencies_parsed_at":"2024-06-26T13:52:55.479Z","dependency_job_id":"3073888d-dc3a-4a81-a6d8-86f495eae9d5","html_url":"https://github.com/bennycode/trading212-api","commit_stats":null,"previous_names":["bennycode/trading212-api"],"tags_count":7,"template":false,"template_full_name":"bennycode/ts-node-starter","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bennycode%2Ftrading212-api","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bennycode%2Ftrading212-api/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bennycode%2Ftrading212-api/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bennycode%2Ftrading212-api/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bennycode","download_url":"https://codeload.github.com/bennycode/trading212-api/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248565074,"owners_count":21125417,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api","exchange","sdk","stocks","trading","trading212","typescript"],"created_at":"2024-10-14T07:54:11.612Z","updated_at":"2025-04-12T12:10:42.429Z","avatar_url":"https://github.com/bennycode.png","language":"TypeScript","funding_links":["https://github.com/sponsors/bennycode","https://ko-fi.com/bennycode"],"categories":[],"sub_categories":[],"readme":"# Trading 212 API for TypeScript\n\n![Language Details](https://img.shields.io/github/languages/top/bennycode/trading212-api) ![License](https://img.shields.io/npm/l/trading212-api.svg) ![Package Version](https://img.shields.io/npm/v/trading212-api.svg)\n\n---\n\n## Motivation\n\nThis [Trading 212](https://www.trading212.com/) API can be used with TypeScript and comes with many useful features such as automatic reconnects and response validation.\n\n## Implemented Endpoints\n\n- [x] [Account Data](https://t212public-api-docs.redoc.ly/#tag/Account-Data)\n- [x] [Equity Orders](https://t212public-api-docs.redoc.ly/#tag/Equity-Orders)\n- [x] [Historical items](https://t212public-api-docs.redoc.ly/#tag/Historical-items)\n- [x] [Instruments Metadata](https://t212public-api-docs.redoc.ly/#tag/Instruments-Metadata)\n- [x] [Personal Portfolio](https://t212public-api-docs.redoc.ly/#tag/Personal-Portfolio)\n- [x] [Pies](https://t212public-api-docs.redoc.ly/#tag/Pies)\n\n## Features\n\n- ✅ **Documented.** Get started with [demo scripts][1] and [generated documentation][2].\n- ✅ **Maintained.** Automated security updates. No threats from outdated dependencies.\n- ✅ **Modern.** HTTP client with Promise API. Don't lose yourself in callback hell.\n- ✅ **Reliable.** Follows [semantic versioning][3]. Get notified about breaking changes.\n- ✅ **Robust.** Automatic requests retries are built-in. No problems if your Wi-Fi is gone.\n- ✅ **Tested.** Unit test coverage to guarantee core functionaly.\n- ✅ **Typed.** Source code is 100% TypeScript. No need to install external typings.\n\n## Usage\n\n### Installation\n\n**npm**\n\n```bash\nnpm install trading212-api\n```\n\n### REST Example\n\nThe [demo section][1] provides many examples on how to use \"trading212-api\". There is also an automatically generated [API documentation][2]. For a quick start, here is a simple example for a REST request:\n\n```ts\nimport {APIClient} from 'trading212-api';\n\nconst baseURL = APIClient.URL_LIVE;\nconst client = new APIClient(baseURL, 'apiKey');\n\nconst info = await client.rest.account.getInfo();\nconsole.log(info);\n```\n\n### API Key Generation\n\nIn order to generate an API key, you can follow the [official instructions](https://helpcentre.trading212.com/hc/en-us/articles/14584770928157). Basically, you need to [login](https://app.trading212.com/), click on your account and do the following:\n\n1. Click \"Switch to Practice\" in order to generate an API key for the demo code, otherwise a live API key will be generated\n2. Go to \"Settings\"\n3. Click on \"API (Beta)\"\n\n### Development\n\nIf cloning the project locally, you can also add a `.env` file to configure the API client (see [.env.defaults][4]). This allows you to run all demo scripts.\n\n**Example**\n\n```bash\nnpm run demo:account\n```\n\n## Browser API\n\n\u003e [!CAUTION] \n\u003e The Trading212 Browser API is quite unstable. Even on their web client, authentication requests sometimes fail while other requests succeed.\n\nThe official Trading212 API does not support placing orders in a live environment. To address this, the library includes an experimental Browser API that uses a headless Chrome browser for programmatic trading.\n\nThe Browser API will need to log in with your username and password, so ensure you set the following environment parameters in your `.env` file:\n\n```bash\nTRADING212_HEADLESS_BROWSER=false\nTRADING212_EMAIL=name@mail.com\nTRADING212_PASSWORD=secret\n```\n\nThis technique will log in locally using your credentials and save them in the \"[credentials](./credentials/)\" directory. This prevents unnecessary re-logins, as the login token remains valid for the session.\n\nHere's how to use the Browser API:\n\n```ts\nimport {initClient} from 'trading212-api';\n\nconst client = await initClient();\n\nconst accountSummary = await client.browser.accounts.getSummary();\nconsole.log(accountSummary.cash.free);\n```\n\nLocally you can test it with:\n\n```bash\nnpm start\n```\n\n### Discoveries\n\nThe Trading212 Browser API will show this error when submitting an empty object (`{}`):\n\n\u003e data: { code: 'InternalError' }\n\nTo fix it, we have to submit an empty array (`[]`):\n\n```ts\nawait axios.post\u003cAccountSummary\u003e(ACCOUNT_SUMMARY_URL, [], {\n  headers: {\n    ...auth.headers,\n    Cookie: toCookieString(cookies),\n    'User-Agent': getUserAgent(),\n    'X-Trader-Client': `application=WC4, version=1.0.0, dUUID=${duuid}`,\n  },\n});\n```\n\nWhen the \"credentials\" are wrong or expired, the API will show:\n\n```json\n{\n  \"code\": \"AuthenticationFailed\",\n  \"context\": {\n    \"type\": \"InvalidSession\"\n  },\n  \"errorMessage\": \"Invalid account session cookie\"\n}\n```\n\nIn such cases, simply delete the \"[credentials](./credentials/)\" directory and try again.\n\n### Inspiration\n\nThe Browser API is build on findings from the article \"[I Reverse-Engineered Trading212’s Web APIs ](https://haksoat.com/reverse-engineering-trading212/)\". You can find the matching Python code in the [TradingTOT library](https://github.com/HAKSOAT/tradingTOT) on GitHub.\n\n## Internals\n\nThis library utilizes [axios](https://github.com/axios/axios) for HTTP calls. You can configure the axios instance using [interceptors](https://axios-http.com/docs/interceptors) if needed. Retries are handled by [axios-retry](https://github.com/softonic/axios-retry), and payloads are validated with [Zod](https://github.com/colinhacks/zod). Unit tests are implemented with [nock](https://github.com/nock/nock) and the headless browser is controlled via [Playwright](https://playwright.dev/).\n\n## Contributions\n\nHere are some best practices PRs that show how to add endpoints:\n\n- [Add Pie API](https://github.com/bennycode/trading212-api/pull/6/files)\n\n## Resources\n\n### Documentation\n\n- [Docs: Trading212 API](https://t212public-api-docs.redoc.ly/)\n- [Forum: New Equity Trading API in Beta](https://community.trading212.com/t/61788)\n- [Blog: Reverse-Engineered Trading212’s Web APIs](https://haksoat.com/reverse-engineering-trading212/)\n\n### Tools\n\n- [JSON to Zod Schema](https://transform.tools/json-to-zod)\n- [OpenAPI Generator: Global Properties](https://openapi-generator.tech/docs/globals/)\n- [OpenAPI Generator: Config Options](https://openapi-generator.tech/docs/generators/typescript-fetch/#config-options)\n\n### Others\n\n- [Python-based Trading212 API](https://github.com/HAKSOAT/tradingTOT)\n\n[1]: https://github.com/bennycode/trading212-api/tree/main/src/demo\n[2]: https://github.com/bennycode/trading212-api/tree/main/docs\n[3]: https://docs.npmjs.com/about-semantic-versioning\n[4]: https://github.com/bennycode/trading212-api/blob/main/.env.defaults\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbennycode%2Ftrading212-api","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbennycode%2Ftrading212-api","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbennycode%2Ftrading212-api/lists"}