| Crates.io | ig_trading_api |
| lib.rs | ig_trading_api |
| version | 0.3.0 |
| created_at | 2024-02-25 12:38:30.241602+00 |
| updated_at | 2025-11-19 16:17:15.728838+00 |
| description | A Rust client for the REST and Streaming APIs from IG.com. |
| homepage | https://www.daniloaz.com/en/ |
| repository | https://github.com/daniloaz/ig-trading-api |
| max_upload_size | |
| id | 1152418 |
| size | 364,373 |
IG Trading API is a comprehensive Rust client library that provides seamless integration with IG.com's REST and Streaming APIs. This library enables developers to build robust trading applications, automated trading strategies, and market analysis tools using the safety and performance benefits of Rust.
IG is a leading online trading platform offering CFDs, spread betting, and share dealing services across various markets including forex, indices, commodities, and cryptocurrencies.
Before using this library, ensure you have:
sudo apt-get install pkg-config libssl-devsudo dnf install pkg-config openssl-develbrew install opensslAdd the following to your Cargo.toml:
[dependencies]
ig_trading_api = "0.2.3"
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
Or use Cargo to add it directly:
cargo add ig_trading_api
cargo add tokio --features macros,rt-multi-thread
This project follows security best practices by separating sensitive credentials from application configuration:
.env - Contains all sensitive data (API keys, passwords, account numbers, URLs)config.yaml - Contains only non-sensitive application behavior settingsCopy the .env.example file to .env:
cp .env.example .env
Edit .env with your actual IG credentials:
# IG API Credentials
IG_API_KEY=your_actual_api_key_here
IG_USERNAME=your_username
IG_PASSWORD=your_password
# Account Numbers
IG_ACCOUNT_NUMBER_DEMO=XXXXX
IG_ACCOUNT_NUMBER_LIVE=XXXXX
# API URLs (default values, change only if needed)
IG_BASE_URL_DEMO=https://demo-api.ig.com/gateway/deal
IG_BASE_URL_LIVE=https://api.ig.com/gateway/deal
# Execution Environment
IG_EXECUTION_ENVIRONMENT=DEMO # or LIVE for production
The config.yaml file contains non-sensitive settings. You can modify these if needed:
ig_trading_api:
# Automatically log in when session expires
auto_login: true
# Logging mechanism (StdLogs or TracingLogs)
logger: "StdLogs"
# Session version for API requests
session_version: 2
# Max connection retry attempts for streaming
streaming_api_max_connection_attempts: 3
.env file to version control (already in .gitignore).env only.env.example as a template (safe to commit)use ig_trading_api::common::ApiConfig;
use ig_trading_api::rest_api::RestApi;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Load configuration from .env and config.yaml
// This automatically loads credentials from environment variables
let config = ApiConfig::default();
// Or explicitly load from environment and config
let config = ApiConfig::from_env_and_config()?;
// Create REST API client
let api = RestApi::new(config).await?;
// Get account information
let (headers, accounts) = api.accounts_get().await?;
println!("Accounts: {:#?}", accounts);
// Get market data
let (headers, market_nav) = api.market_navigation_get(None).await?;
println!("Market Navigation: {:#?}", market_nav);
// Search for markets
let (headers, markets) = api.markets_search_get("EUR/USD".to_string()).await?;
println!("Search Results: {:#?}", markets);
Ok(())
}
use ig_trading_api::common::ApiConfig;
use ig_trading_api::streaming_api::StreamingApi;
use ig_trading_api::rest_api::RestApi;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Load configuration from .env and config.yaml
let config = ApiConfig::default();
// Create REST API client
let rest_api = RestApi::new(config.clone()).await?;
// Create streaming API client
let mut streaming_api = StreamingApi::new(rest_api).await?;
// Subscribe to market data
streaming_api.subscribe_to_market("MARKET:CS.D.EURUSD.MINI.IP").await?;
// Connect and start receiving updates
streaming_api.connect().await;
Ok(())
}
For more detailed examples, check the tests directory.
| Category | Endpoints | Status |
|---|---|---|
| Session | Login, Logout, Refresh Token | โ |
| Accounts | Get Accounts, Preferences | โ |
| Markets | Navigation, Search, Details | โ |
| Positions | Get, Create, Update, Close | โ |
| Orders | Get, Create, Update, Delete | โ |
| Watchlists | Get, Create, Update, Delete | โ |
| Prices | Historical, Real-time | โ |
| Feature | Status |
|---|---|
| Market Data Subscription | โ |
| Account Updates | โ |
| Trade Confirmations | โ |
| Automatic Reconnection | โ |
| Signal Handling | โ |
The project includes comprehensive integration tests for both REST and Streaming APIs.
cargo test
# REST API tests only
cargo test --test rest_api_integration_tests
# Streaming API tests only
cargo test --test streaming_api_integration_tests
RUST_LOG=debug cargo test -- --nocapture
Note: Integration tests require valid IG credentials in your .env file. Tests will run against your demo account by default. Make sure your .env file is properly configured before running tests.
ig_trading_api/
โโโ src/
โ โโโ lib.rs # Library entry point
โ โโโ main.rs # Example executable
โ โโโ common.rs # Common types and utilities
โ โโโ rest_api.rs # REST API implementation
โ โโโ rest_client.rs # HTTP client wrapper
โ โโโ rest_models.rs # REST API data models
โ โโโ rest_regex.rs # Regex utilities
โ โโโ streaming_api.rs # Streaming API implementation
โโโ tests/
โ โโโ rest_api_integration_tests.rs
โ โโโ streaming_api_integration_tests.rs
โโโ assets/ # Logo and images
โโโ .env.example # Environment variables template (COPY TO .env)
โโโ .env # Your secrets (NOT in git)
โโโ config.yaml # Non-sensitive app settings
โโโ config.default.yaml # Default configuration template
โโโ Cargo.toml # Project dependencies
โโโ LICENSE # GPL-3.0 license
โโโ README.md # This file
Contributions are welcome! Here's how you can help:
git clone https://github.com/your-username/ig-trading-api.git
cd ig-trading-api
git checkout -b feature/your-feature-name
cargo test
cargo fmt
cargo clippy
git commit -m "Add: your feature description"
cargo fmt before committingcargo clippy passes with no warningsThis project is licensed under the GNU General Public License v3.0 - see the LICENSE file for details.
For more information about GPL-3.0, visit: https://www.gnu.org/licenses/gpl-3.0.html
Daniel Lรณpez Azaรฑa
This software is provided "as is", without warranty of any kind. Trading financial instruments involves risk, and you should not trade with money you cannot afford to lose. This library is not affiliated with or endorsed by IG Group. Always test your code thoroughly on a demo account before using it in a live trading environment.
USE AT YOUR OWN RISK