Twelve Data Rust Client
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.
Features
- ✅ Builder Pattern API: Clean, chainable parameter builders instead of functions with 20+ parameters
- ✅ Full API Coverage: Complete access to all Twelve Data endpoints (time series, fundamentals, ETFs, technical indicators, etc.)
- ✅ Type-Safe: Strongly typed request/response models generated from OpenAPI spec
- ✅ Async: Built on
reqwestwith async/await support viatokio - ✅ Authentication: API key authentication via Authorization header
Installation
Add to your Cargo.toml:
[]
= "0.4"
= { = "1.49.0", = ["full"] }
Quick Start
use ;
use GetTimeSeriesParams;
use GetTimeSeriesResponse;
async
Authentication
Get your API key from the Twelve Data dashboard.
Set it as an environment variable:
Or configure it directly:
config.api_key = Some;
Response Types
Most endpoints return an enum with variants for different response scenarios:
- Data variant: Contains the actual response data (e.g.,
TimeSeries) ApiErrorvariant: Contains error information when the API returns an errorTextvariant: Contains raw text (CSV) response when usingformatparameter
This provides type-safe error handling and format flexibility without needing to manually parse errors.
Available APIs
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 endpoints
Code Generation
This client is auto-generated from the Twelve Data OpenAPI spec using openapi-generator-rust-builders, which adds automatic builder pattern support.
Requirements
Before regenerating the client, ensure you have:
- Java 21 or later - Required to run the OpenAPI Generator
- Python 3.6+ - Required for spec transformation scripts
- Maven - Included with the generator (uses
./mvnwwrapper)
Regenerating the Client
Run the provided script to fetch the latest spec, apply transformations, and regenerate:
This script:
- Fetches the latest OpenAPI spec from Twelve Data
- Removes the
queryParametersecurity scheme (best practice: API keys should only be in headers, not query parameters) - Ensures all 200 responses include
ApiErrorvariant for error handling - Adds
text/csvresponse type to endpoints supporting theformatparameter - Renames response schemas to cleaner names (e.g.,
GetTimeSeries_200_response→time_series_data) - Regenerates the Rust client with the modified spec
- Formats the generated code
First-Time Setup
If this is your first time generating the client, build the generator first:
Examples
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 handling
Run the tests:
Documentation
License
This client library is licensed under the Unlicense.
The Twelve Data API is a commercial service. See Twelve Data pricing for details.