Magento – Set Shipping Address via Magento 2 REST API

magento2restshipping-address

I have a JS application where I create a cart object with items and an associated gift-message. I would like to set the shipping (and billing) address for the order and am using the following endpoint:

POST /V1/carts/mine/shipping-information

However it asks me for the following two fields in addition to the address.

  1. "shippingMethodCode": "string"
  2. "shippingCarrierCode": "string"

Where do I get these codes from?

The request is:

{
   "addressInformation": {
      "shippingAddress": {
         "city": "New York City",
         "country_id": "US",
         "firstname": "someone",
         "lastname": "something",
         "postcode": "11756",
         "region_code": "NY",
         "street": [
            "address01",
            "address02"
         ],
         "telephone": "1111111111"
      },

     "shippingCarrierCode": "usps"
     "shippingMethodCode": "free"
   }
}

The explicit error is:

{
  status: 404,
  data: Object
    message: "Carrier with such method not found: %1, %2"
    parameters: Array[2]
      0: "usps"
      1: "free"
}

I have looked at

Stores > Configure > Sales > Shipping

but do not see these codes. When I try to request the codes using

GET /V1/carts/mine/shipping-methods

It tells me I need to set an address first.

As a follow up, is there any resource out there that provides some best-pracitices when it comes to using the Magento 2 REST API to perform a cart checkout order?

Thanks for all the help and pointers

Best Answer

I believe you need to call the appropriate api to see the selection of shipping methods available. You can then use those carrier codes / method codes. For cart there are 3 apis based on login - your cart (customer), guest cart (anonymous) and cart management (used by admin) . So for example if you're anonymous you can work with shipping via the quoteGuestShippingMethodManagementV1 interface. /V1/guest-carts/{cartId}/shipping-methods API should list the available shipping methods for the guest cart.

I don't believe there's a resource that outlines best practices for using the cart APIs. In general you'd built a cart (aka add items to a cart), add a shipping address to a cart, see available shipping methods then set a shipping method, see available payment methods then set a payment method, then submit the cart. Not some permutations based on context of the user (anonymous, registered user, admin user). Anonymous and registered users can only see their carts, admin can see all carts.

Did you know you can see the APIs available on your system by going to baseurl/swagger ? We're updating it to lock down to only expose anonymous functions by default but if you use a access token you can actively test the APIs ( coming in 2.1 release ).

Keep the feedback coming!

Related Topic