Redstone query
Redstone query is an extra interface exported by the Redstone API. It uses a human readable syntax to facilitate query creation.

Fluent interface for redstone

Redstone API supports a fluent interface to simplify the syntax.

Importing

To use the fluent interface you should first import the redstone-api in a standard way
1
// Using Node.js `require()`
2
const redstone = require('redstone-api');
3
4
// Using ES6 imports
5
import redstone from 'redstone-api';
Copied!

Usage

All redstone queries consist of 4 parts:
  • Query initialisation (redstone.query())
  • What to fetch (symbol, symbols or allSymbols)
  • For which date/dates (latest, atDate, forLastHours, hoursAgo, fromDate, toDate)
  • Query execution (exec)

Examples

Get the latest price for a single token

1
const price = await redstone.query()
2
.symbol("AR")
3
.latest()
4
.exec();
5
6
console.log(price.value); // latest price value for AR token (in USD)
7
console.log(price.timestamp); // the exact timestamp of the price
Copied!

Get the historical price for a single token

1
const price = await redstone.query()
2
.symbol("AR")
3
.atDate("2021-04-19")
4
.exec();
Copied!
💡 Note: The argument passed to atDate must be convertable to date. You may pass date (e.g. new Date(2021-04-01)), timestamp (e.g. 1617709771289), or just string (e.g. 2021-04-01 or 2021-04-01T12:30:58)

Get the historical price for the last X hours

1
// Returns an array of prices with ~10 minutes interval
2
const prices = await redstone.query()
3
.symbol("AR")
4
.forLastHours(12)
5
.exec();
Copied!

Get the historical price for X hours ago

1
const price = await redstone.query()
2
.symbol("AR")
3
.hoursAgo(24)
4
.exec();
Copied!

Get the historical price for the last X days

1
// Returns an array of prices with ~1h minutes interval
2
const prices = await redstone.query()
3
.symbol("AR")
4
.forLastDays(7)
5
.exec();
Copied!

Get the historical prices in a time range

1
// Returns an array of prices
2
// Interval depends on the time range
3
// For ranges more than 24 hours interval is 1h
4
const prices = await redstone.query()
5
.symbol("AR")
6
.fromDate("2021-04-19")
7
.toDate("2021-04-20")
8
.exec();
Copied!

Get the latest prices for several tokens

1
const prices = await redstone.query()
2
.symbols(["AR", "BTC", "ETH"])
3
.latest()
4
.exec();
5
6
console.log(prices); // Example output below
7
/*
8
{
9
"BTC": {
10
value: 58953.39,
11
timestamp: 1617152802779,
12
...
13
},
14
"ETH": {
15
value: 1856.75,
16
timestamp: 1617152802779,
17
...
18
},
19
...
20
}
21
*/
Copied!

Get the historical prices for several tokens

1
const prices: any = await redstone.query()
2
.symbols(["AR", "ETH", "BTC"])
3
.atDate("2021-04-19")
4
.exec();
Copied!

Get prices for all available tokens

1
const prices = await redstone.query()
2
.allSymbols()
3
.latest()
4
.exec();
5
6
console.log(prices); // Example output below
7
/*
8
{
9
"BTC": {...},
10
"ETH": {...},
11
...
12
}
13
*/
14
15
console.log(prices["AR"].value); // latest price value for AR
16
console.log(prices["EUR"].value); // latest price value for EUR
Copied!