Stock API

The Stock API provides access to stock price data, enabling you to add stock information to your applications.

Endpoint

https://freeapihub.onrender.com/api/v1/stocks

Get stock Data by ID

To retrieve stock data by ID, you can use the following endpoint:

https://freeapihub.onrender.com/api/v1/stocks/:stockId

Replace : stockId with the actual stock ID you want to fetch.

Installation

To use the Stock API, you’ll need to install Axios. You can do this using either npm or pnpm:

Note: Run these commands in your project directory to install Axios.

Using npm

npm install axios

Using pnpm

pnpm add axios

Example Usage

Here’s how to fetch data using Axios:

import axios from "axios";
 
axios
  .get("https://freeapihub.onrender.com/api/v1/stocks")
  .then((response) => {
    console.log(response.data);
  })
  .catch((error) => {
    console.error("Error fetching data:", error);
  });

Here’s how to fetch stock data by stockId using Axios:

import axios from "axios";
 
const stockId = "1a2b3c4d"; // Replace with actual stock ID
 
axios
  .get(`https://freeapihub.onrender.com/api/v1/stocks/${stockId}`)
  .then((response) => {
    console.log(response.data);
  })
  .catch((error) => {
    console.error("Error fetching data by ID:", error);
  });

Response Format

Below is a sample JSON response when requesting stock data:

{
  "success": true,
  "data": [
    {
      "id": "a3f47c12-5e6d-4a6a-9d9c-1c4b9e5fbc1d",
      "name": "Tech Solutions Inc.",
      "symbol": "TSOL",
      "listingDate": "2023-03-15T00:00:00.000Z",
      "isin": "US1234567890",
      "marketCap": 5700000000.0,
      "currentPrice": 320.5,
      "highLow": "345.5/280.3",
      "stockPE": 25.4,
      "bookValue": 52.3,
      "dividendYield": 1.5,
      "roce": 18.3,
      "roe": 20.1
    }
  ],
  "message": "All stocks retrieved successfully"
}

Error Handling

When using the Stock API, it’s essential to handle potential errors effectively. Below are common error codes you may encounter:

Error CodeDescription
404Resource not found
429Too many requests (rate limit)

Example Error Handling

To manage potential errors gracefully, you can use a try…catch block in your Axios requests. Here’s an example of how to implement error handling when fetching stock data:

import axios from "axios";
 
async function fetchStockData() {
  try {
    const response = await axios.get("https://freeapihub.onrender.com/api/v1/stocks");
    console.log(response.data);
  } catch (error) {
    console.error(
      "Error fetching stock data:",
      error.response?.data?.message || "An unexpected error occurred."
    );
  }
}
 
fetchStockData();

Usage Tips

  • Use the Stock API to add financial data to your applications for real-time stock monitoring.
  • Experiment with different stock symbols to see the variety of data returned, helping you understand the structure and attributes available.
  • For a deeper understanding of how to effectively use APIs, refer to the API Usage Guide.

Happy coding with the Stock API! 🎉