Magento 2 API: Shipping and Billing Information

After all the products have been added to the cart, you can start preparing the order for the checkout. In other words, you are going to set shipping and billing info. To finish this, you will have to go through three stages. In this post, I’ will guide you through an example of adding shipping information.

Let’s get started!

Magento API Integration Service by Mageplaza

Connect your store with any 3rd-party software and boost customer experience quickly and efficiently.

Learn more
Magento API Integration service

Adding Shipping and Billing Information via Magento 2 API in three steps:

Step 1: Estimate shipping costs

The shipping costs for each shipping method, which can be applied to the order are calculated by Magento. In today tutorial, you will use two shipping methods which are the flatrate ($5 per item) and tablerate.

Endpoint

POST <host>/rest/<store_code>/V1/carts/mine/estimate-shipping-methods

Headers

Content-Type application/json

Authorization Bearer <customer token>

Payload

The shipping address is contained in the payload.

Code sample

{  "address": {
      "region": "New York",
      "region_id": 43,
      "region_code": "NY",
      "country_id": "US",
      "street": [
        "123 Oak Ave"
        ],
      "postcode": "10577",
      "city": "Purchase",
      "firstname": "Jane",
      "lastname": "Doe",
      "customer_id": 4,
      "email": "[email protected]",
      "telephone": "(512) 555-1111",
      "same_as_billing": 1
  }
}

Response

Please remember that the flatrate shipping method’s cost is $15. The Sprite Yoga Companion Kit bundled product is counted as one item. Because the customer downloads the Advanced Pilates & Yoga item, it is not charged for shipping.

Code sample


[
    {
        "carrier_code": "flatrate",
        "method_code": "flatrate",
        "carrier_title": "Flat Rate",
        "method_title": "Fixed",
        "amount": 15,
        "base_amount": 15,
        "available": true,
        "error_message": "",
        "price_excl_tax": 15,
        "price_incl_tax": 15
    },
    {
        "carrier_code": "tablerate",
        "method_code": "bestway",
        "carrier_title": "Best Way",
        "method_title": "Table Rate",
        "amount": 5,
        "base_amount": 5,
        "available": true,
        "error_message": "",
        "price_excl_tax": 5,
        "price_incl_tax": 5
    }
]

Step 2: Set shipping and billing information

In this call, the shipping and billing addresses are specified. The selected carrier_code and method_code are also specified as well. Because the Table Rate shipping method only costs $5, this option was selected by the customer.

A list of payment options are returned and the order totals are calculated.

Endpoint

POST <host>/rest/<store_code>/V1/carts/mine/shipping-information

Headers

Content-Type application/json

Authorization Bearer <customer token>

Payload

Code sample

{  "addressInformation": {
      "shipping_address": {
       "region": "New York",
       "region_id": 43,
       "region_code": "NY",
       "country_id": "US",
       "street": [
      "123 Oak Ave"
    ],
    "postcode": "10577",
    "city": "Purchase",
    "firstname": "Jane",
    "lastname": "Doe",
    "email": "[email protected]",
    "telephone": "512-555-1111"
  },
  "billing_address": {
      "region": "New York",
    "region_id": 43,
    "region_code": "NY",
    "country_id": "US",
    "street": [
      "123 Oak Ave"
    ],
    "postcode": "10577",
    "city": "Purchase",
    "firstname": "Jane",
    "lastname": "Doe",
    "email": "[email protected]",
    "telephone": "512-555-1111"
  },
  "shipping_carrier_code": "tablerate",
  "shipping_method_code": "bestway"
  }
}

Response

The order’s subtotal is $160, $5 is the shipping costs. And therefore, the grand total will be $165.

banktransfer and checkmo are the payment methods which are available. A payment method will be specified in the following step.

Code sample


