Magento 2 REST API

📚 Introduction to Magento 2 REST API

Magento 2 REST API allows you to connect to your Magento store programmatically. You can manage products, customers, orders, and more through API calls. Whether you’re building a custom application or integrating with third-party services, the REST API is essential for efficient store management.

In this tutorial, we’ll explore the basics of the REST API in Magento 2, showing you how to make requests, authenticate, and interact with store data.

🧠 What is REST API?

REST (Representational State Transfer) is a web service architecture that allows you to interact with resources via HTTP methods like GET, POST, PUT, and DELETE. Magento 2’s REST API follows this pattern, enabling you to:

  • Access store data (products, orders, customers, etc.)
  • Create, update, and delete resources
  • Use JSON as the format for responses

🔑 Authentication

To interact with the Magento 2 REST API, you’ll need an access token. You can generate an API key either as a customer or an admin.

💡 How to Generate an Admin Token

To get an admin token, you need to send a POST request to the following URL:

POST /rest/V1/integration/admin/token

Try It Now

In the request body, pass your admin username and password like this:

{
  "username": "admin",
  "password": "adminpassword"
}

Try It Now

🚀 Making Your First API Call

Now that you have your token, let’s make an API call to fetch a list of products. Here’s an example of a GET request to retrieve the first page of products:

🔧 GET /rest/V1/products

GET /rest/V1/products?searchCriteria[pageSize]=10&pageSize=5
Authorization: Bearer 

Try It Now

Replace with your actual admin token.

📄 Response Example

The response will be in JSON format and look something like this:

{
  "items": [
    {
      "id": 1,
      "sku": "t-shirt",
      "name": "Cool T-Shirt",
      "price": 29.99
    },
    {
      "id": 2,
      "sku": "hat",
      "name": "Stylish Hat",
      "price": 15.99
    }
  ]
}

Try It Now

📦 Creating a Product via API

You can also use the REST API to create new products. Here’s an example of a POST request to add a new product:

🔧 POST /rest/V1/products

POST /rest/V1/products
Authorization: Bearer 
Content-Type: application/json
{
  "product": {
    "sku": "cool-new-shirt",
    "name": "Cool New Shirt",
    "price": 25.99,
    "status": 1,
    "type_id": "simple",
    "attribute_set_id": 4,
    "weight": 1.2,
    "extension_attributes": {
      "stock_item": {
        "qty": 100,
        "is_in_stock": true
      }
    }
  },
  "saveOptions": true
}

Try It Now

Make sure to include all the required product details in the JSON body.

📄 Response Example

The response will contain the newly created product’s ID and other details:

{
  "id": 101,
  "sku": "cool-new-shirt",
  "name": "Cool New Shirt",
  "price": 25.99
}

Try It Now

🛠️ Updating a Product via API

To update an existing product, you can use the following PUT request:

🔧 PUT /rest/V1/products/{sku}

PUT /rest/V1/products/cool-new-shirt
Authorization: Bearer 
Content-Type: application/json
{
  "product": {
    "price": 20.99
  }
}

Try It Now

❌ Deleting a Product via API

To delete a product, use the DELETE request:

🔧 DELETE /rest/V1/products/{sku}

DELETE /rest/V1/products/cool-new-shirt
Authorization: Bearer 

Try It Now

✅ Conclusion

In this tutorial, we explored how to use Magento 2’s REST API to interact with your store. We covered authentication, making GET/POST requests, and managing products programmatically. With this knowledge, you can automate tasks, integrate with other systems, and more.

In the next tutorial, we’ll dive deeper into advanced API topics like custom endpoints and authentication methods.