cancel
Showing results for 
Search instead for 
Did you mean: 

REST API list of customers ?

REST API list of customers ?

Hi all,

 

I'm tring to get the list of all my store customers through the REST API; work fine with an uniq identifier :

V1/rest/customers/1

but how to get the full list of customers or search with filters?

it seems that for example :

GET http://<magento_host>/rest/V1/customers?
searchCriteria[filter_groups][0][filters][0][field]=created_at&
searchCriteria[filter_groups][0][filters][0][value]=2016-07-01 00:00:00&
searchCriteria[filter_groups][0][filters][0][condition_type]=gt

Thank you,

Thomas

6 REPLIES 6

Re: REST API list of customers ?

Hi Thomas,

 

I was able to use this API to search for customers :

GET    /V1/customers/search

 Here is the bash command :

curl -X GET "http://my.website.com/index.php/rest/V1/customers/search? \
searchCriteria[filterGroups][0][filters][0][field]=email& \
searchCriteria[filterGroups][0][filters][0][value]=%& \
searchCriteria[filterGroups][0][filters][0][condition_type]=like" \
-H "Authorization: Bearer xxxxxxxxxxxxxxxxxxxxx" \
-H "Content-type: application/json" -g

But I'm still enable to use it in a php script, see my code below :

<?php

$search = [
    'searchCriteria' => [
        "filterGroups" => [
            "filters" => [
                "field" => "email",
                "value" => "%",
                "condition_type" => "like"
            ]
        ]
    ]
];                 

$token = "xxxxxxxxxxxxxxxxx";$api = "rest/V1/customers/search";
$ch = curl_init("http://my.website.com/index.php/".$api);

curl_setopt
($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($search)); //curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer ".$token));

$result = curl_exec($ch);
$result
= json_decode($result, 1);
print_r($result); ?>

It always return the message below :

[message] => %fieldName is a required field.
[parameters] => Array
    (
        [fieldName] => searchCriteria
    )

Can someone help about fixing this piece of code ?

 

Re: REST API list of customers ?

Hello,

 

please try below code

 

 

$token = "xxxxxxxxxxxxxxxxx";$api = "rest/V1/customers/search";
$ch = curl_init("http://my.website.com/index.php/".$api."?searchCriteria[filterGroups][0][filters][0][field]=email");

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer ".$token));

 

 


Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer

Re: REST API list of customers ?

Thanks for your answer.

 

With your code, I get this :

 

Array
(
    [items] => Array
        (
        )
    [search_criteria] => Array
        (
            [filter_groups] => Array
                (
                    [0] => Array
                        (
                            [filters] => Array
                                (
                                    [0] => Array
                                        (
                                            [field] => email
                                            [value] => 
                                            [condition_type] => eq
                                        )
                                )
                        )
                )
        )
    [total_count] => 0
)

From your example, I got the expected result by using this :

$ch = curl_init("http://my.website.com/index.php/".$api."?searchCriteria[filterGroups][0][filters][0][field]=email&searchCriteria[filterGroups][0][filters][0][value]=%&searchCriteria[filterGroups][0][filters][0][condition_type]=like"); 

Result :

Array
(
    [items] => Array
        (
            [0] => Array
                (
                    [id] => 1
                    [group_id] => 1
                    [created_at] => 2017-05-02 11:00:12
                    [updated_at] => 2017-05-03 08:24:58
                    [created_in] => myshop
                    [email] => user@example.com
                    [firstname] => John2
                    [lastname] => Doe2
                    [store_id] => 2
                    [website_id] => 1
                    [addresses] => Array
                        (
                        )
                    [disable_auto_group_change] => 0
                )
        )
    [search_criteria] => Array
        (
            [filter_groups] => Array
                (
                    [0] => Array
                        (
                            [filters] => Array
                                (
                                    [0] => Array
                                        (
                                            [field] => email
                                            [value] => %
                                            [condition_type] => like
                                        )
                                )
                        )
                )
        )
    [total_count] => 1
)

But the json is not used, how can it be used ?

Re: REST API list of customers ?

I finally fixed my question with http_build_query() and json_decode().

 

$api = "rest/V1/customers/search";

$json = '
{
    "search_criteria": {
        "filter_groups": [
            {
                "filters": [
                    {
                        "field": "email",
                        "value": "%",
                        "condition_type": "like"
                    }
                ]
            }
        ]
    }
}
';
$j = json_decode($json);
$get_params = http_build_query($j);

$ch = curl_init("http://my.website.com/index.php/".$api."?".$get_params);

 

Re: REST API list of customers ?

hi.. i try with 

"/rest/V1/customers/search?searchCriteria[filter_groups][0][filters][0][field]=created_at&searchCriteria[filter_groups][0][filters][0][value]=#{start_time}&searchCriteria[filter_groups][0][filters][0][condition_type]=from&searchCriteria[filter_groups][1][filters][1][field]=created_at&searchCriteria[filter_groups][1][filters][1][value]=#{end_time}&searchCriteria[filter_groups][1][filters][1][condition_type]=to&searchCriteria[page_size]=10&searchCriteria[current_page]=1"

 

 

 

but pagination not work, can you please suggest any solution
my requirement is I try to search contact with start date and end date but getting only 10 contact (I was try with . => searchCriteria[page_size]=100
but not work only get 10 contact

Re: REST API list of customers ?

I tried with your code 

$json = '
{
    "search_criteria": {
        "filter_groups": [
            {
                "filters": [
                    {
                        "field": "email",
                        "value": "gmail.com",
                        "condition_type": "like"
                    }
                ]
            }
        ],
        "page_size": 10
    }
}
';
$j = json_decode($json);
$get_params = http_build_query($j);
$customername = "gmail.com"; 
$requestUrl = $url.'index.php/rest/V1/customers/search?'.$get_params;

I have 22k customers with like gmail.com but

(
    [items] => Array
        (
        )

    [search_criteria] => stdClass Object
        (
            [filter_groups] => Array
                (
                    [0] => stdClass Object
                        (
                            [filters] => Array
                                (
                                    [0] => stdClass Object
                                        (
                                            [field] => email
                                            [value] => gmail.com
                                            [condition_type] => like
                                        )

                                )

                        )

                )

            [page_size] => 10
        )

    [total_count] => 0
)

can you please tell me how we can search customer by email