Today we discuss the Magento 2 rest API. API is also called web services.
API stands for the Application programming interface
Proccess of the prodcut add to cart usign rest API
- Create customer token
- Create an empty cart/quote using the token
- Add to product to the cart
- Fetch cart details
- Fetch cart totals
- Apply coupon code if you have
- Remove coupon code from the cart
See below steps to easy development.
Create Customer token:
Method:POST URL: your_base_url/rest/V1/integration/customer/token Param:- {"username": "user@example","password": "user@123"} Its return token
Create cart:-
Method :- POST your_base_url/rest/default/V1/carts/mine Authorization: Bearer token
Add to cart:-
Method :- POST URL:your_base_url/rest/V1/carts/mine/items Authorization: Bearer token param { "cartItem": { "sku": "80020", "qty": 1, "quote_id": "937" } }
Get cart Detail:-
Method :- GET
Authorization: Bearer token
URL:your_base_url/rest/V1/carts/mine/items
Get cart totals:-
Method :- GET Authorization: Bearer token URL:your_base_url/rest/V1/carts/mine/totals
Apply coupon code:-
method : PUT Coupon Code : 1. abc URL: your_base_url/rest/default/V1/carts/mine/coupons/abc Authorization: Bearer xyz
Remove coupon code :-
Method :- DELETE URL: your_base_url/rest/default/V1/carts/mine/coupons Payload : { "cartId" :937 } // 937 is a cart id
Enjoy rest of the day!
Related Post : Add product to cart GraphQL Magento 2