Using Magento 2's Customer Search REST API
Using Magento 2's Customer Search REST API
Magento 2's Customer Search REST API (V1/customers/search) allows you to retrieve customer information based on specific criteria. This is particularly useful for managing customer data programmatically.
Table Of Content
Using Magento 2's Customer Search REST API
Making a GET Request
To search for customers, send a GET request to:
<HOST_URL>/rest/V1/customers/search
Replace <HOST_URL> with your Magento store's base URL.
Setting Up the Authorization Header
Include the admin token in the Authorization header:
Authorization: Bearer <ADMIN_TOKEN>
Constructing the Search Criteria
Use the searchCriteria parameter to define your search filters. For example, to find a customer by email:
searchCriteria[filterGroups][0][filters][0][field]=email&
searchCriteria[filterGroups][0][filters][0][value]=emmo.net.co&
searchCriteria[filterGroups][0][filters][0][conditionType]=eq
Example Request
Here's how the complete GET request might look:
If you need to delete all indices, execute:
GET <HOST_URL>/rest/V1/customers/search?
searchCriteria[filterGroups][0][filters][0][field]=email&
searchCriteria[filterGroups][0][filters][0][conditionType]=eq
Ensure you replace <HOST_URL> with your store's base URL.
Understanding the Response
A successful response includes customer details matching your criteria. For example:
{
"items": [
{
"id": 3,
"group_id": 1,
"default_billing": "2",
"default_shipping": "2",
"created_at": "2023-10-05 04:54:09",
"updated_at": "2023-10-06 12:36:01",
"created_in": "Default",
"email": "[email protected]",
"firstname": "shehzad",
"lastname": "ali",
"store_id": 1,
"website_id": 1
"addresses": [
{
"id": 2,
"customer_id": 3,
"region": {
"region_code": "ammani",
"region": "hunza",
"region_id": 0
},
"region_id": 0,
"country_id": "GB",
"street": [
"my Street"
],
"company": "Test",
"telephone": "03554645978"
"postcode": "LS1 5EN",
"city": "Leeds",
"firstname": "shehzad",
"lastname": "ali",
"default_shipping": true,
"default_billing": true
}
],
"disable_auto_group_change": 0,
"extension_attributes": {
"company_attributes": {
"company_id": 2,
"status": 1
},
}
}
],
"search_criteria": {
"filter_groups": [
{
"filters": [
{
"field": "email",
"value": "[email protected]",
"condition_type": "eq"
}
]
}
]
},
"total_count": 1
}
This response provides detailed information about the customer, including their addresses and subscription status.
Searching by Multiple Criteria
You can combine multiple filters in a single request. For instance, to search by both email and first name:
searchCriteria[filterGroups][0][filters][0][field]=email&
searchCriteria[filterGroups][0][filters][0][value]=emmo.net.co&
searchCriteria[filterGroups][0][filters][0][conditionType]=eq&
searchCriteria[filterGroups][1][filters][0][field]=firstname&
searchCriteria[filterGroups][1][filters][0][value]=emmo&
searchCriteria[filterGroups][1][filters][0][conditionType]=eq
This filters customers where the email equals [email protected] and the first name equals Rakesh.
Handling Nested Attributes
When dealing with nested attributes, such as customer addresses, you can use the fields parameter to specify which fields to include in the response. For example, to retrieve only the telephone number of a customer's address:
GET <HOST_URL>/rest/V1/customers/3/?fields=addresses[telephone]
This request returns only the telephone number of the customer's address with ID 3.
Tip
To enhance your eCommerce store’s performance with Magento, focus on optimizing site speed by utilizing Emmo themes and extensions. These tools are designed for efficiency, ensuring your website loads quickly and provides a smooth user experience. Start leveraging Emmo's powerful solutions today to boost customer satisfaction and drive sales!
FAQs
What Is the Customer Search REST API in Magento 2?
The Customer Search REST API in Magento 2, accessed via V1/customers/search
, allows you to retrieve customer information programmatically based on specific criteria.
Why Use the Customer Search REST API?
It enables efficient customer data management, helping developers automate and streamline operations like fetching customer details based on filters such as email or name.
How Do You Make a GET Request?
Send a GET request to <HOST_URL>/rest/V1/customers/search
. Replace <HOST_URL>
with your Magento store's base URL.
How Do You Authenticate Requests?
Include the admin token in the Authorization header of the request as: Authorization: Bearer <ADMIN_TOKEN>
.
What Are Search Criteria?
Search criteria allow you to filter customer data based on fields like email, name, or group ID. For example, filter by email using: searchCriteria[filterGroups][0][filters][0][field]=email
.
How Can You Combine Multiple Filters?
You can combine filters by adding multiple filterGroups
to the request. For instance, to filter by both email and first name, include separate filter groups for each field.
What Does the API Response Include?
The API response contains customer details such as ID, name, email, and addresses. It may also include additional attributes like subscription status or company information.
How Can You Handle Nested Attributes?
Use the fields
parameter to specify which nested attributes to retrieve. For example, to get the telephone number from an address, use: ?fields=addresses[telephone]
.
Can You Search by Multiple Criteria at Once?
Yes, you can combine multiple search filters to refine the data. For example, filter by both email and first name by adding respective criteria in searchCriteria
.
How Can You Debug API Issues?
Check your server logs for errors, ensure the API endpoint is correct, and verify the connection between Magento and the API. Test using tools like Postman for quick debugging.