4

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

  • also have same problem i am try to set shipping address and using 2.0.7 version but there is no any solution if have then provide me api link . – Prince Yadav Jul 1 '16 at 13:29
4

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!

  • Thanks for the response. I'm using token authentication so it seems that /carts/mine/shipping-methods returns an error saying that the shipping address is not set ( even though my user has a default shipping address associated to their account ). I can easily set the billing address with /carts/mine/billing-address endpoint but there appears to not be a similar method for setting the shipping address – AdrianV Feb 17 '16 at 18:16
  • Okay I see - that is an issue. There's an internal issue covering this (MAGETWO-46720) - I put it into consideration for our next patch release on 2.0 . Thanks for your help! – Chuck Feb 19 '16 at 16:14
  • any solution for set address to quote? – Sunil Patel May 6 '16 at 7:48
  • MAGETWO-46720 is fixed ( mainline github code ) and has been patched back to 2.0.4 ; so you should be able to set shipping address to quote in 2.0.4+. – Chuck May 9 '16 at 13:37
  • Hello Chuck, can you tell me about swagger ? – Charvi Parikh Mar 7 '18 at 9:23
0

You can set shipping address using below api POST rest/V1/carts/mine/billing-address

in this API you can see parameter: "useForShipping": true,

After that you can get shipping method after GET rest/V1/carts/2/shipping-methods.

Please let me know if you still have issue.

  • What about guest quotes? the ""useForShipping": true is not working for guest-carts. Please advice. – Altaf Hussain Nov 24 '16 at 7:04
  • If we have an all Virtual products so if i use this api /V1/carts/mine/shipping-information it will show error like this { status: 404, data: Object message: "Carrier with such method not found: %1, %2" parameters: Array[2] 0: "usps" 1: "free" } – Saurabh Taletiya Mar 27 at 11:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.