Skip to content
intermediate Phase 46 · Checkout Implementation

Checkout APIs

REST APIs for cart and checkout management, cart management API, and order placement API

45m
0 problems
Topic Progress 0%

Cart Management API

Cart Endpoints

# Create empty cart (guest)
POST /rest/V1/carts
Response: 145  (cart ID)

# Create cart for logged-in customer
POST /rest/V1/carts/mine
Response: 145

# Get cart by ID
GET /rest/V1/carts/{cartId}

# Get customer's active cart
GET /rest/V1/carts/mine

# Add item to cart
POST /rest/V1/carts/{cartId}/items
{
    "cartItem": {
        "sku": "simple-product",
        "qty": 2,
        "quote_id": 145
    }
}

# Update cart item
PUT /rest/V1/carts/{cartId}/items/{itemId}
{
    "cartItem": {
        "item_id": 5,
        "qty": 3,
        "quote_id": 145
    }
}

# Remove item from cart
DELETE /rest/V1/carts/{cartId}/items/{itemId}

# Delete cart
DELETE /rest/V1/carts/{cartId}

Guest Cart Operations

// Guest cart uses cart ID instead of auth token
// All endpoints work with /V1/guest-carts/{cartId} prefix

// Guest add item
POST /rest/V1/guest-carts/{cartId}/items
{
    "cartItem": {
        "sku": "simple-product",
        "qty": 1,
        "quote_id": "{guest-cart-id}"
    }
}

Shipping Information API

Set Shipping Address and Method

# Customer cart
POST /rest/V1/carts/mine/shipping-information
{
    "addressInformation": {
        "shipping_address": {
            "firstname": "John",
            "lastname": "Doe",
            "street": ["123 Main St"],
            "city": "New York",
            "region_id": 43,
            "postcode": "10001",
            "country_id": "US",
            "telephone": "555-1234",
            "email": "john@example.com"
        },
        "shipping_method_code": "flatrate",
        "shipping_carrier_code": "flatrate"
    }
}

# Guest cart
POST /rest/V1/guest-carts/{cartId}/shipping-information
// Same request body

Estimate Shipping Rates

# Get estimated shipping methods
POST /rest/V1/carts/mine/shipping-methods
{
    "address": {
        "country_id": "US",
        "postcode": "10001",
        "region_id": 43
    }
}

Response:
[
    {
        "carrier_code": "flatrate",
        "method_code": "flatrate",
        "carrier_title": "Flat Rate",
        "method_title": "Fixed",
        "price": 5.00
    },
    {
        "carrier_code": "tablerate",
        "method_code": "bestway",
        "carrier_title": "Table Rate",
        "method_title": "Best Way",
        "price": 7.50
    }
]

Payment and Order Placement

Set Payment and Place Order

# Set payment method and place order
POST /rest/V1/carts/mine/payment-information
{
    "paymentMethod": {
        "method": "checkmo"
    },
    "billingAddress": {
        "firstname": "John",
        "lastname": "Doe",
        "street": ["123 Main St"],
        "city": "New York",
        "region_id": 43,
        "postcode": "10001",
        "country_id": "US",
        "telephone": "555-1234",
        "email": "john@example.com"
    }
}

Response: 1  (order ID)

# Guest cart order placement
POST /rest/V1/guest-carts/{cartId}/payment-information
// Same request body

Payment Information Only

# Get available payment methods
GET /rest/V1/carts/mine/payment-methods

Response:
[
    {
        "code": "checkmo",
        "title": "Check / Money Order"
    },
    {
        "code": "banktransfer",
        "title": "Bank Transfer"
    }
]

# Set payment method without placing order
POST /rest/V1/carts/mine/set-payment-information
{
    "paymentMethod": {
        "method": "checkmo"
    },
    "billingAddress": {...}
}

Complete Checkout Flow (PHP)

namespace Vendor\Checkout\Service;

class CheckoutApi
{
    public function __construct(
        private \Magento\Quote\Api\CartRepositoryInterface $cartRepository,
        private \Magento\Quote\Api\GuestCartRepositoryInterface $guestCartRepository,
        private \Magento\Checkout\Api\Data\PaymentInformationInterfaceFactory $paymentInfoFactory,
        private \Magento\Checkout\Model\PaymentInformationManagement $paymentInfoManagement
    ) {}

    public function placeOrder(
        int $cartId,
        array $paymentData
    ): int {
        $cart = $this->cartRepository->get($cartId);

        // Set shipping
        $shippingAddress = $cart->getShippingAddress();
        $shippingAddress->setShippingMethod('flatrate_flatrate');
        $cart->save();

        // Set payment and place order
        $paymentInfo = $this->paymentInfoFactory->create();
        $paymentInfo->setPaymentMethod($paymentData['method']);
        $paymentInfo->setBillingAddress($this->createAddress($paymentData['billing']));

        $orderId = $this->paymentInfoManagement->savePaymentInformationAndPlaceOrder(
            $cartId,
            $paymentInfo
        );

        return $orderId;
    }
}

Quiz

1. What is the difference between /V1/carts and /V1/guest-carts?

Question 1 options

2. What does the payment-information endpoint return?

Question 2 options

3. How do you get available shipping methods before selection?

Question 3 options

Flashcards

Question

Create guest cart endpoint?

Answer

POST /rest/V1/carts

Question

Add item to cart endpoint?

Answer

POST /rest/V1/carts/{cartId}/items

Question

Place order endpoint?

Answer

POST /rest/V1/carts/mine/payment-information

Question

Guest cart URL pattern?

Answer

/rest/V1/guest-carts/{cartId}/...

Revision Notes

Key Takeaways

  • 1. Cart management uses /V1/carts (auth) and /V1/guest-carts (guest)
  • 2. Shipping information is set via POST to shipping-information endpoint
  • 3. Order placement combines payment method and billing address in one call
  • 4. Payment methods are retrieved via GET before order placement
  • 5. All cart operations support both customer and guest variants

Interview Tips

  • Walk through the complete checkout API flow: cart → items → shipping → payment → order
  • Explain the difference between authenticated and guest cart endpoints
  • Discuss how shipping rates are estimated before method selection

Cheat Sheet

Checkout API Flow:
  1. POST /V1/carts (create)
  2. POST /V1/carts/{id}/items (add products)
  3. POST /V1/carts/mine/shipping-information (address + method)
  4. POST /V1/carts/mine/payment-information (payment + place order)

Guest: Replace /mine/ with /guest-carts/{cartId}/
Auth: Bearer token in Authorization header