Barchart OnDemand

Finance

Free Market Data API from Barchart

Barchart offers a free market data API that allows developers to access real-time data from futures exchanges, cash commodities, and currencies in full support of JSON or XML structure. Here, we will provide an overview of the API and include example code in JavaScript to help you get started.

API Features

  • Real-time and historical data from futures exchanges, cash commodities, and currencies
  • Multiple data formats, including JSON and XML
  • Efficient data retrieval with HTTP GET requests
  • Supports various programming languages, including JavaScript
  • Comprehensive API documentation

Getting Started

To get started with the Barchart API, you need to create an account and request an API key. Once you obtain the key, you can use it to access the different endpoints provided by the API.

Examples

Here are some examples of using the Barchart API with JavaScript:

Example 1: Get Current Futures Price for Soybeans

const apiKey = "YOUR_API_KEY";
const symbol = "ZS";

fetch(`https://marketdata.websol.barchart.com/getQuote.json?key=${apiKey}&symbols=${symbol}`)
  .then(response => response.json())
  .then(data => {
    const soybeanPrice = data.results[0].lastPrice;
    console.log(`The current price of soybeans is ${soybeanPrice}`);
  })
  .catch(error => console.error(error));

Example 2: Get Historical Data for Crude Oil

const apiKey = "YOUR_API_KEY";
const symbol = "CL";

const from = "20210101";
const to = "20211231";

fetch(`https://marketdata.websol.barchart.com/getHistory.json?key=${apiKey}&symbol=${symbol}&type=daily&startDate=${from}&endDate=${to}`)
  .then(response => response.json())
  .then(data => {
    const crudeOilPrices = data.results.map(result => {
      return { date: result.tradeTimestamp, price: result.close };
    });
    console.log(crudeOilPrices);
  })
  .catch(error => console.error(error));

Example 3: Get News Headlines for Corn

const apiKey = "YOUR_API_KEY";
const symbol = "ZC";

fetch(`https://newsapi.barchart.com/api/v1/headlines/${symbol}?apikey=${apiKey}`)
  .then(response => response.json())
  .then(data => {
    const cornHeadlines = data.data.map(headline => {
      return { title: headline.title, url: headline.url };
    });
    console.log(cornHeadlines);
  })
  .catch(error => console.error(error));

Example 4: Get Symbol Search Results

const apiKey = "YOUR_API_KEY";
const keyword = "cattle";

fetch(`https://marketdata.websol.barchart.com/getSymbolSuggest.json?key=${apiKey}&query=${keyword}`)
  .then(response => response.json())
  .then(data => {
    const cattleSymbols = data.results.map(result => {
      return { symbol: result.symbol, name: result.name };
    });
    console.log(cattleSymbols);
  })
  .catch(error => console.error(error));

Conclusion

The Barchart free market data API provides developers with real-time and historical data from various markets, including futures exchanges, cash commodities, and currencies. Using JavaScript, you can easily access the API and retrieve the data in the format of your choice. The API documentation is comprehensive and user-friendly, making it easy to get started and build powerful applications. Happy coding!

Related APIs in Finance