{
    "payment_methods": [
        {
            "code": "cashondelivery",
            "title": "Cash On Delivery"
        },
        {
            "code": "banktransfer",
            "title": "Bank Transfer Payment"
        },
        {
            "code": "purchaseorder",
            "title": "Purchase Order"
        },
        {
            "code": "checkmo",
            "title": "Check / Money order"
        }
    ],
    "totals": {
        "grand_total": 165,
        "base_grand_total": 165,
        "subtotal": 160,
        "base_subtotal": 160,
        "discount_amount": 0,
        "base_discount_amount": 0,
        "subtotal_with_discount": 160,
        "base_subtotal_with_discount": 160,
        "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": 160,
        "shipping_incl_tax": 5,
        "base_shipping_incl_tax": 5,
        "base_currency_code": "USD",
        "quote_currency_code": "USD",
        "items_qty": 4,
        "items": [
            {
                "item_id": 6,
                "price": 22,
                "base_price": 22,
                "qty": 1,
                "row_total": 22,
                "base_row_total": 22,
                "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": 22,
                "base_price_incl_tax": 22,
                "row_total_incl_tax": 22,
                "base_row_total_incl_tax": 22,
                "options": "[]",
                "weee_tax_applied_amount": null,
                "weee_tax_applied": null,
                "name": "Radiant Tee-M-Orange"
            },
            {
                "item_id": 7,
                "price": 18,
                "base_price": 18,
                "qty": 1,
                "row_total": 18,
                "base_row_total": 18,
                "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": 18,
                "base_price_incl_tax": 18,
                "row_total_incl_tax": 18,
                "base_row_total_incl_tax": 18,
                "options": "[{\"value\":\"Advanced Pilates & Yoga (Strength)\",\"label\":\"Downloads\"}]",
                "weee_tax_applied_amount": null,
                "weee_tax_applied": null,
                "name": "Advanced Pilates & Yoga (Strength)"
            },
            {
                "item_id": 8,
                "price": 68,
                "base_price": 68,
                "qty": 1,
                "row_total": 68,
                "base_row_total": 68,
                "row_total_with_discount": 0,
                "tax_amount": 0,
                "base_tax_amount": 0,
                "discount_amount": 0,
                "base_discount_amount": 0,
                "discount_percent": 0,
                "price_incl_tax": 68,
                "base_price_incl_tax": 68,
                "row_total_incl_tax": 68,
                "base_row_total_incl_tax": 68,
                "options": "[{\"value\":\"1 x Sprite Stasis Ball 65 cm <span class=\\\"price\\\">$27.00<\\/span>\",\"label\":\"Sprite Stasis Ball\"},{\"value\":\"1 x Sprite Foam Yoga Brick <span class=\\\"price\\\">$5.00<\\/span>\",\"label\":\"Sprite Foam Yoga Brick\"},{\"value\":\"1 x Sprite Yoga Strap 8 foot <span class=\\\"price\\\">$17.00<\\/span>\",\"label\":\"Sprite Yoga Strap\"},{\"value\":\"1 x Sprite Foam Roller <span class=\\\"price\\\">$19.00<\\/span>\",\"label\":\"Sprite Foam Roller\"}]",
                "weee_tax_applied_amount": null,
                "weee_tax_applied": null,
                "name": "Sprite Yoga Companion Kit"
            },
            {
                "item_id": 13,
                "price": 52,
                "base_price": 52,
                "qty": 1,
                "row_total": 52,
                "base_row_total": 52,
                "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": 52,
                "base_price_incl_tax": 52,
                "row_total_incl_tax": 52,
                "base_row_total_incl_tax": 52,
                "options": "[{\"value\":\"Gray\",\"label\":\"Color\"},{\"value\":\"S\",\"label\":\"Size\"}]",
                "weee_tax_applied_amount": null,
                "weee_tax_applied": null,
                "name": "Chaz Kangeroo Hoodie"
            }
        ],
        "total_segments": [
            {
                "code": "subtotal",
                "title": "Subtotal",
                "value": 160
            },
            {
                "code": "shipping",
                "title": "Shipping & Handling (Best Way - Table Rate)",
                "value": 5
            },
            {
                "code": "tax",
                "title": "Tax",
                "value": 0,
                "extension_attributes": {
                    "tax_grandtotal_details": []
                }
            },
            {
                "code": "grand_total",
                "title": "Grand Total",
                "value": 165,
                "area": "footer"
            }
        ]
    }
}

Step 3: Verify this step

To verify the step, you sign in as a customer, then go to the checkout page,

You should see Bank Transfer as the payment method, the billing and shipping addresses are displayed, and the costs for shipping have been calculated.

Congrats! You’ve finished all the necessary steps. Now, check the results to make sure everything works smoothly on your website, which doesn’t cause any trouble for your customers in shipping information later.


SVG

Shipping Rules for Magento 2

Configure shipping methods, rates, timeframe, & visibility based on weights, values, destination, SKU, and others

Check it out!


Conclusion

Above, I have just guided you on how to call API to set Shipping and Billing Information. Hope it is helpful for you. Should you have further questions or new ideas, feel free to leave a comment below. Thanks for reading!

Image Description
With over a decade of experience crafting innovative tech solutions for ecommerce businesses built on Magento, Jacker is the mastermind behind our secure and well-functioned extensions. With his expertise in building user-friendly interfaces and robust back-end systems, Mageplaza was able to deliver exceptional Magento solutions and services for over 122K+ customers around the world.
x