Is there a way to get orders by phone number? (Admin API)

AlexDenysiuk
Tourist
6 0 1

I can get orders by email (.../admin/api/2021-01/orders.json?email=example@gmail.com), but it doesn't work with a phone (?phone=+123456789)

Replies 2 (2)

AlexDenysiuk
Tourist
6 0 1

up up up up up

RyanAM
Shopify Partner
9 0 6

I'm not sure if there's a way to directly do this, but you can use a 2 step process:

 

1. Get the customer(s) by phone number (note that you can use a different API version than 2021-04 if you choose):

GET https://<your-store>.myshopify.com/admin/api/2021-04/customers.json?phone={phone_number}

 

2. Create a list of those customer IDs or email addresses, then loop through them making requests to the orders endpoint:

[PSUEDOCODE]

foreach(email_addresses as email_address){
    GET https://<your-store>.myshopify.com/admin/api/2021-04/orders.json?email={email_address}
}

[ or ]

foreach(customer_ids as customer_id){
    GET https://<your-store>.myshopify.com/admin/api/2021-04/orders.json?customer_id={customer_id}
}

 

 

In a perfect world you should only have 1 customer per phone number, but I wouldn't rely on it. The disadvantage to using this method (especially since you can't use a comma-delimited list of email addresses or phone numbers) is that you have to aggregate results and lose useful functionality such as pagination, so you'll have to roll your own. This also uses up more API requests than if it were natively supported (I didn't see anything in the documentation suggesting that what you're asking is supported, but at the same time I also didn't see the email parameter being officially supported either and that works just fine).

 

Of course, there's always GraphQL, which may support this natively and in fewer requests (I'm not sure), but if you're like me there are a number of reasons you prefer the Admin API over GraphQL. It seems to be a common theme that the Admin API strategically pushes you towards having to use GraphQL due to lack of crucial features (i.e. using pagination in combination with certain filters to get a list of Products isn't supported), but if you get creative enough there are usually imperfect ways to avoid it at the expense of more API calls and putting a bigger workload on your system (i.e. having to write your own filtering and pagination mechanics).

 

Having worked with the Shopify API for over 5 years, the developers have done an amazing job not only on Shopify but also on their API (which I must say has come a LONG way since I first started working with it), and I've found that if I can't do something, there's usually a reason for it that I'll figure out down the road so it's always worth thinking about why something isn't possible or is difficult. They do a good job thinking through all the scenarios and giving developers a pretty hard nudge away from making mistakes by refusing to support things that will turn around to bite third-party developers in the long run.