The Jewelry Innovations News
Custom API to Fetch Products by Category
In this post, we will discuss how to create a custom API in WordPress that fetches products based on their categories. This API is particularly useful for WooCommerce-based sites where you need to retrieve product information dynamically.
API Overview:
The custom API allows you to fetch products by specifying a category. It includes pagination and token-based authentication for added security.
How to Use the API:
- Endpoint:
- The API endpoint is:
https://www.jewelryinnovations.com/wp-json/api/getProducts
- The API endpoint is:
- Method:
- The API uses the
POST
method.
- The API uses the
- Parameters:
- Authentication Token: To access this API, you need a valid authentication token. If you don’t have a token, please contact our support team at [hrosenthal@jewelryinnovations.com] to request one.
- Category: Specify the name of the product category you want to retrieve. You can pass a single category or multiple categories separated by commas.
-
List of Some Categories you can use to Fetch the Products
-
- per_page: (Optional) The number of products per page. The default is 10.
- page: (Optional) The page number for pagination. The default is 1.
How to Send the Token:
The token must be passed in the request headers using the following format:
“ Authorization: YOUR_TOKEN_VALUE “
Testing the API with Postman:
- Open Postman:
- If you don’t have Postman installed, you can download it from Postman’s official website.
- Create a New Request:
- Click on “New” and then “Request”.
- Set Up the Request:
- Method: Select
POST
. - URL: Enter
https://www.jewelryinnovations.com/wp-json/api/getProducts
.
- Method: Select
- Add Parameters:
- Go to the “Body” tab and select “raw” and then “JSON” from the dropdown.
- Enter the following JSON data:
Single Category JSON data:-
Multiple Categories JSON data:-
- Send the Request:
- Click the “Send” button to make the request.
- You should receive a response with the product data based on the category you specified.
Single Category Example:-
Multiple Categories Example:-
How to Use the Product API: Step-by-Step Guide with Authentication:
Conclusion:
This custom API allows you to fetch products based on their categories efficiently. It includes pagination and token-based authentication for added security.
Comments are closed