Links

API Reference

SpiceClient(apiKey, url)

The top-level object that connects to Spice.xyz
  • apiKey(string, required): API key to authenticate with the endpoint
  • url(string, optional): URL of the endpoint to use (default: flight.spiceai.io:443)
import { SpiceClient } from "@spiceai/spice";
const spiceClient = new SpiceClient('API_KEY');

SpiceClient Methods

query(queryText: string, onData: (partialData: Table) => void) => Table
  • queryText: (string, required): The SQL query to execute
  • onData: (callback, optional): The callback function that is used for handling streaming data.
query returns an Apache Arrow Table.
To get the data in JSON format, iterate over each row by calling toArray() on the table and call toJSON() on each row.
const table = await spiceClient.query("SELECT * from eth.recent_blocks LIMIT 10")
table.toArray().forEach((row) => {
console.log(row.toJSON());
});
Get all of the elements for a column by calling getChild(name: string) and then calling toJSON() on the result.
const table = await client.query(
'SELECT number, base_fee_per_gas / 1e9 AS base_fee_per_gas_gwei FROM eth.recent_blocks limit 3'
);
let baseFeeGwei = tableResult.getChild("base_fee_per_gas_gwei");
console.log(baseFeeGwei?.toJSON())
getPrice(pair: string) => LatestPrice
  • pair: (string, required): The crypto/currency pair, for example "BTC-USD"
getPrice returns a LatestPrice object
LatestPrice {
pair: string;
minPrice: string;
maxPrice: string;
avePrice: string;
}
example api query
const res = await spiceClient.getPrice("eth-btc")
console.log(res);
getPrices(pair: string, startTime?: number, endTime?: number, granularity?: string) => HistoricalPrices
  • pair: (string, required): The crypto/currency pair, for example "BTC-USD"
  • startTime: start time milliseconds since Unix Epoch
  • endTime: end time milliseconds since Unix Epoch
  • granularity: valid duration
getPrices returns a HistoricalPrices object
HistoricalPrices {
pair: string;
prices: {
timestamp: string;
price: number;
}[];
}
example api query
const prices = await spiceClient.getPrices(
'BTC-USD',
new Date('2023-01-01').getTime(),
new Date('2023-01-02').getTime(),
'1h'
);
console.log(res);