Building Your First Trading Bot with the eToro API
A step-by-step guide to building a simple trading bot using the eToro Demo Trading API.
eToro Developer Relations
Trading bots don't have to be complicated. In this post, we'll build a simple bot that monitors market data and places trades on the eToro demo account — all in under 50 lines of code.
What We're Building
A Node.js script that:
- Searches for an instrument by symbol
- Checks the current rate
- Places a demo buy order if the rate meets a condition
- Logs the result
Setup
Make sure you have your API keys ready (see Getting Started). Set them as environment variables:
export ETORO_API_KEY="your-api-key"
export ETORO_USER_KEY="your-user-key"
The Bot
const crypto = require('crypto');
const BASE = 'https://public-api.etoro.com/api/v1';
function getHeaders() {
return {
'x-api-key': process.env.ETORO_API_KEY,
'x-user-key': process.env.ETORO_USER_KEY,
'x-request-id': crypto.randomUUID(),
'Content-Type': 'application/json',
};
}
async function searchInstrument(symbol) {
const res = await fetch(
`${BASE}/market-data/search?internalSymbolFull=${symbol}`,
{ headers: getHeaders() }
);
return res.json();
}
async function openPosition(instrumentId, amount) {
const res = await fetch(
`${BASE}/trading/execution/demo/market-open-orders/by-amount`,
{
method: 'POST',
headers: getHeaders(),
body: JSON.stringify({
InstrumentId: instrumentId,
Amount: amount,
Leverage: 1,
IsBuy: true,
}),
}
);
return res.json();
}
async function getPortfolio() {
const res = await fetch(`${BASE}/trading/demo/portfolio`, {
headers: getHeaders(),
});
return res.json();
}
async function main() {
// 1. Find the instrument
const search = await searchInstrument('AAPL');
console.log('Search results:', search);
// 2. Place a demo order
// Use the instrumentId from your search results
const order = await openPosition(1001, 500);
console.log('Order result:', order);
// 3. Check portfolio
const portfolio = await getPortfolio();
console.log('Portfolio:', portfolio);
}
main().catch(console.error);
How It Works
searchInstrument()callsGET /api/v1/market-data/searchto find an instrument by symbol.openPosition()callsPOST /api/v1/trading/execution/demo/market-open-orders/by-amountto place a buy order on the demo account. Note the PascalCase field names:InstrumentId,Amount,Leverage,IsBuy.getPortfolio()callsGET /api/v1/trading/demo/portfolioto check your positions and P&L.
Each function generates a fresh x-request-id UUID for every call, as required by the API.
Going Further
This is a starting point. From here you could:
- Add sell logic by calling
POST /api/v1/trading/execution/demo/market-close-orders/positions/{positionId} - Subscribe to real-time prices via
wss://ws.etoro.com/wsinstead of polling - Add limit orders with
POST /api/v1/trading/execution/demo/market-if-touched-orders - Build a simple strategy using historical candle data from
GET /api/v1/market-data/instruments/candles
Rate Limits
Remember: GET requests are limited to 60/minute, and POST/PUT/DELETE to 20/minute. Build in appropriate delays if you're running loops.
Ready to Go Live?
When your demo bot works correctly, change /demo/ to /real/ in your endpoint paths. Make sure your API key has Real environment permissions.
Check the Demo Trading Guide for the full trading API reference, or browse the API documentation for all available endpoints.