Magento – Magento 2 regsitered user add to cart API

addtocartapimagento2

In postman , I tried to add the product to the cart for registered user using this API :

/rest/V1/carts/mine

but I am facing an error like

{
    "message": "Consumer is not authorized to access %resources",
    "parameters": {
        "resources": "self"
    }
}

Best Answer

Follow the below steps by replacing your domain URL and use your customer credentials to login and do the add to cart.

Step 1:-

Get the registered user login details(from website) so that you will get authentication key hence follow the below step first

note:- please note down the response token/key it will useful for next following steps at header section

Method:- POST

API:- http://yoursite.com/rest/V1/integration/customer/token?username=test@gmail.com&password=test@123

screenshot:- [Customer Authentication with Magento through Postman]

Step 2:-

Create Cart using below API call along with the adding header section keys and values as shown below and it will generate cart id.

Method:- POST

API:- http://yoursite.com/rest/V1/carts/mine

screenshot:- [Create new cart in Magento through Postman]

Step 3:- Add item to Cart

Method:- POST

API:- http://yoursite.com/rest/V1/carts/mine/items

Body section: { "cart_item": { "quote_id": "15", "sku": "24-MB03", "qty": 1 } }

screenshot:- [Add a item to cart in Magento through Postman]

Note:- don't forget to add authentication key and value & Content-Type is application/JSON in the postman app at header section. Based on product type this input has to vary.

Step 4:-

Get Cart details with cart items

Method:- GET

API:- http://yoursite.com/rest/V1/carts/mine

screenshot:- [get Cart details/items from Magento through Postman]

Related Topic