Getting started with API trading
An API lets your own code do what you would otherwise do by clicking in a broker's app: read prices, read your positions, and place or close trades. This guide explains how that works and what to sort out before you write a line of code.
Risk warning: CFDs and spread bets are complex instruments and come with a high risk of losing money rapidly due to leverage. You can lose money on these products, and each provider publishes its own loss figure under FCA rules. You should consider whether you understand how they work and whether you can afford to take the high risk of losing your money. Automated and API trading can amplify losses through bugs, connectivity failures and runaway orders.
What an API is
When you tap Buy in a trading app, the app sends a small message to the broker's computers and shows you the reply. An API is that same conversation, opened up so a program you control can have it instead.
Your program sends a request to an address called an endpoint. The request says what you want, such as your open positions, and proves who you are. The broker sends back a reply as structured text that code can read. That is the whole idea. Everything else is detail about which endpoints exist, how you prove who you are, and how often you may ask.
People use it to export their trade history, to keep a journal up to date without typing, to watch positions and send themselves alerts, to test a rule on past prices, and to place orders by rule. Most start with reading, not trading, and that is the right order.
What you need before you start
- An account with a broker that offers an API, or that offers a trading platform with one.
- A demo environment to practise in. Start there and stay there until your code has run for weeks without a surprise.
- An API key or login for that environment, kept out of your code.
- A computer with Python installed. Other languages work too. Python has the most examples.
- Patience with errors. Every first attempt fails somewhere, and the error code tells you where.
Is this for you at all?
If you trade on a phone app and have never written code, read Is API trading for you? first: one screen on who should not bother, what the realistic first win is, what the first request costs and what this is not.
Two routes: the broker's own API, or a platform API
Some brokers build their own API. Code written for it works with that broker only, but it usually reaches everything the broker offers.
Other brokers offer trading software made by someone else, and that software has an API of its own. Code written for a platform API can move with you between the brokers that offer the platform, which matters if you may change broker later.
Platform APIs, and how many brokers here offer each
- MetaTrader 4 (MQL4)Offered by 12 of the brokers on this register. MetaQuotes' FX terminal; automation is MQL4 code running inside the terminal.
- MetaTrader 5 (Python package and MQL5)Offered by 8 of the brokers on this register. MetaQuotes' multi-asset trading terminal. MQL5 programs run inside it, and the MetaTrader5 Python package talks to it locally.
- cTrader Open APIOffered by 2 of the brokers on this register. Spotware's open API for cTrader accounts, JSON or Protobuf over TCP or WebSocket.
Keep your key safe
An API key is a password for a program. Anyone holding it can do whatever the key allows, and on a live account that can mean placing trades.
- Never write a key into a script, a notebook, a screenshot or a chat window. Set it as an environment variable on your own machine and let the script read it from there.
- Never type a key into a website that is not your broker's own. This site has no field that asks for one.
- Keep demo and live keys apart, and name them so you cannot confuse them.
- If a key may have leaked, revoke it at the broker straight away and make a new one.
The keys and demo accounts lesson shows how to set an environment variable on each operating system.
Limits: how often you may ask
Every broker limits how many requests you may send. The limits are usually several separate figures, each with its own scope: one for an application, one for an account, one for trading calls, one for price history. A program that loops without pausing can use a week's allowance in seconds, and repeated breaches can get a key suspended.
As a worked example, these are IG's published limits as held on this site's register. Where a broker's own pages disagree, pace against the lower figure.
| Limit | Value | Scope | Source |
|---|---|---|---|
| Historical price data points a week | 10,000 | Per account, price history endpoints only | labs.ig.comchecked |
| Non trading requests a minute | 30 | Per account, non trading calls only | labs.ig.comchecked |
| Non trading requests a minute | 60 | Per application, that is per API key, non trading calls only | labs.ig.comchecked |
| Concurrent streaming subscriptions | 40 | Per streaming connection | labs.ig.comchecked corroborated by labs.ig.com/streaming-api-guide.html |
| Trading requests a minute | 100Sources conflict ig.com states around 40, checked . IG's own pages disagree. The conflict is labelled, not resolved. Pace against 40. | Per account, trading calls only | labs.ig.comchecked |
Each broker page lists the limits recorded for that broker.
Your first call
Start where no account is needed. This site's practice API answers with invented data and asks for no key, so a first request can go wrong without any cost. The lesson walks through it in Python.
Make a first request to the practice API
Then make the same kind of request to a broker, as a read, on a demo account: log in, then ask for your open positions. It cannot place a trade, and it proves that your key, your login and your code all work. That step needs a broker demo login.
Follow the first API call lesson, on a broker demo
When a call fails, the reply carries an error code. Look it up rather than guessing: there are 1,776 recorded on the errors pages.
Brokers on this register with an API of their own
How you get a key at each, as the broker's own pages state it. Lower down the page on purpose: pick a broker after the first call, not before.
| Broker | How you get access | Source |
|---|---|---|
| Bitget | Log in to the Bitget website and create an API key under API Key Management. Each key set is an API key, a secret key and a passphrase you choose, with read or read and write permissions, and one UID can hold up to 50 key sets. Bitget recommends binding the key to an IP address. | Bitget API quick startchecked |
| Capital.com | Enable 2FA, then Settings > API integrations > Generate new key, with a custom key password. | Capital.com Public APIchecked |
| Deriv | Two kinds of app. With an OAuth 2.0 app the user signs in and consents at Deriv, and the app exchanges the code it gets back for an access token. With a Personal Access Token app the user generates a token in Deriv and enters it into the app. An earlier wording here (register an app_id in Manage Applications, or try the test app_id 1089) is legacy: it is not on Deriv's authentication page as read on 12 Oct 2026. | Deriv API: authenticationchecked |
| Dukascopy | Two routes of Dukascopy's own. The JForex API is a Java client library, shipped as the JForex SDK, that connects straight to Dukascopy's trade servers without the JForex platform running. The FIX API, on FIX 4.4, is described as for professional market participants: Dukascopy issues a TargetCompId, two SenderCompIds, a username and a password, and connections must come from IP addresses and ports agreed beforehand. The FIX page states no minimum deposit. | Dukascopy JForex APIchecked |
| eToro | Settings > Trading > API Key Management > Create New Key; choose Read or Write, optional IP whitelist and expiry. | eToro API portal: authenticationchecked |
| FOREX.com / StoneX Trading (City Index) | Open and fund a standard account, then request API access from support; an AppKey is emailed if accepted. | FOREX.com API Tradingchecked |
| FXCM | Free SDKs from FXCM GitHub; sign the EULA and apply for a demo account. Support at api@fxcm.com. | GitHub fxcm/ForexConnectAPIchecked |
| IG | API keys are only issued to existing live account holders: generate one in My IG > Settings > API keys. | How to use IG APIchecked |
| Interactive Brokers | No key: run TWS or IB Gateway (TWS API) or the Client Portal Gateway and log in via browser (Web API). | IBKR TWS API documentation, the IB Gatewaychecked |
| OANDA | Account Management Portal > My Services > Manage API Access, accept licence, generate personal access token. | OANDA v20 Introductionchecked |
| Pepperstone | API by application. You need a live Pepperstone account, then you email support to request one of two routes. The Standard API: once approved you generate an API key to connect your software. The FIX API: for account holders with a minimum trading volume of 160 million euro a month, or the local currency equivalent. The page publishes no protocol, documentation or eligibility test for the Standard API. | Pepperstone API tradingchecked |
| Saxo OpenAPI | Create a developer portal account for SIM (24 hour token or registered app); apply for live app credentials after testing. | Saxo OpenAPI Learnchecked |
| Swissquote | Swissquote's APIs are presented on its institutional pages, which you reach by contacting a Swissquote office, not through a self service key. Three trading APIs are named there: a REST API on the OpenWealth standard for securities, cryptocurrencies and FX spot, a FIX API for securities and cryptocurrencies, and a dedicated FIX API for leveraged forex whose stated client types are corporates, banks and retail brokers. Separately, the platform comparison table on Swissquote's UK private client pages lists FIX API as a feature of the CFXD platform and not of MetaTrader 4 or MetaTrader 5, which carry Expert Advisors instead. That table does not say how a private client applies for FIX access. | Swissquote institutional, securities and cryptos via an APIchecked |
| Trading 212 | On the web or in the app: menu, Settings, API (Beta), Generate API key. A risk warning must be accepted first. You then name the key, choose its permissions and choose whether to restrict it to given IP addresses. A key and a secret are issued, and the secret is shown once. | Trading 212 help centre: Trading 212 API keychecked |
Before you let code place a trade
- Run it on demo for long enough to see a weekend, a market close and at least one outage.
- Give every order a stop, and cap the size your code is allowed to send.
- Decide what happens when the connection drops half way through an order, and test that case on purpose.
- Log every request and reply, so that when something goes wrong you can see what was actually sent.
- Keep a way to switch it off that does not depend on the program working.
None of this is advice about what to trade. It is the engineering care that automated orders need.
Common questions
Do I need to be a programmer to trade through an API?
No, but you need to be willing to learn a little. Reading your own positions takes a short script. The learning track on this site starts from no experience at all.
Is API trading free?
It depends on the broker. Some include API access with an ordinary account and some charge for it or require a minimum balance or volume. Check the access line on the broker's page here, which carries the source it was read from.
Can I test without risking money?
Usually, yes. Many brokers offer a demo environment for their API. Whether a demo key is available, and whether you need a live account first, differs by broker and is listed in the table above.
Is it safe to give an API key to a tool or a website?
Treat a key like the password to your account. Keep it in an environment variable on your own machine, never paste it into a website that is not your broker's, and prefer keys that can only read where a broker offers them. This site never asks for one.
What is the difference between a broker API and a platform API?
A broker API is built by one broker for its own accounts. A platform API, such as the MetaTrader or cTrader interface, belongs to trading software that many brokers offer, so code written for it can move between those brokers.
Where to go next
- Your first Python script: profit and loss on one tradeWrite and run a script that works out the profit or loss on one trade. It introduces variables, arithmetic, an if statement and print.
- Variables and types: what a value can beMeet the four kinds of value every script is made of, see why text does not do arithmetic, and convert between them. It introduces str, int, float, bool and f-strings.
- If statements and comparisons: let the program decideAsk a question in code and do different things depending on the answer. It introduces comparisons, if, elif and else, and the words and, or and not.
The whole learning track, or go straight to your broker, the other guides or a side by side comparison of two APIs.