The top-level object that connects to Spice.ai
apiKey
(string, required): API key to authenticate with the endpointurl
(string, optional): URL of the endpoint to use (default: flight.spiceai.io:443)
import { SpiceClient } from "@spiceai/spice";
const spiceClient = new SpiceClient('API_KEY');
query(queryText: string, onData: (partialData: Table) => void) => Table
queryText
: (string, required): The SQL query to executeonData
: (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())
getLatestPrices(pairs: string[]) => LatestPrices
pairs
: (Array of string, required): The crypto/currency pairs, for example ["BTC-USD", "USD-ETH"].
getLatestPrices
returns the latest prices for a list of asset pairs. getLatestPrices
returns
LatestPrice {
[pair: string]: {
prices?: { [exchange: string]: string };
minPrice?: string;
maxPrice?: string;
avePrice?: string;
}
}
Example API query
let pairs = ["BTC-USD", "USD-ETH"];
const price = await client.getLatestPrices([pairs]);
pairs.forEach((v: string) => {
price[v].prices.forEach((exchange: string) => {
console.log("pair=" + v, "exchange=" + exchange, price[v].prices[exchange])
})
})
getPrices(pair: string[], startTime?: number, endTime?: number, granularity?: string) => HistoricalPrices
pairs
: (Array of string, required): The crypto/currency pairs, for example ["BTC-USD", "USD-ETH"].startTime
: start time milliseconds since Unix EpochendTime
: end time milliseconds since Unix Epochgranularity
: valid duration
getPrices
returns prices for a list of asset pairs for a given period of time. getPrices
returns
HistoricalPrices {
[pair: string]: {
timestamp: string;
price: number;
high?: number;
low?: number;
open?: number;
close?: number;
}
}
Example API Query
const prices = await client.getPrices(
pairs,
new Date('2023-01-01').getTime() / 1000,
new Date('2023-01-02').getTime() / 1000,
'1h'
);
pairs.forEach((v: string) => {
console.log(price[v])
})