Have your say in Community Polls: What was/is your greatest motivation to start your own business?
Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

Calculate shipping rate with draft order api

Solved

Calculate shipping rate with draft order api

zivmiz
Shopify Partner
3 0 2

Hi,

I'm developing a custom app and I'm trying to create a draft order with the calculation of shipping via API.

The problem is that I don't have the shipping rates options.

How can I get the shipping rates of a specific draft order?

like there is in checkout api?

Accepted Solution (1)
Brett13
Tourist
4 1 5

This is an accepted solution.

I did find a way to get this working.  Take a look at the Checkouts API.  There is an endpoint to shipping_rates.json that returns the shipping rates.  All you need is the checkout token.  I was able to get this from the draft order's invoice URL. For example, draft order invoice URL: https://apple.myshopify.com/690933842/invoices/d64d93d528cfc75c0876183d58f4a has checkout token: d64d93d528cfc75c0876183d58f4a.

https://shopify.dev/docs/admin-api/rest/reference/sales-channels/checkout?api[version]=2020-04

Screen Shot 2020-08-04 at 11.38.27 AM.png

View solution in original post

Replies 17 (17)

_JB
Shopify Staff (Retired)
836 100 223

Hey @zivmiz,

 

Just wanted to let you know we're looking in to this. The documentation definitely makes it seem like you can calculate shipping rates using this endpoint, but to be honest I don't remember ever seeing that and I'm not seeing a way to do it now. I'll post back here when I have an update for you.

JB | Solutions Engineer @ Shopify 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

Brett13
Tourist
4 1 5

I'm also facing this issue.  I need to get the shipping rates for a draft order to be able to set the shipping_line of the draft order.  Any updates on this?

nepthar
Tourist
6 0 4

Hey guys,

Just want to throw my two cents in here! I've also been trying to get this to work based on what I read in the docs, but I can't figure it out. Seems like I'm not alone! Any word on a possible fix?

Brett13
Tourist
4 1 5

This is an accepted solution.

I did find a way to get this working.  Take a look at the Checkouts API.  There is an endpoint to shipping_rates.json that returns the shipping rates.  All you need is the checkout token.  I was able to get this from the draft order's invoice URL. For example, draft order invoice URL: https://apple.myshopify.com/690933842/invoices/d64d93d528cfc75c0876183d58f4a has checkout token: d64d93d528cfc75c0876183d58f4a.

https://shopify.dev/docs/admin-api/rest/reference/sales-channels/checkout?api[version]=2020-04

Screen Shot 2020-08-04 at 11.38.27 AM.png

nepthar
Tourist
6 0 4

Thanks for that follow up! I'm going to give it a try now. Out of curiosity, are you a "Sales Channel"?

Brett13
Tourist
4 1 5

Sure no problem.  No, I'm not a sales channel.  Just a software developer from Unific dealing with this issue myself.

NiceTeam
Excursionist
15 0 13

The problem with the solution here is that this endpoint is only available to Sales Channels. It can't be used by normal apps.
As far as I know, there is still no way to get the shipping rate for draft order without specifying it by yourself.

tolgapaksoy
Shopify Partner
112 7 75

Use the Storefront API to fetch the shipping rates on the frontend/client.

We use this exact trick with DraftOrder API, and use the checkout token in the invoice URL as a checkout ID. This checkout ID is used client-side to query for shipping rates.

NiceTeam
Excursionist
15 0 13

@tolgapaksoyThank you for this information. It seems like this should work 🙂 Will try it out.

gorodezkiy
Shopify Partner
6 0 20

For our regular private app I could get available shipping rates by using GraphQL Admin API - draftOrderCalculate mutation - https://shopify.dev/docs/admin-api/graphql/reference/orders/draftordercalculate#interactive-example-... :

Query POST https://jennyyoo-staging.myshopify.com/admin/api/2020-01/graphql.json :

mutation draftOrderCalculate($input: DraftOrderInput!) {
  draftOrderCalculate(input: $input) {
    calculatedDraftOrder {
      subtotalPrice
      totalPrice
      totalShippingPrice
      totalTax
      availableShippingRates {
          handle
          title
          price {
              amount
          }
      }
    }
    userErrors {
      field
      message
    }
  }
}

 

Input:

{
    "input": {
        "lineItems": [{
            "variantId": "gid://shopify/ProductVariant/123",
            "quantity": 1,
            "appliedDiscount": null
        }],
        "shippingAddress": {
            "firstName": "Eduard",
            "lastName": "...",
            "address1": "...",
            "address2": "...",
            "phone": "123",
            "city": "Miami",
            "countryCode": "US",
            "provinceCode": "FL",
            "zip": "123"
        },
        "billingAddress": {
            "firstName": "Eduard",
            "lastName": "...",
            "address1": "...",
            "address2": "...",
            "phone": "123",
            "city": "Miami",
            "countryCode": "US",
            "provinceCode": "FL",
            "zip": "123"
        },
        "email": "eduard@example.com"
    }
}

 

Response:

{
    "data": {
        "draftOrderCalculate": {
            "calculatedDraftOrder": {
                "subtotalPrice": "285.00",
                "totalPrice": "319.20",
                "totalShippingPrice": "0.00",
                "totalTax": "34.20",
                "availableShippingRates": [
                    {
                        "handle": "shopify-USPS%20First%20Class%20Mail-2.75",
                        "title": "USPS First Class Mail",
                        "price": {
                            "amount": "2.75"
                        }
                    },
                    {
                        "handle": "shopify-Fedex%20Home%20Delivery-9.95",
                        "title": "Fedex Home Delivery",
                        "price": {
                            "amount": "9.95"
                        }
                    },
                    {
                        "handle": "fedex-FEDEX_EXPRESS_SAVER-22.67",
                        "title": "FedEx FedEx Express Saver®",
                        "price": {
                            "amount": "22.67"
                        }
                    },
                    {
                        "handle": "fedex-FEDEX_2_DAY-30.84",
                        "title": "FedEx FedEx 2Day®",
                        "price": {
                            "amount": "30.84"
                        }
                    },
                    {
                        "handle": "fedex-STANDARD_OVERNIGHT-46.24",
                        "title": "FedEx FedEx Standard Overnight®",
                        "price": {
                            "amount": "46.24"
                        }
                    }
                ]
            },
            "userErrors": []
        }
    },
    "extensions": {
        "cost": {
            "requestedQueryCost": 11,
            "actualQueryCost": 11,
            "throttleStatus": {
                "maximumAvailable": 2000.0,
                "currentlyAvailable": 1989,
                "restoreRate": 100.0
            }
        }
    }
}

 

ApiPaul
Tourist
4 0 1

Thanks @gorodezkiy you rocks!

 
 
 
Sibari
Shopify Partner
9 0 0

Excelent! thanks!!

Cryptoz
Shopify Partner
1 0 0

This worked in my RUST app. Thanks so much Legend!

Lilante
Visitor
2 0 0

There is also another option. Use this code snippet:

      "shipping_line": {
            "custom":"true",
            "title":"Fedex",
            "price":"12.99"
        }
 
nepthar
Tourist
6 0 4

Can you explain what you mean by that? Are you suggesting we just charge everyone 12.99 for "Fedex"? 😄

Skylitech
Shopify Partner
9 0 5

Hi, 

Do you have any solution to this?

We need this.

There is no way to calculate to fetch available shipping options with draft order API.

 

PavloDvoryak
Visitor
1 0 0

Have you found a solution?