How to place an order for guest customer by REST API Magento 2?

Which steps are required to place an order for a Guest Customer by REST API?

You must follow the steps to place an order without creating a customer account. You can add items to your cart and place an order with the Guest Customer.

You have to follow six Steps for the REST API to Place an order by Guest Customer,

1) Create Guest Empty Cart
2) Get Cart Id by token(masked_id) return from Step 1.
3) Add an Item to the Cart for the Guest.
4) Fetch estimate-shipping-methods for the cart.
5) Fetch the available Payment method for the cart.
6) Place an Order for the Guest customer.

Step 1) Create Guest Empty Cart

Guest Customers need to create an empty cart(quote) with the REST API. The response will be a cart_id that will be passed to a subsequent API to place an order.

Guest Create-empty-cart
Guest Create-empty-cart REST API

Action: POST

API URL: <SITE_URL>/rest/<store_code>/V1/guest-carts
(Example: https://magento.test/rest/default/V1/guest-carts)

Payload: Empty

Response: w04QKi5YeYh6CC1tTFbpY1sPNKhbrMIZ

Step 2) Get Cart Id by token(masked_id) return from Step 1.

In the second step, you can get quote id by calling API, you have to replace :cartId with response return in step 1.

Get Quote id by masked id
Guest Customer Get Cart id by masked id

Action: GET

API URL: <SITE_URL>/rest/<store_code>/V1/guest-carts/:cartId
(Example: https://magento.test/rest/V1/guest-carts/w04QKi5YeYh6CC1tTFbpY1sPNKhbrMIZ)

Payload: Empty

Response:

{
    "id": 1187676,
    "created_at": "2022-07-05 04:23:12",
    "updated_at": "2022-07-05 04:23:12",
    "is_active": true,
    "is_virtual": false,
    "items": [],
    "items_count": 0,
    "items_qty": 0,
    "customer": {
        "email": null,
        "firstname": null,
        "lastname": null
    },
    "billing_address": {
        .....
    }
}

Step 3) Add Item to Cart for the Guest.

For the third step, you have cart_id(w04QKi5YeYh6CC1tTFbpY1sPNKhbrMIZ) and quote_id(1187676) from the step 1 and 2.

Guest add item to cart
Guest add an item to the cart

You have to add items to the cart using REST API. you can able to add single or multiple items to the cart with API.

Action: POST

API URL: <SITE_URL>/rest/<store_code>/V1/guest-carts/:cartId/items
(Example: https://magento.test/rest/default/V1/guest-carts/w04QKi5YeYh6CC1tTFbpY1sPNKhbrMIZ/items)

Payload:

{
  "cartItem": {
    "quote_id": "1187676",
    "sku": "24-MB01",
    "qty": 1
  }
}

Response:

{
    "item_id": 1448307,
    "sku": "24-MB01",
    "qty": 1,
    "name": "Item Bag",
    "price": 7.99,
    "product_type": "simple",
    "quote_id": "1187676"
}

Step 4) Fetch estimate-shipping-methods for the cart.
In step 4, you need to fetch available shipping methods from the shipping address you have passed in the payload.
The response will be an available shipping method for the cart.

Step 4. Estimate shipping method
Estimate shipping method REST API

Action: POST

API URL: <SITE_URL>/rest/<store_code>/V1/guest-carts/:cartId/estimate-shipping-methods
(Example: https://magento.test/rest/default/V1/guest-carts/w04QKi5YeYh6CC1tTFbpY1sPNKhbrMIZ/estimate-shipping-methods)

Payload:

{
  "address": {
    "region": "Dublin",
    "country_id": "IE",
    "street": [
      "123 Oak Ave"
    ],
    "postcode": "W34 X5Y5",
    "city": "Dublin",
    "firstname": "Rakesh",
    "lastname": "Jesadiya",
    "customer_id": null,
    "email": "rakesh@jesadiya.com",
    "telephone": "4422531111",
    "same_as_billing": 1
  }
}

Response:

[
    {
        "carrier_code": "matrixrate",
        "method_code": "matrixrate_1",
        "carrier_title": "Select Shipping Method",
        "method_title": "Standard Shipping - Small",
        "amount": 5,
        "base_amount": 5,
        "available": true,
        "error_message": "",
        "price_excl_tax": 5,
        "price_incl_tax": 5
    }
]

Step 5) Fetch available Payment method for the cart.

Set Shipping info
Set Shipping information REST API

Fetch payment method by setting shipping carrier and shipping code in the payload of shipping-information API.

Action: POST

API URL: <SITE_URL>/rest/<store_code>/V1/guest-carts/:cartId/shipping-information
(Example: https://magento.test/rest/default/V1/guest-carts/w04QKi5YeYh6CC1tTFbpY1sPNKhbrMIZ/shipping-information)

Payload:

{
    "addressInformation": {
        "shipping_address": {
            "region": "Dublin",
            "country_id": "IE",
            "street": [
                "123 Oak Ave"
            ],
            "postcode": "W34 X5Y5",
            "city": "Dublin",
            "firstname": "Jane Guest",
            "lastname": "Doe Guest",
            "customer_id": null,
            "email": "rakesh@wearejh.com",
            "telephone": "4445531111"
        },
        "billing_address": {
            "region": "Dublin",
            "country_id": "IE",
            "street": [
                "123 Oak Ave"
            ],
            "postcode": "W34 X5Y5",
            "city": "Dublin",
            "firstname": "Jane Guest",
            "lastname": "Doe Guest",
            "customer_id": null,
            "email": "rakesh@wearejh.com",
            "telephone": "4445531111"
        },
        "shipping_carrier_code": "matrixrate",
        "shipping_method_code": "matrixrate_1"
    }
}

Response:

{
    "payment_methods": [
        {
            "code": "checkmo",
            "title": "Check / Money order"
        },
        {
            "code": "cashondelivery",
            "title": "Cash On Delivery"
        },
        {
            "code": "adyen_cc",
            "title": "Credit Card"
        }
    ],
    "totals": {
    ....
     }
}

Step 6) Place Order for the Guest customer.

In the step 5, you can able to see different payment methods available for the checkout.
Here In our case, two offline payment methods and one online payment method called adyen_cc

You have to pass the payment method code to the payment-information API.

Step 6. Guest Place an Order
Guest Place an Order REST API

Action: POST

API URL: <SITE_URL>/rest/<store_code>/V1/guest-carts/:cartId/payment-information
(Example: https://magento.test/rest/default/V1/guest-carts/w04QKi5YeYh6CC1tTFbpY1sPNKhbrMIZ/payment-information)

Payload:

{
    "email": "guest@jesadiya.com",
    "paymentMethod": {
        "method": "cashondelivery"
    },
    "billing_address": {
        "email": "rakesh@wearejh.com",
        "region": "County Kerry",
        "region_id": 0,
        "region_code": "",
        "country_id": "IE",
        "street": [
            "123 Dublin street"
        ],
        "postcode": "W34 X4Y5",
        "city": "Dublin",
        "telephone": "441XXXXX44",
        "firstname": "Janes Guest",
        "lastname": "Does Guest"
    }
}

Response: Order Entity id.

All the above steps are for the Guest Checkout and you will be able to place an order using REST API.