The Kite Connect API Rust client
A Rust client for communicating with the Kite Connect API.
Kite Connect is a set of REST-like APIs that expose many capabilities required
to build a complete investment and trading platform. Execute orders in real
time, manage user portfolio, stream live market data (WebSockets), and more,
with the simple HTTP API collection.
Licensed under the MIT License.
Documentation
Installation
Add this to your Cargo.toml:
[dependencies]
kiteconnect-rs = "0.1.0"
Features
- Async/Await Support: Built with modern async Rust using tokio
- Type Safety: Full type definitions for all API responses
- WebSocket Ticker: Real-time market data streaming
- Comprehensive API Coverage: All Kite Connect endpoints supported
- Error Handling: Robust error handling with custom error types
API Usage
use kiteconnect_rs::{KiteConnect, orders::OrderParams};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let api_key = "your_api_key";
let mut kite = KiteConnect::builder(api_key).build()?;
println!("{}", kite.get_login_url());
let request_token = "your_request_token";
let api_secret = "your_api_secret";
let session = kite.generate_session(&request_token, &api_secret).await?;
kite.set_access_token(&session.access_token);
let margins = kite.get_user_margins().await?;
println!("Margins: {:#?}", margins);
let order_params = OrderParams {
exchange: Some("NSE".to_string()),
tradingsymbol: Some("RELIANCE".to_string()),
transaction_type: Some("BUY".to_string()),
order_type: Some("MARKET".to_string()),
quantity: Some(1),
product: Some("CNC".to_string()),
validity: Some("DAY".to_string()),
..Default::default()
};
let order_response = kite.place_order(order_params).await?;
println!("Order placed: {:#?}", order_response);
Ok(())
}
Kite Ticker Usage
use std::time::Duration;
use kiteconnect_rs::ticker::{Mode, Ticker, TickerEvent};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let (ticker, handle) = Ticker::builder("your_api_key", "your_access_token")
.auto_reconnect(true)
.reconnect_max_retries(10)
.connect_timeout(Duration::from_secs(10))
.build()?;
let mut event_receiver = handle.subscribe_events();
let event_handle = handle.clone();
let serve_task = tokio::spawn(async move {
if let Err(e) = ticker.serve().await {
eprintln!("Ticker error: {}", e);
}
});
let event_task = tokio::spawn(async move {
while let Ok(event) = event_receiver.recv().await {
match event {
TickerEvent::Connect => {
println!("Connected!");
let tokens = vec![256265, 738561]; event_handle.subscribe(tokens.clone()).await?;
event_handle.set_mode(Mode::Full, tokens).await?;
}
TickerEvent::Tick(tick) => {
println!("Tick received: {:#?}", tick);
}
TickerEvent::OrderUpdate(order) => {
println!("Order update: {:#?}", order);
}
TickerEvent::Error(error) => {
eprintln!("Ticker error: {}", error);
}
TickerEvent::Close(code, reason) => {
println!("Connection closed: {} - {}", code, reason);
}
TickerEvent::Reconnect(attempt) => {
println!("Reconnecting... attempt {}", attempt);
}
}
}
Ok::<(), Box<dyn std::error::Error>>(())
});
tokio::select! {
_ = serve_task => {},
_ = event_task => {},
}
Ok(())
}
Examples
Check the examples folder for comprehensive examples covering:
- Basic API operations: Authentication, user profile, margins
- Order management: Place, modify, cancel orders
- Portfolio operations: Holdings, positions, P&L
- Market data: Quotes, historical data, instruments
- Mutual funds: Orders, holdings, SIPs
- Alerts & GTT: Create and manage alerts
- WebSocket ticker: Real-time market data streaming
You can run examples with:
cargo run --example order_operations
cargo run --example ticker_example
cargo run --example portfolio_example
Development
Setup
git clone https://github.com/ketan-10/kiteconnect-rs.git
cd kiteconnect-rs
Run tests
cargo test
Run integration tests
cargo test --test integration_tests
Generate documentation
cargo doc --open
Contributing
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature)
- Commit your changes (
git commit -m 'Add some amazing feature')
- Push to the branch (
git push origin feature/amazing-feature)
- Open a Pull Request
License
This project is licensed under the MIT License - see the LICENSE file for details.
- Frequently used commands
cargo run --example margins_example
cargo doc --open --no-deps
cargo test test_user_profile_parsing --test unit_tests -- --nocapture
cargo test test_generate_session test_renew_access_token --test integration_tests
RUST_BACKTRACE=1 cargo run --example margins_example