Applies to: Customers API | Customer Groups API | Customer Segments API | Customer Custom Attributes API
Learn how to search for customer profiles using one or more query filters.
Developers can use the SearchCustomers endpoint in the Customers API to search customer profiles in the Customer Directory using supported query filters. The search results include customer profiles that meet all the query conditions.
The SearchCustomers
endpoint searches the Customer Directory associated with a Square seller account. When calling this endpoint, you can use one or more of the following search query filters in any combination:
phone_number
- Filters by the phone number of the customer profile, which was collected by the seller or imported into the Customer Directory.email_address
- Filters by the email address of the customer profile, which was collected by the seller or imported into the Customer Directory.creation_source
- Filters by the source that created the customer profile.created_at
- Filters by the timestamp of when the customer profile was created.updated_at
- Filters by the timestamp of when the customer profile was last updated.reference_id
- Filters by an ID used to cross-reference the customer to an entity in an external system.group_ids
- Filters by the customer groups the customer profile belongs to.segment_ids
- Filters by the customer segments the customer profile belongs to.custom_attribute
- Filters by the value or updated date of custom attributes that are associated with a customer profile.
Only customer profiles that contain public information (given_name
, family_name
, company_name
, email_address
, or phone_number
) are included in the results. Customer profiles that have no public information are omitted.
Multiple query filters specified in a search request are combined as AND
statements, so the search results include the customer profiles that meet all the query conditions. These matching customer profiles are represented as a list of Customer
objects.
Consider the following set of customer profiles, all of which were updated in the last week:
- Customer A - Created 1 year ago through an eCommerce site that calls the Customers API.
- Customer B - Created 2 months ago through an eCommerce site that calls the Customers API.
- Customer C - Created 2 weeks ago using Square Point of Sale.
The following table shows example search queries for the set of customer profiles. Note that only profiles that satisfy all of the filters in the search query are returned in the results.
Search query | Customers returned |
---|---|
Include profiles created through the Customers API. Include profiles created in the last 6 months. Include profiles updated in the last week. | B |
Include profiles created through the Customers API. Include profiles updated in the last week. | A and B |
Include profiles updated in the last week. | A, B, and C |
Combining multiple filters as OR
statements in a search query isn't supported. To search for customers that meet at least one of a set of query conditions, send multiple single-filter searches and then join the result sets.
If any search condition isn't met, the result is an empty object ({}
).
When searching by email_address
, phone_number
, reference_id
, and some custom attribute
types, the API lets you search with an exact or fuzzy match of the specified customer attributes against a query expression. With an exact search, a query expression is compared literally against the targeted customer attribute value. With a fuzzy match, the query expression is tokenized and each resulting token is compared against targeted attribute values. Customer profiles are returned if their targeted attribute values match all tokens in any order.
You can choose to sort search results using the created_at
field or using the default sort order. By default, customer profiles are sorted alphanumerically by concatenating given_name
and family_name
. If these names aren't set, a string comparison is performed using one of the following fields in the following order: company_name
, email_address
, and phone_number
.
The default SearchCustomers
page size is 100 records, but you can use the limit
query filter to specify a smaller page size. If your search results include more than the default or specified limit, the endpoint paginates the results and returns a cursor
that lets you retrieve the next page of results. Note that the SearchCustomers
endpoint treats the search limit as advisory and might return more or fewer results.
To get the total number of customer profiles that match the search query, set the count
field to true
in the request. This directs Square to return a count
field in the response that contains the count of matching customer profiles. Only customer profiles that contain public information are counted.
Under normal operating conditions, newly created or updated customer profiles become available in SearchCustomers and ListCustomers endpoints in less than 30 seconds after they are created or updated. Some updates can take up to 1 minute to propagate. Occasionally, extraordinary network conditions (such as, incidents or outages) can slow the propagation even further. Under such circumstances, newly created or updated profiles might not be included in your search result. When this happens, wait a short time and then retry your search request.
Customer profiles can be created from different sources. For example, you can create a customer profile using the Square Dashboard, Square Point of Sale, and the Customers API. For the complete list of creation source values, see CustomerCreationSource.
Note
For information about using the Customers API to create and manage customer profiles, see Manage Customer Profiles.
To search customer profiles by creation source, include the creation_source
filter in the request and specify one or more values. Multiple values are treated as OR
statements.
For example, to search for customer profiles created by the Square Directory service (either through the Square Dashboard or Square Point of Sale), specify DIRECTORY
:
Search customers
To search for customer profiles created by third-party applications, specify THIRD_PARTY
:
Search customers
To search for customer profiles created by the Square Directory service or a third-party application, include both values:
Search customers
You can call the SearchCustomers endpoint with the created_at
or updated_at
filter to search for customer profiles based on the time they were created or last updated, respectively. Such filters are useful to synchronize customer data by polling. For example, you can send updated_at
search queries at a given time interval and determine subsequent actions to take on the customer profiles returned in the results.
The created_at
and updated_at
filters are identical in structure. They both require that you specify a start_at
and end_at
time to specify a time interval in which the targeted customer profiles were created or updated. For example, the following created_at
filter defines the time interval when targeted customer profiles were created:
Search customers
The timestamp format must be RFC-3339 compliant.
To search for a customer profile created or updated at a specific point of time, set the start_at
value slightly smaller than the end_at
value. The following updated_at
filter shows an example of how to specify a specific time when a customer profile was updated:
Search customers
You can search for customer profiles by email address with an exact or fuzzy filter. A search query can include either an exact filter or a fuzzy filter, but not both.
To search for customer profiles by email address with an exact match, use the email_address
filter as shown in the following search request example:
Search customers
This returns all customer profiles whose email_address
field holds the value of "[email protected]"
.
If you make an error in your query expression (for example, enter "[email protected]"
as "[email protected]"
), none of the previous results are returned. As shown in the following example, this behavior differs from the fuzzy search.
To search for customer profiles by email address with a fuzzy match, use the email_address
filter as shown in the following search request example:
Search customers
The query expression of john.doe
is first tokenized into two tokens of john
and doe
. The customer profile containing the email address of [email protected]
matches both tokens and is therefore returned in the search result.
A fuzzy search also serves as a partial search. For example, if you typed [email protected]
by mistake in the fuzzy query filter, you still get customer profiles containing the email address [email protected]
.
A fuzzy search provides you with great flexibility. For example, you can obtain the customer profiles containing [email protected]
even with the following fuzzy filter:
Search customers
Tokenization of the query expression of doe.john #com
turns it into a three-token (john
, doe
, and com
) query. The fuzzy search then selects customers profiles whose email addresses are john*doe@*.com
or doe*john@*.com
, where *
stands for any valid email-address characters that can include, for example, .
, _
, or even a middle of name like joe
.
Fuzzy search by email addresses can be a powerful option when used in a simple partial match or more advanced type-ahead user interfaces or embedded search experiences.
You can search for customer profiles by phone number with an exact or fuzzy filter. A search query can include either an exact filter or a fuzzy filter, but not both.
Phone numbers are represented by the phone_number
field of a Customer
object. Searches by phone number are applicable only to customer profiles that are stored in or imported into the seller's Customer Directory.
To conduct an exact search by phone number, specify the complete phone number in E.164-compliant format. This format starts with a +
symbol and is followed by a country code and the subscriber number (which includes the area code). Spaces, dashes, parentheses, and periods are allowed in the query expression.
For an exact match operation, Square removes spaces and non-numeric characters from the query expression and the stored phone numbers.
The following are E.164-compliant examples:
- +12345678901 (standard form)
- +1-234-567-8901
- +1 234.567.8901
- +1 (234) 567-8901
The following example request shows an exact search by phone number using an E.164-compliant format:
Search customers
Did you know?
For exact matching, specifying an E.164-compliant query expression (for example +12345678901
) also returns phone numbers that use an international prefix instead of the +
symbol (for example 0012345678901
).
The following table shows more examples to demonstrate the behavior of the exact match phone_number
filter:
Search query | Matched phone numbers | Unmatched phone numbers |
---|---|---|
+14152345678 or +1 (415) 234-5678 | For US sellers: 4152345678 415.234.5678 415-234-5678 (415) 234-5678 +1(415)234-5678 | For US sellers: +12344155678 415-234-5679 |
+61298765432 or +61 2 9876 5432 | For AU sellers: 298765432 2 9876 5432 02 9876 5432 (02) 9876-5432 +61-2-9876-5432 | For AU sellers: +61 3 9876 5432 2 9876 5431 |
Fuzzy search by phone number works in a similar way to fuzzy search by email addresses. A phone number is tokenized by removing non-numeric characters. Matched customer profiles are those whose phone_number
field has tokens that match all the provided tokens in the search query at least once in any order of tokens.
For a fuzzy match operation, Square first removes non-numeric characters from the query expression and then tokenizes the expression on spaces. For example, +33
becomes 33
and (234) 567-8901
becomes 234 5678901
.
To use the fuzzy search by phone number, you can specify part of a customer's phone number. For example:
Search customers
As another example, if you use the following fuzzy search filter:
Search customers
You get all the US customers whose phone numbers use the following formats:
- (234) 567-NNNN, (234) N56-7NNN, (234) NN5-67NN, (234) NNN-567N, (234) NNN-N567
- (n23) 456-7NNN, (n23) 4N5-67NN, (n23) 4NN-567N, (n23) 4NN-N567
- (nN2) 345-67NN, (nN2) 34N-567N, (nN2) 34N-N567
- (nNN) 234-567N, (nNN) N23-4567, (nNN) 234-N567
- (nNN) 567-234N, (nNN) N56-7243, (nNN) 567-N234
- (nN5) 672-34NN, (nN5) 67N-234N, (nN5) 67N-N234
- (n56) 723-4NNN, (n56) 7N2-34NN, (n56) 7NN-234N, (n56) 7NN-N234
- (567) 234-NNNN, (567) N23-4NNN, (567) NN23-4NN, (567) NNN-234N, (567) NNN-N234
Where "n" stands for any number between 1 and 9 and "N" stands for a number between 0 and 9. This example shows that the fuzzy phone_number
filter matches a query expression with the stored phone_number
field token by token, irrespective of the token order.
Fuzzy search by phone numbers can be powerful. For example, you can use a fuzzy search filter to offer likely matches that users can select while they enter the phone number digit by digit, provided that you have defined metrics for the likelihood of the match.
When a Square customer profile is associated with an external entity in another system, the association is captured by the reference_id
field of the Square Customer object.
Given the ID of an external entity, you can use the Square Customers API search by reference ID to determine the associated customer profiles in a Square seller's Customer Directory. To do so, you specify a reference_id
filter when calling the SearchCustomers endpoint.
As with search by email address and by phone number, the reference_id
search filter lets you query customer profiles with an exact or fuzzy match of the query expression against the value of the reference_id
field.
To search Square customer profiles by reference ID with an exact match, use a reference_id
filter as shown in the following example search request:
Search customers
The response returns the Square customer profiles whose reference_id
field is exactly ZGYZ-ABZG-97YX-XJ16
.
To perform a fuzzy search by reference ID, use a fuzzy search filter as shown in the following example search filter:
Search customers
This filter returns the customer profiles whose reference_id
field contains the ZGYZ
and 97YX
tokens at least once each and in any order.
As a string value, a reference ID is tokenized by removing non-alphanumeric characters. A customer profile is selected if each provided token is matched at least once against the profile's reference_id
field, irrespective of the order of the tokens.
Fuzzy search by reference ID can be a useful tool for managing customer profiles. For example, when used with a structured, multi-part identifier schema, a fuzzy search helps simplify the discovery of customer profiles in a structured way by matching part of a reference ID (for example, a prefix or suffix) or by querying reference IDs that contain multiple tokens.
Search query | Matched reference_ids | Unmatched reference_ids |
---|---|---|
XJ15 | XJ15-ABZG-97YX-ZGYZ, ZGYZ-ABZG-97YX-XJ15 | ZGYZ-ABZG-97YX-XJ16 |
NYC M | NYC_M_35_JOHNSON NYC27MURRAY | NYC-F-27_WILSON SFO_M_35_THOMAS |