{"id":2584,"date":"2024-12-31T20:23:00","date_gmt":"2024-12-31T20:23:00","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/magento-2-api-create-cart-add-products-to-cart\/"},"modified":"2025-05-14T12:18:22","modified_gmt":"2025-05-14T06:48:22","slug":"magento-2-api-create-cart-add-products-to-cart","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/magento-2-api-create-cart-add-products-to-cart\/","title":{"rendered":"Magento 2 API &#8211; Create Cart &#038; Add Products to Cart"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Hello, Magento peeps!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In my previous blog post, I talked about resetting the customer password using Magento 2 API. In case you missed it, you can read it at&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/magento-2-api-reset-customer-password\/\" target=\"_blank\" rel=\"noreferrer noopener\">Magento 2 API \u2013 Reset Customer Password<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Today, I will show you we can create a cart and add products. In case you are developing a new storefront or mobile app based on Magento 2, you may require using a set of Magento 2 APIs to integrate everything together. To allow the customers to add products to the cart, the client must call an API request to Magento 2 to create a cart first. In this blog post, I will guide you completely through the Magento 2 API for creating a cart and adding products to it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So, let\u2019s begin with the\u00a0Magento 2 API \u2013 Create Cart &amp; Add Products to Cart!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to Create a Cart and Add Products to It Using Magento 2 API?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The API request to create a cart and add products to it in Magento 2 requires customer-level access, and therefore the client must pass the customer token to validate the request. The customer token is generated by Magento 2 after the validation of the customer\u2019s credentials. You may love to read this blog post on&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/magento-2-api-get-customer-token\/\" target=\"_blank\" rel=\"noreferrer noopener\">Magento 2 API \u2013 Get Customer Token<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In order to add products to the cart, the client first needs to send a cart creation request to Magento 2 API. The request returns a quote ID, which can be further used to add items to the cart using Magento 2 API.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Create a Cart Using Magento 2 API (Magento 2 REST API Get Cart Id)<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">In order to get the cart id using Magento 2 API, the client needs to send a POST request to the&nbsp;<em>store_url\/rest\/default\/V1\/carts\/mine<\/em>&nbsp;endpoint with the customer token in the header. An example of the same is provided below.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Method:&nbsp;<\/strong>POST<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>URL:&nbsp;<\/strong><em>store_url\/rest\/default.V1\/carts\/mine<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Header:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Bearer Token:&nbsp;<\/strong>&lt;Customer_token&gt;<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2022\/06\/Get-Cart-ID-1.png\" alt=\"Add Products to Cart\" class=\"wp-image-50657\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Response:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Once the customer token is validated, the API returns the quote id, which can be later used for adding or removing products from the cart using Magento 2 API.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2:&nbsp;Add Products to Cart Using Magento 2 API<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">We have successfully created a cart in Magento 2, and now it\u2019s time to add items to it. Adding items to the cart using Magento 2 REST API requires the product SKU, quantity, and quote id of the cart. The client needs to send a POST request to the&nbsp;<em>store_url\/rest\/default\/V1\/carts\/mine\/items&nbsp;<\/em>endpoint along with the required details in the payload and customer access token to add products to the cart using Magento 2 rest API. Let\u2019s understand it in a better way, using an example.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Method:&nbsp;<\/strong>POST<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>URL:&nbsp;<\/strong><em>store_url\/rest\/V1\/carts\/mine\/items<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Header:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Bearer Token:&nbsp;<\/strong>&lt;Customer_token&gt;<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Body:<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">{  \"cartItem\":{\n \"sku\": \"Tshirt\",\n \"qty\": 1,\n \"quote_id\": \"51\"}\n}<\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2022\/06\/Add-Products-to-Cart.webp\" alt=\"Get Cart ID\" class=\"wp-image-50659\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Response:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">On successful authorization of the customer token, Magento 2 responds with the following information:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">{\n    \"item_id\": 81,\n    \"sku\": \"Tshirt\",\n    \"qty\": 1,\n    \"name\": \"Tshirt\",\n    \"price\": 100,\n    \"product_type\": \"simple\",\n    \"quote_id\": \"82\"\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You\u2019ve successfully created a cart and added products to it using Magento 2 API.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A shopping cart is one of the must-have functionalities for online stores and shopping apps. The Magento 2 API to create a cart and products to it is helpful for creating such functionalities in third-party developments based on Magento 2. I hope this blog post will help you while creating such functionalities in your third-party development. In case you still have any queries or doubts regarding Magento 2 API, feel free to comment. I will be happy to help you. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Also, do not forget to share this blog post with your developer friends via social media. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thanks for reading. <\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-8f761849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button btn-orange\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/meetanshi.com\/blog\/magento-2-api-reset-customer-password\/\">Magento 2 API \u2013 Reset Customer Password<\/a><\/div>\n<\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button btn-orange\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/meetanshi.com\/blog\/magento-2-api-set-shipping-billing-information\/\">Magento 2 API \u2013 Set Shipping &amp; Billing Information<\/a><\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Hello, Magento peeps! In my previous blog post, I talked about resetting the customer password using Magento 2 API. In case you missed it, you&#8230;<\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-2584","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/2584","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=2584"}],"version-history":[{"count":4,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/2584\/revisions"}],"predecessor-version":[{"id":13501,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/2584\/revisions\/13501"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=2584"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=2584"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=2584"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}