0
Retrieves a list of shipping rates
One script reply has been approved by the moderators Verified

Retrieves a list of available shipping rates for the specified checkout. Implementers need to poll this endpoint until rates become available. Each shipping rate contains the checkout's new subtotal price, total tax, and total price in the event that this shipping rate is selected. This can be used to update the UI without performing further API requests. To apply a shipping rate, update the checkout's shipping line with the handle of the selected rate.

Created by hugo697 606 days ago Viewed 22459 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 606 days ago
1
type Shopify = {
2
  token: string;
3
  store_name: string;
4
};
5
/**
6
 * Retrieves a list of shipping rates
7
 * Retrieves a list of available shipping rates for the specified checkout. Implementers need to poll this endpoint until rates become available. Each shipping rate contains the checkout's new subtotal price, total tax, and total price in the event that this shipping rate is selected. This can be used to update the UI without performing further API requests. To apply a shipping rate, update the checkout's shipping line with the handle of the selected rate.
8
 */
9
export async function main(
10
  auth: Shopify,
11
  api_version: string = "2023-10",
12
  token: string
13
) {
14
  const url = new URL(
15
    `https://${auth.store_name}.myshopify.com/admin/api/${api_version}/checkouts/${token}/shipping_rates.json`
16
  );
17

18
  const response = await fetch(url, {
19
    method: "GET",
20
    headers: {
21
      "X-Shopify-Access-Token": auth.token,
22
    },
23
    body: undefined,
24
  });
25
  if (!response.ok) {
26
    const text = await response.text();
27
    throw new Error(`${response.status} ${text}`);
28
  }
29
  return await response.json();
30
}
31