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.

Is it possible to search customers by company?

Solved

Is it possible to search customers by company?

devkev
Shopify Partner
3 1 0

In the REST API it lets you search customers by company. But this does not seem possible in the GraphQL API. Is that correct?

 

REST API docs for customers search:

https://shopify.dev/docs/api/admin-rest/2024-07/resources/customer#get-customers-search?query=email:...

 

GraphQL docs for customers query:

https://shopify.dev/docs/api/admin-graphql/2024-07/queries/customers

 

 

Accepted Solution (1)
devkev
Shopify Partner
3 1 0

This is an accepted solution.

Thanks for the suggestion! What I ended up doing was using the REST API to search by company and then building a GraphQL customer query using the customer ids from REST. That way I can search by company and still use GraphQLs ability to load related data like addresses and orders.

 

REST QUERY:

https://your-development-store.myshopify.com/admin/api/2024-07/customers/search.json?fields=id&query=company%3A%22mycompany%22

 

 GraphQL Query:

 

query {
    customers(first: 50, query: "(id:111111111 OR id:22222222 OR id:333333333)") {
        nodes {
            id
            firstName
            lastName
            emailMarketingConsent {
                marketingState
            }
            addresses {
                company
            }
            defaultAddress {
                city
                provinceCode
                countryCode
            }
            lastOrder {
                processedAt
            }
            numberOfOrders
            amountSpent {
                amount
            }
            tags
        }
        pageInfo {
            hasPreviousPage
            hasNextPage
            startCursor
            endCursor
        }
    }
}

 

 

By the way, I tried copying your query into the GraphQL explorer and it gave me "Invalid search field for this query."

 

View solution in original post

Replies 4 (4)

Liam
Community Manager
3108 344 899

Hi Devkev

 

In the Shopify GraphQL Admin API, there isn’t a direct way to search customers by their company name. The REST API does provide this capability, but the GraphQL API does not currently have an equivalent query parameter for searching customers by company. However, you can achieve a similar result by querying customers and filtering their addresses to check for the company name. Here is an example of how you might do this:

 

query GetCustomersByCompany {
  customers(first: 50, query: "addresses.company:YourCompanyName") {
    edges {
      node {
        id
        firstName
        lastName
        email
        addresses {
          company
        }
      }
    }
  }
}

Hope this helps!

Liam | Developer Advocate @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

devkev
Shopify Partner
3 1 0

This is an accepted solution.

Thanks for the suggestion! What I ended up doing was using the REST API to search by company and then building a GraphQL customer query using the customer ids from REST. That way I can search by company and still use GraphQLs ability to load related data like addresses and orders.

 

REST QUERY:

https://your-development-store.myshopify.com/admin/api/2024-07/customers/search.json?fields=id&query=company%3A%22mycompany%22

 

 GraphQL Query:

 

query {
    customers(first: 50, query: "(id:111111111 OR id:22222222 OR id:333333333)") {
        nodes {
            id
            firstName
            lastName
            emailMarketingConsent {
                marketingState
            }
            addresses {
                company
            }
            defaultAddress {
                city
                provinceCode
                countryCode
            }
            lastOrder {
                processedAt
            }
            numberOfOrders
            amountSpent {
                amount
            }
            tags
        }
        pageInfo {
            hasPreviousPage
            hasNextPage
            startCursor
            endCursor
        }
    }
}

 

 

By the way, I tried copying your query into the GraphQL explorer and it gave me "Invalid search field for this query."

 

Liam
Community Manager
3108 344 899

Hi again - I looked into this further and the customer search filters are actually deprecated on GraphQL. Have you looked into using customer segments?

 

 

Liam | Developer Advocate @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

devkev
Shopify Partner
3 1 0

I hadn't thought of using customer segments. I found this list of attributes that can be queried. But company doesn't appear to be one of them. What I have now is working well enough I guess. I probably just stick with that. Thanks for the suggestion.