Sorting products by BEST_SELLING does not work

Im trying to get shofy products from a store using the sortKey BEST_SELLING however im getting an error:

GraphQL Response: {
  errors: [
    {
      message: 'Variable $sortKey of type ProductSortKeys was provided invalid value',
      locations: [Array],
      extensions: [Object]
    }
  ]
}

This also does not work with PRICE either, error:

GraphQL Response: {
  errors: [
    {
      message: 'Variable $sortKey of type ProductSortKeys was provided invalid value',
      locations: [Array],
      extensions: [Object]
    }
  ]
}

my implemention:

static async getShopifyProducts(
    did: string,
    {
      first = 6,
      after,
      before,
      searchTerm,
      sortKey = 'BEST_SELLING',
      reverse = true,
    }: {
      first?: number;
      after?: string | null;
      before?: string | null;
      searchTerm?: string;
      sortKey?: ShopifySortKey;
      reverse?: boolean;
    },
  ): Promise<{ products: ShopifyProduct[]; pageInfo: PageInfo } | null> {
    const credentials = await this.getShopifyCredentials(did);
    if (!credentials) return null;

    const { shopDomain, accessToken } = credentials;
    const decryptedToken = decryptAccesstoken(accessToken);

    try {
      // Build GraphQL query
      const query = `
        query GetProducts(
          $first: Int
          $after: String
          $before: String
          $query: String
          $sortKey: ProductSortKeys
          $reverse: Boolean
        ) {
          products(
            first: $first
            after: $after
            before: $before
            query: $query
            sortKey: $sortKey
            reverse: $reverse
          ) {
            edges {
             ...
            }
            pageInfo {
              ...
            }
          }
        }
      `;

      const variables = {
        first,
        after,
        before,
        query: searchTerm,
        sortKey,
        reverse,
      };

      const response = await fetch(
        `https://${shopDomain}/admin/api/2025-04/graphql.json`,
        {
          method: 'POST',
          headers: {
            'Content-Type': 'application/json',
            'X-Shopify-Access-Token': decryptedToken,
          },
          body: JSON.stringify({ query, variables }),
        },
      );

Hey @kevin_lam,

Thanks for sharing the full details. Looking at your query, it looks like you’re querying the admin API. The BEST_SELLING value isn’t available through the admin API sortkeys: ProductSortKeys - GraphQL Admin

It is available on the storefront API though if that will work: ProductSortKeys - Storefront API

Let me know if it does.

Hey @kevin_lam, just checking in to see if the above helped?