Discussing APIs and development related to customers, discounts, and order management.
I've created a customer record in my development store using the customers API with the following:
await this.client.post({
path: "/admin/api/2023-04/customers.json",
data: {
customer: {
email: emailAddress,
tags: ["aTag"],
email_marketing_consent: {
state: "subscribed",
opt_in_level: "confirmed_opt_in",
},
},
},
});
I've verified the account appears in the customers dashboard for my development store, but when I attempt to search for that customer using the following, I get no results:
await this.client.get({
path: `/admin/api/2023-04/customers/search.json?query=${encodeURIComponent(
email:${emailAddress}`
)}`,
});
I have verified getting the account using just the customer ID via the REST API works, but for whatever reason the search query returns no results.
Any ideas what I'm missing or doing wrong?
Solved! Go to the solution
This is an accepted solution.
Hi Liam,
I appreciate the quick response! I did a little bit more digging, turns out I was constructing the request incorrectly. I'm pretty sure I followed the expected format for the query, but after changing it to this, it worked.
await this.client.get({
path: `/admin/api/2023-04/customers/search.json`,
query: {
query: `email:${emailAddress}`,
},
});
I guess the client removes any URL parameters applied directly to the path.
Hi Rtsharp,
The first thing I'd check is to make sure that the emailAddress
is correctly populated and it matches exactly the email address used to create the customer. I'd also make sure you are properly handling the asynchronous nature of the requests Ensure that you are either await
-ing the promise or handling it with a .then()
block.
Also if you're using encodeURIComponent
on your query string could this cause the email to be encoded and affect the query? You could try removing encodeURIComponent
to see if that makes a difference?
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
This is an accepted solution.
Hi Liam,
I appreciate the quick response! I did a little bit more digging, turns out I was constructing the request incorrectly. I'm pretty sure I followed the expected format for the query, but after changing it to this, it worked.
await this.client.get({
path: `/admin/api/2023-04/customers/search.json`,
query: {
query: `email:${emailAddress}`,
},
});
I guess the client removes any URL parameters applied directly to the path.