In this article, we will look into how we can add products to the cart and complete the checkout process by placing an order using Magento API.
The APIs used to create a cart, add products to the cart, place an order, etc. require customer token authentication.
Create Shopping Cart
We create an empty cart and quote for a specified customer if the customer does not have a cart yet. The quote id (cart id) is necessary for another API that adds products to cart.
API Doc:
https://adobe-commerce.redoc.ly/2.4.5-admin/tag/cartsmine#operation/PostV1CartsMine
Endpoint:
GET <host>/rest/V1/carts/mine
Authentication Bearer Token:
<customer-token>
Response:
Returns newly created quote id for the customer.
3
Create Shopping Cart using cURL Request
curl --location --request POST 'https://app.your-project.test/rest/V1/carts/mine' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer eyJraWQiOiIxIiwiYWxnIjoiSFMyNxxxxx' \
--data-raw ''
Add product to cart
API Doc:
https://adobe-commerce.redoc.ly/2.4.5-admin/tag/cartsmineitems#operation/PostV1CartsMineItems
Endpoint:
GET <host>/rest/V1/carts/mine/items
Authentication Bearer Token:
<customer-token>
Payload:
We are passing the quote_id
that we generated above.
{
"cartItem": {
"sku": "24-MB02",
"qty": 1,
"quote_id": "3"
}
}
Response:
{
"item_id": 5,
"sku": "24-MB02",
"qty": 1,
"name": "Fusion Backpack",
"price": 59,
"product_type": "simple",
"quote_id": "3"
}
Add product to cart using cURL Request
curl --location --request POST 'https://app.your-project.test/rest/V1/carts/mine/items' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer eyJraWQiOiIxIiwiYWxnIjoiSFMyNxxxxx' \
--data-raw '{
"cartItem": {
"sku": "24-MB02",
"qty": 1,
"quote_id": "3"
}
}'
Set Shipping Address & Shipping Method
Get available shipping methods
We pass the customer address to the estimate-shipping-methods API endpoint. This will return the available shipping methods for that particular address.
API Doc:
https://adobe-commerce.redoc.ly/2.4.5-admin/tag/cartsmineestimate-shipping-methods
Endpoint:
GET <host>/rest/V1/carts/mine/estimate-shipping-methods
Authentication Bearer Token:
<customer-token>
Payload:
{
"address": {
"region": "New York",
"region_id": 43,
"region_code": "NY",
"country_id": "US",
"street": [
"1582 James Street"
],
"postcode": "14623",
"city": "Rochester",
"firstname": "Test",
"lastname": "Example",
"email": "test@example.com",
"telephone": "585-598-8140",
"same_as_billing": 1,
"save_in_address_book": 0
}
}
Response:
[
{
"carrier_code": "flatrate",
"method_code": "flatrate",
"carrier_title": "Flat Rate",
"method_title": "Fixed",
"amount": 5,
"base_amount": 5,
"available": true,
"error_message": "",
"price_excl_tax": 5,
"price_incl_tax": 5
},
{
"carrier_code": "tablerate",
"method_code": "bestway",
"carrier_title": "Best Way",
"method_title": "Table Rate",
"amount": 15,
"base_amount": 15,
"available": true,
"error_message": "",
"price_excl_tax": 15,
"price_incl_tax": 15
}
]
Get available shipping methods using cURL Request
curl --location --request POST 'https://app.your-project.test/rest/V1/carts/mine/estimate-shipping-methods' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer eyJraWQiOiIxIiwiYWxnIjoiSFMyNxxxxx' \
--data-raw '{
"address": {
"region": "New York",
"region_id": 43,
"region_code": "NY",
"country_id": "US",
"street": [
"1582 James Street"
],
"postcode": "14623",
"city": "Rochester",
"firstname": "Test",
"lastname": "Example",
"email": "test@example.com",
"telephone": "585-598-8140",
"same_as_billing": 1,
"save_in_address_book": 0
}
}'
Set Shipping and Billing Address
We pass the billing address, shipping address, and shipping method to the shipping-information API endpoint. This will return the available payment methods.
API Doc:
https://adobe-commerce.redoc.ly/2.4.5-admin/tag/cartsmineshipping-information
Endpoint:
GET <host>/rest/V1/carts/mine/shipping-information
Authentication Bearer Token:
<customer-token>
Payload:
{
"addressInformation": {
"shipping_address": {
"region": "New York",
"region_id": 43,
"region_code": "NY",
"country_id": "US",
"street": [
"1582 James Street"
],
"postcode": "14623",
"city": "Rochester",
"firstname": "Test",
"lastname": "Example",
"email": "test@example.com",
"telephone": "585-598-8140"
},
"billing_address": {
"region": "New York",
"region_id": 43,
"region_code": "NY",
"country_id": "US",
"street": [
"1582 James Street"
],
"postcode": "14623",
"city": "Rochester",
"firstname": "Test",
"lastname": "Example",
"email": "test@example.com",
"telephone": "585-598-8140"
},
"shipping_method_code": "flatrate",
"shipping_carrier_code": "flatrate"
}
}
Response:
{
"payment_methods": [
{
"code": "checkmo",
"title": "Check / Money order"
},
{
"code": "cashondelivery",
"title": "Cash On Delivery"
}
],
"totals": {
"grand_total": 64,
"base_grand_total": 64,
"subtotal": 59,
"base_subtotal": 59,
"discount_amount": 0,
"base_discount_amount": 0,
"subtotal_with_discount": 59,
"base_subtotal_with_discount": 59,
"shipping_amount": 5,
"base_shipping_amount": 5,
"shipping_discount_amount": 0,
"base_shipping_discount_amount": 0,
"tax_amount": 0,
"base_tax_amount": 0,
"weee_tax_applied_amount": null,
"shipping_tax_amount": 0,
"base_shipping_tax_amount": 0,
"subtotal_incl_tax": 59,
"shipping_incl_tax": 5,
"base_shipping_incl_tax": 5,
"base_currency_code": "USD",
"quote_currency_code": "USD",
"items_qty": 1,
"items": [
{
"item_id": 5,
"price": 59,
"base_price": 59,
"qty": 1,
"row_total": 59,
"base_row_total": 59,
"row_total_with_discount": 0,
"tax_amount": 0,
"base_tax_amount": 0,
"tax_percent": 0,
"discount_amount": 0,
"base_discount_amount": 0,
"discount_percent": 0,
"price_incl_tax": 59,
"base_price_incl_tax": 59,
"row_total_incl_tax": 59,
"base_row_total_incl_tax": 59,
"options": "[]",
"weee_tax_applied_amount": null,
"weee_tax_applied": null,
"name": "Fusion Backpack"
}
],
"total_segments": [
{
"code": "subtotal",
"title": "Subtotal",
"value": 59
},
{
"code": "shipping",
"title": "Shipping & Handling (Flat Rate - Fixed)",
"value": 5
},
{
"code": "tax",
"title": "Tax",
"value": 0,
"extension_attributes": {
"tax_grandtotal_details": []
}
},
{
"code": "grand_total",
"title": "Grand Total",
"value": 64,
"area": "footer"
}
]
}
}
Set shipping and billing address using cURL Request
curl --location --request POST 'https://app.your-project.test/rest/V1/carts/mine/shipping-information' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer eyJraWQiOiIxIiwiYWxnIjoiSFMyNxxxxx' \
--data-raw '{
"addressInformation": {
"shipping_address": {
"region": "New York",
"region_id": 43,
"region_code": "NY",
"country_id": "US",
"street": [
"1582 James Street"
],
"postcode": "14623",
"city": "Rochester",
"firstname": "Test",
"lastname": "Example",
"email": "test@example.com",
"telephone": "585-598-8140"
},
"billing_address": {
"region": "New York",
"region_id": 43,
"region_code": "NY",
"country_id": "US",
"street": [
"1582 James Street"
],
"postcode": "14623",
"city": "Rochester",
"firstname": "Test",
"lastname": "Example",
"email": "test@example.com",
"telephone": "585-598-8140"
},
"shipping_method_code": "flatrate",
"shipping_carrier_code": "flatrate"
}
}'
Create Order / Place Order
This will set payment information and place order for the specified cart.
We pass the payment method and billing address to the payment-information API endpoint. This will return the newly created Order ID.
Endpoint:
GET <host>/rest/V1/carts/mine/payment-information
Authentication Bearer Token:
<customer-token>
Payload:
{
"paymentMethod": {
"method": "checkmo"
},
"billing_address": {
"region": "New York",
"region_id": 43,
"region_code": "NY",
"country_id": "US",
"street": [
"1582 James Street"
],
"postcode": "14623",
"city": "Rochester",
"firstname": "Test",
"lastname": "Example",
"email": "test@example.com",
"telephone": "585-598-8140"
}
}
Response:
Newly created Order ID is returned.
"3"
Create Order / Place Order using cURL Request
curl --location --request POST 'https://app.your-project.test/rest/V1/carts/mine/payment-information' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer eyJraWQiOiIxIiwiYWxnIjoiSFMyNxxxxx' \
--data-raw '{
"paymentMethod": {
"method": "checkmo"
},
"billing_address": {
"region": "New York",
"region_id": 43,
"region_code": "NY",
"country_id": "US",
"street": [
"1582 James Street"
],
"postcode": "14623",
"city": "Rochester",
"firstname": "Test",
"lastname": "Example",
"email": "test@example.com",
"telephone": "585-598-8140"
}
}'
Hope this helps. Thanks.
Magento REST API Reference: https://developer.adobe.com/commerce/webapi/rest/quick-reference/