0

Get Order

Published Nov 8, 2023

Get an order with its customer, addresses, totals, line items, fulfillments, transactions and refunds.

Script shopify Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 31 days ago
1
//native
2

3
/**
4
 * Get Order
5
 * Get an order with its customer, addresses, totals, the first 50 line items, fulfillments, transactions and refunds. Orders older than 60 days need the read_all_orders scope.
6
 */
7
export async function main(auth: RT.Shopify, order_id: string) {
8
  const id = order_id.startsWith("gid://")
9
    ? order_id
10
    : `gid://shopify/Order/${order_id}`
11

12
  const response = await fetch(
13
    `https://${auth.store_name}.myshopify.com/admin/api/2026-10/graphql.json`,
14
    {
15
      method: "POST",
16
      headers: {
17
        "X-Shopify-Access-Token": auth.token,
18
        "Content-Type": "application/json",
19
        Accept: "application/json",
20
      },
21
      body: JSON.stringify({
22
        query: `query ($id: ID!) { order(id: $id) { id legacyResourceId name email phone poNumber createdAt updatedAt processedAt cancelledAt cancelReason closed closedAt test tags note customAttributes { key value } displayFinancialStatus displayFulfillmentStatus currencyCode subtotalPriceSet { shopMoney { amount currencyCode } } totalDiscountsSet { shopMoney { amount currencyCode } } totalShippingPriceSet { shopMoney { amount currencyCode } } totalTaxSet { shopMoney { amount currencyCode } } totalPriceSet { shopMoney { amount currencyCode } } totalRefundedSet { shopMoney { amount currencyCode } } totalOutstandingSet { shopMoney { amount currencyCode } } customer { id displayName defaultEmailAddress { emailAddress } defaultPhoneNumber { phoneNumber } } shippingAddress { name firstName lastName company address1 address2 city provinceCode zip countryCodeV2 phone } billingAddress { name firstName lastName company address1 address2 city provinceCode zip countryCodeV2 phone } shippingLine { title code originalPriceSet { shopMoney { amount currencyCode } } } lineItems(first: 50) { nodes { id name sku quantity currentQuantity unfulfilledQuantity refundableQuantity originalUnitPriceSet { shopMoney { amount currencyCode } } discountedUnitPriceAfterAllDiscountsSet { shopMoney { amount currencyCode } } variant { id title } product { id title } } pageInfo { hasNextPage endCursor } } fulfillments(first: 20) { id name status displayStatus createdAt trackingInfo { company number url } } transactions(first: 20) { id kind status gateway test processedAt amountSet { shopMoney { amount currencyCode } } } refunds(first: 20) { id createdAt note totalRefundedSet { shopMoney { amount currencyCode } } } } }`,
23
        variables: { id },
24
      }),
25
    }
26
  )
27

28
  if (!response.ok) {
29
    throw new Error(`${response.status} ${await response.text()}`)
30
  }
31

32
  const { data, errors } = await response.json()
33
  if (errors) {
34
    throw new Error(JSON.stringify(errors))
35
  }
36

37
  return data.order
38
}
39

Other submissions
  • Submitted by hugo697 Typescript (fetch-only)
    Created 558 days ago
    1
    type Shopify = {
    2
      token: string;
    3
      store_name: string;
    4
    };
    5
    /**
    6
     * Retrieves a specific order
    7
     * Retrieves a specific order
    8
     */
    9
    export async function main(
    10
      auth: Shopify,
    11
      api_version: string = "2023-10",
    12
      order_id: string
    13
    ) {
    14
      const url = new URL(
    15
        `https://${auth.store_name}.myshopify.com/admin/api/${api_version}/orders/${order_id}.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