2 releases
Uses new Rust 2024
| 0.1.1 | Oct 17, 2025 |
|---|---|
| 0.1.0 | Oct 16, 2025 |
#262 in HTTP client
46KB
783 lines
Quick Start
[dependencies]
polygon = "0.1"
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
use polygon::Polygon;
use polygon::rest::aggs;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = Polygon::new()?;
let result = aggs::get_previous_close(&client, "AAPL").await?;
println!("{}", result);
Ok(())
}
Set your API key via environment variable:
export POLYGON_API_KEY=your_key_here
Or use a .env file, or set it manually:
let client = Polygon::default().with_key("your_api_key");
Query API
Endpoints return a Query builder. Call .get() to execute:
use polygon::query::Execute as _;
use polygon::rest::{raw, tickers};
// Raw JSON response
let json = raw::tickers::related(&client, "AAPL").get().await?;
// Decoded into typed structs
let data = tickers::all(&client)
.param("limit", 10)
.params([("exchange", "XNYS"), ("sort", "ticker")])
.get()
.await?;
println!("{} {}", data[0].ticker, data[0].name);
Design:
- Required parameters are function arguments, optional parameters use
.param()or.params() rest::raw::fooreturns JSON strings,rest::fooreturns decoded types- Same API for both: construct query, chain parameters, call
.get() .param()accepts any type implementingParam(integers, strings, bools)- Errors include HTTP status, message, and request ID
Available Endpoints
rest::aggs- Aggregate bars (OHLC)rest::tickers- Ticker reference data
Custom HTTP Client
Implement the Request trait to use your own HTTP client:
use polygon::{Request, Polygon};
struct MyClient;
impl Request for MyClient {
fn new() -> Self { MyClient }
fn get(&self, url: &str) -> impl Future<Output = Result<String>> + Send {
async move { /* your implementation */ Ok(String::new()) }
}
fn post(&self, url: &str, body: &str) -> impl Future<Output = Result<String>> + Send {
async move { /* your implementation */ Ok(String::new()) }
}
}
let client = Polygon::with_client(MyClient)?;
License
MIT
Dependencies
~5–21MB
~225K SLoC