Edits history of script submission #23725 for ' Get Customer (shopify)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * Get Customer
     * Get a customer with contact details, marketing consent, order count, amount spent, addresses and last order.
     */
    export async function main(auth: RT.Shopify, customer_id: string) {
      const id = customer_id.startsWith("gid://")
        ? customer_id
        : `gid://shopify/Customer/${customer_id}`
      const response = await fetch(
        `https://${auth.store_name}.myshopify.com/admin/api/2026-10/graphql.json`,
        {
          method: "POST",
          headers: {
            "X-Shopify-Access-Token": auth.token,
            "Content-Type": "application/json",
            Accept: "application/json",
          },
          body: JSON.stringify({
            query: `query ($id: ID!) { customer(id: $id) { id legacyResourceId displayName firstName lastName state tags note locale verifiedEmail taxExempt numberOfOrders amountSpent { amount currencyCode } createdAt updatedAt defaultEmailAddress { emailAddress marketingState marketingOptInLevel } defaultPhoneNumber { phoneNumber } defaultAddress { name firstName lastName company address1 address2 city provinceCode zip countryCodeV2 phone } lastOrder { id name createdAt } addressesV2(first: 20) { nodes { id name firstName lastName company address1 address2 city provinceCode zip countryCodeV2 phone } } } }`,
            variables: { id },
          }),
        }
      )
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      const { data, errors } = await response.json()
      if (errors) {
        throw new Error(JSON.stringify(errors))
      }
    
      return data.customer
    }
    

    Submitted by hugo989 3 hours ago