Site icon webarchers

How to create Magento 2 REST API | Magento 2 REST API example

Have you ever come across these kinds of questions – What is Magento 2 REST API? How to use Magento 2 REST API? I think yes, that’s why you are here. In this rich post you are going to get the answers of your questions with examples. So, just jump in the post and learn about the APIs in Magento 2.

The questions:

  1. What is Magento 2 REST API
  2. How to create/use Magento 2 REST API

What is Magento 2 Rest API

Some time you need to integrate you store to a third party app in order to exchange some piece of information or amount of data. In that situation Magento 2 REST APIs plays the role.

Let suppose you want to send the order details to a third party service what you will do? The answer is – you will use REST API to send the details.

“The Magento 2 REST API can be used to perform requests and receive responses by using the HTTP protocol.”

How to create/use Magento 2 REST API

You need to create a user with all or selected privilege to get access token to access the API. So, the first step is integration-

1. First setup Integrations

1.1 Open the System > Extensions > Integrations

1.2 Add New Integration

1.3 Set the integration Name and other settings, then specify your Magento 2 back-end password in the Your Password field

1.4 Switch to the API sidebar tab and chose all or select the resources which will be available to OAuth clients:

1.5 Press the Save button. The integration will be saved and the Integrations list will be shown again. Press the Activate link in the integration row:

1.6 A confirmation screen will be shown. Press the Allow button

1.7 The credentials screen will be shown. Use them in your third-party software to access your Magento 2 as OAuth server.

You will see:

Copy it to somewhere, and then press the Done button.

2. See how Magento 2 API works

Now we have all the required keys. Let’s play with them. Please Copy the Access Token we get in our previous step. And follow the steps below

Set Header: Authorization: Bearer <Access Token>, Content-Type: application/json (As shown in image)

Now you are all set to use your postman to fetch your Magento 2 store data. Let’s see how-

Role: To update product stock and price etc. using SKUs

URL: http://yourwebsite.com/rest/V1/products/{sku} 

Method: PUT

Header: Authorization: Bearer <Access Token>, Content-Type: application/json

Token: your access token

Payload:

{
  "product": {
    "price": 70,
    "extension_attributes": {
        "stock_item": {
            "qty": 400
        }
    }
  }
}

Response: Full updated json

Role: To update order status of given increment id

URL: http://yourwebsite.com/rest/V1/orders

Method: POST

Header: Authorization: Bearer <Access Token>, Content-Type: application/json

Payload:

{
    "entity": {
        "entity_id": 5,
	 "increment_id": 00000054,
        "state":"processing",
        "status": "processing"
    }
}

Response: Updated order json

Role: To fetch order json using order id

URL: http://yourwebsite.com/rest/V1/orders/{orderid}

Method: GET

Header: Authorization: Bearer <Access Token>, Content-Type: application/json

Payload: None

Response: Full Json or related order id

Role: To fetch orders using date range

URL: http://yourwebsite.com/rest/V1/orders?searchCriteria[filter_groups][0][filters][0][field]=updated_at&searchCriteria[filter_groups][0][filters][0][value]=2021-01-01T14:01:54.9571247Z&searchCriteria[filter_groups][0][filters][0][condition_type]=gt

Method: GET

Header: Authorization: Bearer <Access Token>, Content-Type: application/json

Payload: None

Response: Full Json

Role: To fetch all products with stock at once

URL: magentositeURL/rest/V1/products?searchCriteria[page_size]=20

Method: GET

Header: Authorization: Bearer <Access Token>, Content-Type: application/json

Payload: None

Response: Full Json

Role: To fetch order from given date range

URL: https://yourwebsite.com/rest/V1/orders?searchCriteria[filter_groups][0][filters][0][field]=created_at&searchCriteria[filter_groups][0][filters][0][value]=2021-01-01T14:01:54.9571247Z&searchCriteria[filter_groups][0][filters][0][condition_type]=from&searchCriteria[filter_groups][1][filters][1][field]=created_at&searchCriteria[filter_groups][1][filters][1][value]=2021-02-01T22:22:10&searchCriteria[filter_groups][1][filters][1][condition_type]=to

Method: GET

Header: Authorization: Bearer <Access Token>, Content-Type: application/json

Payload: None

Response: Full Json

Exit mobile version