Getting started overview

To begin interacting with IG's trading platform programmatically, you first need to establish an account and enable API access. This involves a sequence of steps that range from basic account creation to generating API keys and performing your initial authenticated request. The IG REST API, documented on the IG Labs REST API reference, facilitates access to market data, trading commands, and account management functions.

The core process involves:

  1. Account Creation: Registering for an IG trading account.
  2. API Key Generation: Obtaining the necessary credentials for API access from the My IG dashboard.
  3. Authentication: Establishing a session with the API using your login credentials and API key.
  4. First Request: Executing a simple data retrieval call to confirm successful integration.

IG offers various trading products, including CFD trading and forex trading, with specific charges detailed on their IG pricing page.

Quick reference guide

The following table provides a high-level overview of the steps involved in getting started with the IG API:

Step What to do Where
1. Open an account Register for a live or demo trading account. IG homepage
2. Enable API access Navigate to 'My IG' and activate API access. My IG Dashboard (after login)
3. Generate API Key Create a unique API key for application identification. My IG Dashboard > API Keys section
4. Authenticate Send a POST request to the /session endpoint with credentials and API key. IG API authentication documentation
5. Make First Call Use the returned session token to query market data. IG API markets endpoint

Create an account and get keys

To access the IG API, you must first have an active IG trading account. This can be either a live account for real-money trading or a demo account for testing purposes without financial risk. The registration process typically involves providing personal details and completing identity verification as required by financial regulations, such as those enforced by the UK Financial Conduct Authority (FCA), under which IG is regulated in the UK.

Account registration

  1. Navigate to the IG Group homepage.
  2. Select 'Create live account' or 'Create demo account' based on your needs.
  3. Follow the on-screen prompts to input your personal information, financial details, and complete any required verification steps.
  4. Once your account is approved and verified, you will gain access to the My IG dashboard.

Enabling API access and key generation

After successfully registering and logging into your IG account:

  1. Log in to your My IG dashboard.
  2. Locate the 'API Keys' or 'Developer API' section, usually found under 'Settings' or 'Account Management'.
  3. Enable API access if it's not already active. This step might require agreement to specific terms and conditions for API usage.
  4. Generate a new API key. This key is a unique identifier for your application and should be kept confidential. IG's documentation advises against sharing API keys and recommends secure storage practices.
  5. Make a note of your API key, as it will be required for all subsequent API requests.

Your first request

The IG REST API uses a two-step authentication process: first, you authenticate with your login credentials and API key to obtain a session token, and then you use this session token for subsequent requests. All API requests should be made to the appropriate base URL (e.g., https://api.ig.com/gateway/deal for live accounts or https://demo-api.ig.com/gateway/deal for demo accounts), followed by the specific endpoint path.

Authentication request

To authenticate, send a POST request to the /session endpoint. You will need to include your API key in the X-IG-API-KEY header and your username, password, and CST (Client Session Token, initially empty or 'null') in the request body. The server will respond with a CST (Client Session Token) and X-SECURITY-TOKEN in the headers, which are crucial for subsequent calls.

POST /session HTTP/1.1
Host: demo-api.ig.com
X-IG-API-KEY: YOUR_API_KEY
Content-Type: application/json
Accept: application/json

{
  "identifier": "YOUR_USERNAME",
  "password": "YOUR_PASSWORD",
  "encrypted": "false"
}

A successful response will include CST and X-SECURITY-TOKEN headers. Extract these values for use in future requests. They act as your session credentials, similar to how OAuth tokens function for authorized access.

Making a market data request

Once authenticated, you can make your first data request, for example, to retrieve a list of available markets. Use the CST and X-SECURITY-TOKEN obtained from the session response in the headers of your subsequent requests.

GET /markets HTTP/1.1
Host: demo-api.ig.com
X-IG-API-KEY: YOUR_API_KEY
CST: YOUR_CST_TOKEN
X-SECURITY-TOKEN: YOUR_SECURITY_TOKEN
Accept: application/json

This request should return a JSON array of market data, confirming that your API key and session tokens are correctly configured and that you can successfully query the IG API.

Common next steps

After successfully completing your first API call, you can explore various functionalities offered by the IG API. Key areas include:

  • Streaming Data: Implement real-time market data streaming using WebSockets for live price updates and trade confirmations. Refer to the IG Stream API documentation for details on setting up a streaming connection.
  • Placing Orders: Develop logic for opening, modifying, and closing positions. This involves understanding the various order types (e.g., market, limit, stop) and their respective parameters, as outlined in the positions endpoint documentation.
  • Account Management: Retrieve account balances, open positions, and transaction history to monitor your trading activity. The account details endpoint provides access to this information.
  • Error Handling: Implement robust error handling mechanisms to gracefully manage API rate limits, authentication failures, and other potential issues. Consult the IG API response codes for a comprehensive list of error messages and their meanings.
  • SDKs and Libraries: While IG does not provide official SDKs, community-contributed libraries in various programming languages may exist to simplify interaction with the API. Always verify the reliability and security of third-party tools.

Troubleshooting the first call

If you encounter issues during your initial API call to IG, consider the following common troubleshooting steps:

  • Incorrect API Key: Ensure that the X-IG-API-KEY header contains the exact API key generated in your My IG dashboard. Any discrepancy will result in authentication failure.
  • Invalid Credentials: Double-check your username and password used in the authentication request. These credentials are case-sensitive.
  • Endpoint URL: Verify that you are using the correct base URL for your account type (e.g., demo-api.ig.com for demo accounts and api.ig.com for live accounts). Using the wrong endpoint will prevent connection.
  • Missing Headers: Confirm that all required headers (X-IG-API-KEY, CST, X-SECURITY-TOKEN) are present and correctly populated in subsequent requests after authentication.
  • Content-Type Header: For POST requests, ensure the Content-Type: application/json header is set correctly if you are sending a JSON payload.
  • Firewall or Proxy Issues: If you are making requests from a restricted network environment, a firewall or proxy might be blocking outbound API calls. Check your network configuration or consult your network administrator.
  • Rate Limiting: While unlikely on a first call, repeated failed attempts might trigger temporary rate limits. Refer to the IG API limits documentation for details on usage restrictions.
  • API Access Status: Log in to your My IG dashboard to confirm that API access is still enabled for your account. It may have been inadvertently disabled or require re-activation.