Skip to main content
Back to Blog
tutorialtradingbotdemo

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.

e

eToro Developer Relations

August 15, 20253 min read

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:

  1. Searches for an instrument by symbol
  2. Checks the current rate
  3. Places a demo buy order if the rate meets a condition
  4. 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

  1. searchInstrument() calls GET /api/v1/market-data/search to find an instrument by symbol.
  2. openPosition() calls POST /api/v1/trading/execution/demo/market-open-orders/by-amount to place a buy order on the demo account. Note the PascalCase field names: InstrumentId, Amount, Leverage, IsBuy.
  3. getPortfolio() calls GET /api/v1/trading/demo/portfolio to 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/ws instead 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.

We use cookies to improve your experience. By using this site, you agree to our use of cookies. Privacy Policy (opens in new tab)