Exploring FinnHub.io's Public API with JavaScript

Finnhub.io provides a wide range of financial data APIs. It offers real-time stock prices, news headlines, financial statements, and more. In this blog post, we will explore some of FinnHub's APIs in JavaScript with examples.

Getting Started

To use FinnHub's public API, you need to have an API key, which you can obtain for free by signing up for an account on the FinnHub website.

To get started, you can use a simple JavaScript fetch request to make an HTTP request to FinnHub's API endpoint:

const API_KEY = 'YOUR_API_KEY';
const API_ENDPOINT = 'https://finnhub.io/api/v1';

fetch(`${API_ENDPOINT}/quote?symbol=AAPL&token=${API_KEY}`)
  .then(response => response.json())
  .then(data => console.log(data));

Here, we are making a request to FinnHub's quote API endpoint for Apple's stock with the symbol AAPL, and passing our API key as a query parameter. The console.log() method is used to display the JSON response in the browser's console.

Real-Time Stock Prices API

FinnHub's real-time stock prices API provides up-to-date price information for stocks. You can use this API to track real-time prices of stocks, ETFs, and other financial products.

fetch(`${API_ENDPOINT}/quote?symbol=AMZN&token=${API_KEY}`)
  .then(response => response.json())
  .then(data => console.log(data));

The above code fetches the real-time stock price of Amazon with the symbol AMZN.

fetch(`${API_ENDPOINT}/stock/candle?symbol=AAPL&resolution=D&from=${Date.now() / 1000 - 60 * 60 * 24 * 365}&to=${Date.now() / 1000}&token=${API_KEY}`)
  .then(response => response.json())
  .then(data => console.log(data));

The above code fetches Apple's stock price history with daily resolution for the past year.

Financial Statements API

FinnHub's financial statements API provides financial data of companies such as their income statement, balance sheet, cash flow statement, and more.

fetch(`${API_ENDPOINT}/stock/financials?symbol=AAPL&statement=income&freq=quarterly&token=${API_KEY}`)
  .then(response => response.json())
  .then(data => console.log(data));

The above code fetches Apple's quarterly income statement.

fetch(`${API_ENDPOINT}/stock/financial-metrics?symbol=MSFT&metric=all&token=${API_KEY}`)
  .then(response => response.json())
  .then(data => console.log(data));

The above code fetches all financial metrics of Microsoft with the symbol MSFT.

News Headlines API

FinnHub's news headlines API provides up-to-date news articles from various sources.

fetch(`${API_ENDPOINT}/news?category=general&token=${API_KEY}`)
  .then(response => response.json())
  .then(data => console.log(data));

The above code fetches general news articles.

fetch(`${API_ENDPOINT}/news-sentiment?symbol=AAPL&token=${API_KEY}`)
  .then(response => response.json())
  .then(data => console.log(data));

The above code fetches the sentiment of news articles related to Apple with the symbol AAPL.

Conclusion

FinnHub's comprehensive and easy-to-use financial data APIs provide a great resource for developers to create rich financial applications. In this post, we explored some of FinnHub's APIs in JavaScript with examples. I hope this article has helped you get started with FinnHub's public API. Happy coding!

Related APIs