Get OHLC Data
Description
Authentication
Request
Headers
Content-Type: application/json
Query Parameters
Parameter
Type
Required
Description
Response
Success Response
Error Response
Example Usage
interface OHLCData {
id: number;
coinId: string;
timestamp: string;
unixTimestamp: string;
open: string;
high: string;
low: string;
close: string;
volume: string;
}
// Function to fetch OHLC data
async function getOHLCData(
tokenMintAddress: string,
fromTimestamp?: number,
toTimestamp?: number
) {
try {
let url = `https://api.heyhal.xyz/api/v1/coin/ohlc?token=${tokenMintAddress}`;
if (fromTimestamp) {
url += `&from=${fromTimestamp}`;
}
if (toTimestamp) {
url += `&to=${toTimestamp}`;
}
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const candles: OHLCData[] = await response.json();
return candles;
} catch (error) {
console.error('Error fetching OHLC data:', error);
throw error;
}
}
// Example usage with TradingView Lightweight Charts
import { createChart } from 'lightweight-charts';
async function createPriceChart(
containerId: string,
tokenMintAddress: string
) {
const chart = createChart(containerId, {
width: 800,
height: 400
});
const candlestickSeries = chart.addCandlestickSeries();
// Get last 7 days of data
const toTimestamp = Math.floor(Date.now() / 1000);
const fromTimestamp = toTimestamp - (7 * 24 * 60 * 60);
try {
const ohlcData = await getOHLCData(
tokenMintAddress,
fromTimestamp,
toTimestamp
);
const formattedData = ohlcData.map(candle => ({
time: parseInt(candle.unixTimestamp),
open: parseFloat(candle.open),
high: parseFloat(candle.high),
low: parseFloat(candle.low),
close: parseFloat(candle.close)
}));
candlestickSeries.setData(formattedData);
} catch (error) {
console.error('Error creating chart:', error);
}
}
Implementation Notes
Best Practices
Last updated