-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic.ts
More file actions
54 lines (45 loc) · 1.63 KB
/
basic.ts
File metadata and controls
54 lines (45 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
* Basic Usage Example
*
* Simple example showing how to get latest and historical oil prices
*/
import { OilPriceAPI } from 'oilpriceapi';
const client = new OilPriceAPI({
apiKey: process.env.OIL_PRICE_API_KEY || 'your_api_key_here'
});
async function main() {
try {
// Get latest WTI price
console.log('Getting latest WTI price...');
const wtiPrices = await client.getLatestPrices({
commodity: 'WTI_USD'
});
console.log(`WTI: ${wtiPrices[0].formatted}`);
// Get latest Brent price
console.log('\nGetting latest Brent price...');
const brentPrices = await client.getLatestPrices({
commodity: 'BRENT_CRUDE_USD'
});
console.log(`Brent: ${brentPrices[0].formatted}`);
// Get all latest prices
console.log('\nGetting all latest commodity prices...');
const allPrices = await client.getLatestPrices();
console.log(`Retrieved ${allPrices.length} commodity prices`);
// Get historical prices for past week
console.log('\nGetting WTI prices for past week...');
const historicalPrices = await client.getHistoricalPrices({
commodity: 'WTI_USD',
period: 'past_week'
});
console.log(`Retrieved ${historicalPrices.length} historical prices`);
// Show first and last price
if (historicalPrices.length > 0) {
console.log(`First: ${historicalPrices[0].formatted} at ${historicalPrices[0].created_at}`);
console.log(`Last: ${historicalPrices[historicalPrices.length - 1].formatted} at ${historicalPrices[historicalPrices.length - 1].created_at}`);
}
} catch (error) {
console.error('Error:', error);
process.exit(1);
}
}
main();