To satisfy the increasing demands of e-commerce businesses, Magento 2 now provides Application Programming Interface (API) that lets developers work with the platform’s data.
This guide will show you how to get the product list from a Magento 2 website by using the REST API and GraphQL.
Let’s get started and unlock the potential of the Magento 2 API to access all products for your e-commerce needs.
Get All Products Data With REST API
You can use a tool to access the API. Here we are using Postman.
To get product list data, we need to get the access token of the admin user:
Endpoint: “http(s)://yourdomain.com/rest/V1/integration/admin/token”
Method: POST
Request:
{ "username" : "string", "password" : "string" }
Response: “token” (string)
Example:
Magento system will check whether that username is available or not, then the API will return the access token as a string.
For example:
If your username and password are incorrect, the API will return an error like the following:
After having the access token, we can get product list information from the API.
Endpoint: “http(s)://yourdomain.com/rest/V1/ products”
Method: GET
Request: searchCriteria.
Header: Authorization: Bearer (Token)
Response: token(string)
For example:
If the access token is correct, the API will return the customer’s information.
For example:
If the access token is incorrect, the API will return an error.
For example:
That’s how we can get the product list data using the Magento REST API.
Bonus: Get All Products Data With GraphQL
Now, let’s move on to getting the catalog information using Magento 2 GraphQL.
Endpoint: “http(s)://yourdomain.com/rest/V1/ products”
Method: POST
Syntax: products( search: String filter: ProductFilterInput pageSize: Int currentPage: Int sort: ProductSortInput ): Products
Request example:
First, write down the endpoint and your request:
{ products( filter: { sku: { like: "24-WB%" } } pageSize: 20 currentPage: 1 sort: { name: DESC } ) { items { sku } filters { name filter_items_count request_var filter_items { label value_string items_count } } } }
Response:
{ "data": { "products": { "total_count": 26, "items": [ { "name": "BUNDLE: Tiny Round \"Design Your Own\" Children's Necklace for Girls (50+ Optional Charms & FREE Engraving)", "sku": "Tiny Round", "price": { "regularPrice": { "amount": { "currency": "USD", "value": 439.78 } } } }], "page_info": { "page_size": 25, "current_page": 1 } } } }
Now click run to send the request to the server.
API will return a response that has data like this:
Wrap Up
In conclusion, knowing how to get all products through the Magento 2 API allows you to efficiently manage your e-commerce data.
By following the steps outlined in this blog post, you can make use of the full potential of the API and streamline your business processes. Follow the guide to take control of your product data efficiently and run your business smoothly.
Sir I am a bignner in magento. This article help alot. Thank you.
Glad we could help, Rashid!