Get oil prices in Node.js with the published SDK
Install the oilpriceapi npm package, fetch the latest Brent record, and keep its source timestamp with the price. The example reports authentication, quota, rate limit, and timeout failures.
Published · Updated
Get an API keyInstall and authenticate
Use Node.js 18 or newer. Run npm install oilpriceapi, then set OILPRICEAPI_KEY as a private environment variable from your dashboard. Never commit it, send it to the browser, or include it in a URL or log.
Fetch Brent with source context
Save this as oil-price.mjs and run it with node oil-price.mjs. The SDK requests the latestBRENT_CRUDE_USD record. Check the returned source timestamp and stale flag before using the value.
import {
AuthenticationError,
OilPriceAPI,
OilPriceAPIError,
RateLimitError,
TimeoutError,
isEntitlementError,
isQuotaError,
} from "oilpriceapi";
if (!process.env.OILPRICEAPI_KEY) {
console.error("Set OILPRICEAPI_KEY from your dashboard before running this script.");
process.exit(1);
}
const client = new OilPriceAPI({
apiKey: process.env.OILPRICEAPI_KEY,
retries: 0,
timeout: 30_000,
});
try {
const [record] = await client.getLatestPrices({
commodity: "BRENT_CRUDE_USD",
});
const timestampField = ["as_of", "source_timestamp", "created_at", "updated_at"]
.find((field) => typeof record?.[field] === "string" && record[field].trim());
if (!record || !Number.isFinite(record.price) || !record.code ||
!record.currency || !record.unit || !record.source || !timestampField) {
throw new Error("MALFORMED_RESPONSE: source context is incomplete");
}
console.log({
code: record.code,
price: record.price,
currency: record.currency,
unit: record.unit,
source: record.source,
source_timestamp: record[timestampField],
stale: record.stale,
});
} catch (error) {
if (error instanceof AuthenticationError) {
console.error("Replace the missing, expired, or revoked API key.");
} else if (error instanceof RateLimitError) {
console.error("Wait for the API-provided reset window.");
} else if (error instanceof TimeoutError) {
console.error("Retry once, then check https://status.oilpriceapi.com.");
} else if (isQuotaError(error)) {
console.error("Request quota exhausted; check the API recovery details.");
} else if (isEntitlementError(error)) {
console.error("This dataset requires a different plan.");
} else if (error instanceof OilPriceAPIError) {
console.error(error.requestId ? "Request ID: " + error.requestId : "API request failed.");
} else {
throw error;
}
process.exitCode = 1;
}If the response lacks a numeric price or source context, the example fails instead of displaying a misleading value. A 401 needs a valid key; a 403 may need plan access; a 429 needs the API-provided reset window. After a timeout, retry once and check service status. See the SDK repository for the full API and typed error examples.
Use it in a daily job
Schedule the script with your platform scheduler at the cadence your workflow needs. Store the value, code, source timestamp, currency, and unit together. Alert on failed runs and inspect the stale flag so an old value is never silently presented as current. Polling every few seconds is unnecessary for a daily report and can exhaust an account quota.
Continue with another tool
Follow the same source-context approach in Python, Excel, or Google Sheets.
For endpoints and response fields, see the API documentation.
Get an API key