Product API

The Product API provides access to product data, making it easy to retrieve information on various products for e-commerce applications, cataloging, or testing purposes.

Endpoint

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

Get Product Data by ID

To retrieve product data by productId, you can use the following endpoint:

https://freeapihub.onrender.com/api/v1/products/:productId

Replace : productId with the actual product ID you want to fetch.

Installation

To use the Product 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/products")
  .then((response) => {
    console.log(response.data);
  })
  .catch((error) => {
    console.error("Error fetching data:", error);
  });

Here’s how to fetch product data by id using Axios:

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

Response Format

Below is a sample JSON response when requesting product data:

{
  "success": true,
  "data": [
    {
      "id": "b1c0e7d2-3f5c-4a4d-bb7a-65a7d9f6a0a9",
      "title": "Wireless Headphones",
      "description": "High-quality wireless headphones with noise-cancellation feature.",
      "price": 129,
      "discountPercentage": 15,
      "rating": 5,
      "stock": 45,
      "brand": "SoundMaster",
      "category": "Electronics",
      "thumbnail": "https://example.com/images/product1-thumbnail.jpg",
      "images": [
        "https://example.com/images/product1-1.jpg",
        "https://example.com/images/product1-2.jpg",
        "https://example.com/images/product1-3.jpg"
      ]
    }
  ],
  "message": "All products retrieved successfully"
}

Error Handling

When using the Product 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 product data:

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

Usage Tips

  • Use the Product API to retrieve product data for e-commerce applications, product listings, or inventory management systems.
  • Experiment with different API calls to explore the variety of products available and to understand the structure and attributes returned.
  • For a deeper understanding of how to effectively use APIs, refer to the API Usage Guide.

Happy coding with the Product API! 🎉