Comment on page
API Reference
The top-level object that connects to Spice.ai
Query
can be accessed from SpiceClient, Prices functions
can be accessed from SpiceClient.pricespub struct SpiceClient {
flight: SqlFlightClient,
pub prices: PricesClient,
}
new(api_key: &str) -> Self
api_key
(&str, required): API key to authenticate with the endpoint
query(&mut self, query: &str) -> Result<FlightRecordBatchStream, Box<dyn Error>>
use spice_rs::Client;
let client = Client::new(api_key).await;
query
: (&str, required): The SQL query to execute
let flight_data_stream = client.query("SELECT * FROM eth.recent_blocks LIMIT 10;").await.expect("Error executing query");
while let Some(batch) = flight_data_stream.next().await {
match batch {
Ok(batch) => {
/* process batch */
println!("{:?}", batch)
},
Err(e) => {
/* handle error */
},
};
.prices.get_supported_pairs(&self) -> Result<Vec, Box<dyn Error>>
get_supported_pairs
returns supported pairs by prices clientexample api query
let result = spice_client
.prices
.get_supported_pairs()
.await
.expect("Error getting supported pairs");
println!("{:?}", result);
.prices.get_prices(&self, pairs: &[&str]) -> Result<LatestPricesResponse, Box<dyn Error>>
pairs
: (&[&str], required): The crypto/currency pairs, for example &["BTC-USD", "ETH-USD"]
get_prices
returns a LatestPricesResponse
structpub struct LatestPricesResponse {
// This assumes each key in the JSON (like "BTC-USDC", "LTC-USDT") is dynamic and represents a currency pair
#[serde(flatten)]
pub prices: HashMap<String, LatestPriceDetail>,
}
LatestPricesResponse
consists of LatestPricesResponse
structpub struct LatestPriceDetail {
#[serde(deserialize_with = "string_to_float_map")]
pub prices: HashMap<String, f64>,
#[serde(
rename = "minPrice",
default,
deserialize_with = "string_to_float_option"
)]
pub min_price: Option<f64>,
#[serde(
rename = "maxPrice",
default,
deserialize_with = "string_to_float_option"
)]
pub max_price: Option<f64>,
#[serde(
rename = "meanPrice",
default,
deserialize_with = "string_to_float_option"
)]
pub mean_price: Option<f64>,
}
example api query
let result = client
.prices
.get_prices(&["BTC-USD", "ETH-USD"])
.await
.expect("Error getting prices");
println!("{:?}", result);
.prices.get_historical_prices( &self, pairs: &[&str], start: Option<DateTime>, end: Option<DateTime>, granularity: Option<&str>, ) -> Result<HashMap<String, Vec<HistoricalPriceData>>, Box<dyn Error>>
pairs
: (&[&str], required): The crypto/currency pairs, for example &["BTC-USD", "ETH-USD"]start
: start time milliseconds since Unix Epochend
: end time milliseconds since Unix Epoch
get_historical_prices
returns a vector of HistoricalPrices
structpub struct HistoricalPriceData {
pub timestamp: DateTime<Utc>,
pub price: f64,
pub high: Option<f64>,
pub low: Option<f64>,
pub open: Option<f64>,
pub close: Option<f64>,
}
example api query
let start_time = Utc.timestamp_opt(1697669766, 0).single();
let end_time = Utc.timestamp_opt(1697756166, 0).single();
let result = spice_client
.prices
.get_historical_prices(&["BTC-USD", "ETH-USD"], start_time, end_time, Some("1h"))
.await
.expect("Error getting historical prices");
println!("{:?}", result);
Last modified 1mo ago