How To Get A Product By REST API and GraphQL In Magento 2

How To Get A Product By REST API and GraphQL In Magento 2

Today, let’s learn how to get a product from your Magento website by using the REST API and GraphQL.

1. Get A Product With REST API

You can use a tool to access the API. In this situation, we will use Postman.

We need to get the access token of the admin user to get a product.

Endpoint: “http(s)://yourdomain.com/rest/V1/integration/admin/token”

Method: POST

Request:

{
	"username" : "string",
	"password" : "string"
}

Response: “token” (string)

For example:

API

In the first step, we get the access token of the admin user to get a product.

Endpoint: “http(s)://yourdomain.com/rest/V1/integration/admin/token”

Method: POST

Request:

{
	"username" : "string",
	"password" : "string"
}

Response: “token” (string)

For example:

API

Now, we have the access token. Let begin to get a product from API.

Endpoint: “http(s)://yourdomain.com/rest/V1/ products/:sku”

Method: GET

Request: sku.

Header: Authorization: Bearer (Token)

Response: token(string)

For example:

Magento API

You need to check the access token. If it is correct, the API will return Product’s Information.

REST API

If the access token is incorrect, the API will return the error message like the following:

Magento REST

This is a process to get a product from REST API

2. Get A Product With GraphQL

Now we will get a product but this time we will use GraphQL instead of using the REST API.

Endpoint: “http(s)://yourdomain.com/graphql”

Method: POST

Syntax:  products(
    search: String
    filter: ProductFilterInput
    pageSize: Int
    currentPage: Int
    sort: ProductSortInput
): Products


Request example:

{
  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
            }
        }
    }
}

For example:

Then, you write down the endpoint and your request:

REST

After that, you can click run to send a request to the server. We hope that this instruction will help you get a product through API easily. If you have more questions, leave a comment below.

Related Posts:

How To Get Categories Through Magento 2 REST API?

How To Get Product List (Catalog) Through Magento 2 API?

How To Create API Request In Magento 2?