| Crates.io | twelve-data-client |
| lib.rs | twelve-data-client |
| version | 0.4.0 |
| created_at | 2026-01-01 00:26:34.957941+00 |
| updated_at | 2026-01-04 20:55:36.090325+00 |
| description | Rust API client for Twelve Data with automatic builder pattern generation. Provides access to comprehensive financial market data including stocks, forex, ETFs, mutual funds, commodities, and cryptocurrencies. |
| homepage | https://github.com/alex12058/rust-twelve-data-client |
| repository | https://github.com/alex12058/rust-twelve-data-client |
| max_upload_size | |
| id | 2015539 |
| size | 15,590,034 |
A Rust API client for Twelve Data — comprehensive financial market data API with access to stocks, forex, ETFs, mutual funds, commodities, and cryptocurrencies across 50+ countries.
This client features automatic builder pattern generation for all API operations, making it ergonomic to work with endpoints that have many optional parameters.
reqwest with async/await support via tokioAdd to your Cargo.toml:
[dependencies]
twelve-data-client = "0.4"
tokio = { version = "1.49.0", features = ["full"] }
use twelve_data_client::apis::{configuration, time_series_api};
use twelve_data_client::apis::time_series_api::GetTimeSeriesParams;
use twelve_data_client::models::GetTimeSeriesResponse;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Configure authentication
let mut config = configuration::Configuration::new();
config.api_key = Some(configuration::ApiKey {
prefix: Some("apikey".to_string()),
key: std::env::var("TWELVE_DATA_API_KEY")?,
});
// Build parameters using builder pattern
let params = GetTimeSeriesParams::builder()
.symbol("AAPL")
.interval("1day")
.outputsize(10)
.build();
// Make API call and handle response
let response = time_series_api::get_time_series(&config, params).await?;
match response {
GetTimeSeriesResponse::TimeSeries(data) => {
println!("Symbol: {:?}", data.meta.as_ref().unwrap().symbol);
println!("Data points: {}", data.values.as_ref().unwrap().len());
}
GetTimeSeriesResponse::ApiError(err) => {
eprintln!("API Error: {}", err.message.unwrap_or_default());
}
GetTimeSeriesResponse::Text(csv) => {
println!("CSV Response:\n{}", csv);
}
}
Ok(())
}
Get your API key from the Twelve Data dashboard.
Set it as an environment variable:
export TWELVE_DATA_API_KEY="your_api_key_here"
Or configure it directly:
config.api_key = Some(configuration::ApiKey {
prefix: Some("apikey".to_string()),
key: "your_api_key_here".to_string(),
});
Most endpoints return an enum with variants for different response scenarios:
TimeSeries)ApiError variant: Contains error information when the API returns an errorText variant: Contains raw text (CSV) response when using format parameterThis provides type-safe error handling and format flexibility without needing to manually parse errors.
This client provides access to all Twelve Data endpoints organized by module:
time_series_api - Historical OHLCV datafundamentals_api - Company financials, balance sheets, income statementsreference_data_api - Symbols, exchanges, currenciestechnical_indicator_api - Technical analysis indicatorsmarket_data_api - Real-time quotes and pricesetfs_api - ETF-specific datamutual_funds_api - Mutual fund dataanalysis_api - Analysis endpointsregulatory_api - Regulatory filingsadvanced_api - Advanced data endpointsThis client is auto-generated from the Twelve Data OpenAPI spec using openapi-generator-rust-builders, which adds automatic builder pattern support.
Before regenerating the client, ensure you have:
./mvnw wrapper)Run the provided script to fetch the latest spec, apply transformations, and regenerate:
bash regenerate.sh
This script:
queryParameter security scheme (best practice: API keys should only be in headers, not query parameters)ApiError variant for error handlingtext/csv response type to endpoints supporting the format parameterGetTimeSeries_200_response → time_series_data)If this is your first time generating the client, build the generator first:
cd openapi-generator-rust-builders
./mvnw clean package -DskipTests
cd ..
bash regenerate.sh
See the tests/ directory for complete working examples:
time_series_builder.rs - Fetching historical time series data with builder pattern, including JSON, error, and CSV response handlingRun the tests:
export TWELVE_DATA_API_KEY="your_api_key"
cargo test --test time_series_builder
This client library is licensed under the Unlicense.
The Twelve Data API is a commercial service. See Twelve Data pricing for details.