How to fetch real time commodity prices
Published on

How to fetch real time commodity prices

Authors
  • Name
    Arline Shawnee

Fetching real-time commodity prices is essential for businesses and individuals who rely on up-to-date market information. Whether you're a trader, an investor, or simply need to stay informed about commodity market trends, having access to real-time prices is crucial. In this blog post, we'll explore how you can fetch real-time commodity prices using CommodityPriceAPI, a powerful and user-friendly API service.

Introduction to CommodityPriceAPI

CommodityPriceAPI offers a simple JSON API for real-time and historical commodity prices. It covers over 70 commodities, including crude oil, gold, silver, natural gas, wheat, and corn. The API provides live rates delivered in 60 seconds, 10 minutes, and hourly intervals, sourced from various data sources including exchanges, financial institutions, and banks​​​​.

CommodityPriceAPI-image

Getting Started with CommodityPriceAPI

  1. Sign Up and Get Your API Key

    First, you need to sign up for CommodityPriceAPI here and obtain your API key. This key is essential for authenticating your requests to the API. Be cautious not to share your API key publicly, such as on GitHub or in client-side code​​.

  2. Choose a Pricing Plan

    CommodityPriceAPI offers flexible pricing plans, including a 7-day free trial. You can choose a monthly or annual subscription, depending on your needs. The pricing plans are simple, allowing you to upgrade, downgrade, or cancel at any time​.

Using CommodityPriceAPI

To fetch the latest commodity rates, you can use the /rates/latest endpoint. You will need to include your API key in the request, either as a query parameter or in the request header. Here’s an example of how to fetch the latest rates in javascript:

const axios = require('axios');

const options = {
    method: 'GET',
    url: 'https://api.commoditypriceapi.com/v1/rates/latest',
    params: {symbols: 'WTIOIL,XAU,XAG'},
    headers: {
        'X-API-Key': 'api_key',
        'Content-Type': 'application/json'
    }
};

const response = await axios.request(options);

const rates = response.data.rates;

const wti_oil = rates['WTIOIL'];
const xau = rates['XAU'];
const xag = rates['XAG'];

This request will return the latest rates for commodities like WTIOIL, XAU (gold) and XAG (silver)​​.

{
    "rates": {
        "WTIOIL": 71.89,
        "XAU": 1792.9,
        "XAG": 26.3
    }
}

Historical Data and Time Series

CommodityPriceAPI also offers endpoints for historical rates and time series data. This is particularly useful for analyzing past market trends and making informed decisions based on historical price movements. The historical data is available for all days going back to 1990.

To fetch historical rates, you can use the /rates/historical endpoint. You will need to specify the date and symbols in the request. Here’s an example of how to fetch historical rates in javascript:

const axios = require('axios');

const options = {
  method: 'GET',
  url: 'https://api.commoditypriceapi.com/v1/rates/historical',
  params: {date: '2021-01-01', symbols: 'WTIOIL,XAU,XAG'},
  headers: {
    'X-API-Key': 'api_key',
    'Content-Type': 'application/json'
  }
};

const response = await axios.request(options);

const rates = response.data.rates;
const wti_oil = rates['WTIOIL'];
const xau = rates['XAU'];
const xag = rates['XAG'];

This request will return the historical rates for commodities like WTIOIL, XAU (gold) and XAG (silver) on January 1st, 2021​​.

{
    "date": "2021-01-01",
    "rates": {
        "WTIOIL": {
            "open": 48.52,
            "high": 49.61,
            "low": 48.31,
            "close": 49.1
        },
        "XAU": {
            "open": 1894.5,
            "high": 1906.4,
            "low": 1871.1,
            "close": 1898.9
        },
        "XAG": {
            "open": 26.41,
            "high": 26.8,
            "low": 26.2,
            "close": 26.6
        }
    }
}

To fetch time series data, you can use the /rates/time-series endpoint. You will need to specify the start date, end date, and symbols in the request. Here’s an example of how to fetch time series data in javascript:

const axios = require('axios');

const options = {
  method: 'GET',
  url: 'https://api.commoditypriceapi.com/v1/rates/time-series',
  params: {start_date: '2021-01-01', end_date: '2021-01-31', symbols: 'WTIOIL,XAU,XAG'},
  headers: {
    'X-API-Key': 'api_key',
    'Content-Type': 'application/json'
  }
};

const response = await axios.request(options);

const rates = response.data.rates;

const wti_oil = rates['2021-01-01']['WTIOIL'];
const xau = rates['2021-01-01']['XAU'];
const xag = rates['2021-01-01']['XAG'];

This request will return the time series data for commodities like WTIOIL, XAU (gold) and XAG (silver) between January 1st, 2021 and January 31st, 2021​​.

{
    "rates": {
        "2021-01-01": {
            "WTIOIL": {
                "open": 48.52,
                "high": 49.61,
                "low": 48.31,
                "close": 49.1
            },
            "XAU": {
                "open": 1894.5,
                "high": 1906.4,
                "low": 1871.1,
                "close": 1898.9
            },
            "XAG": {
                "open": 26.41,
                "high": 26.8,
                "low": 26.2,
                "close": 26.6
            }
        },
        "2021-01-02": {
            "WTIOIL": {
                "open": 48.52,
                "high": 49.61,
                "low": 48.31,
                "close": 49.1
            },
            "XAU": {
                "open": 1894.5,
                "high": 1906.4,
                "low": 1871.1,
                "close": 1898.9
            },
            "XAG": {
                "open": 26.41,
                "high": 26.8,
                "low": 26.2,
                "close": 26.6
            }
        },
        ...
    }
}

Practical Applications

Market Analysis and Decision Making

Real-time commodity prices are vital for market analysis. Traders and investors can use this data to make timely and informed decisions, maximizing their returns and minimizing risks. Integration with Financial Applications

Developers can integrate CommodityPriceAPI into financial applications, providing users with up-to-date commodity prices. This enhances the functionality and value of financial apps and platforms.

Conclusion

CommodityPriceAPI is a robust tool for anyone needing real-time and historical commodity prices. Its simplicity, reliability, and comprehensive coverage make it an excellent choice for traders, investors, and developers. By following the steps outlined above, you can easily integrate this API into your workflow or applications.