• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
webarchers

webarchers

A perfect Blog Junction

  • About Us
  • Hire A Developer
  • Contact
Web Development > Magento 2 > How to create Magento 2 REST API | Magento 2 REST API example

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

August 16, 2021 by Nitin Sharma

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:

  • Consumer Key
  • Consumer Secret
  • Access Token
  • Access Token Secret

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

  • Open postman.
  • Choose method GET/PUT/POST
  • Paste your store URL – example: https://yourwebsite.com/rest/V1/

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

Filed Under: Latest, Magento 2, Web Development Tagged With: API, how to create Magento 2 Rest API, Learn Magento 2, Magento, Magento 2, REST, REST API

Primary Sidebar

Search Here

Featured Posts

How to find the second highest number from an array in PHP

More from this category

  • Top 10 Popular Magento 2 Interview Questions in 2021
  • Magento 2: Get All Product and Stock details using Rest APIs

Latest Posts

  • How to find the second highest number from an array in PHP
  • Top 10 Popular Magento 2 Interview Questions in 2021
  • Magento 2: Get All Product and Stock details using Rest APIs
  • How to create Magento 2 REST API | Magento 2 REST API example
  • [Solution] Popup Minicart when product add to the cart Magento 2

Categories

  • Blog Marketing (2)
  • Content Creation (3)
  • Content Marketing (4)
  • Email Marketing (1)
  • Facebook Marketing (1)
  • How To (2)
  • Infographics (1)
  • JavaScript (1)
  • Latest (4)
  • Local SEO (1)
  • Magento 2 (8)
  • PHP (2)
  • SEO (6)
  • SEO Updates (4)
  • SMO (2)
  • Top 10 (7)
  • Video Marketing (2)
  • Web Development (16)
  • What's Hot (4)
  • WordPress (4)
  • YouTube Channel (2)

Meet Author

I’m a web developer and a blogger as well. I love to create websites and develop what I imagine. Writing is my hobby and I do it well.

Copyright © 2022 WebArchers built with ❤ and Genesis

  • About
  • Contact