limit number of results using magento API V2

limit number of results using magento API V2

August 24, 2026 · Illustrated author avatarBy Stefan Bogdanescu
limit number of results using magento API V2

Dear Developer,

I understand your frustration in trying to limit the number of results from Magento API V2 using the customerCustomerList method. The documentation often lacks clarity on such basic yet crucial requirements.

Let’s dive into solving this issue step-by-step, keeping the principles of simplicity and efficiency in mind.

Understanding the Magento API V2

The Magento API V2 uses SOAP (Simple Object Access Protocol) for communication between your application and the Magento server. It provides a set of methods that allow you to interact with various functionalities like catalog management, order processing, customer management, etc.

When it comes to retrieving data, most methods return all available results by default. However, they do not provide an out-of-the-box way to limit the number of returned records.

The Challenge: Limiting Results

Your current code snippet retrieves a list of all customers:

$result = $client->__soapCall('customerCustomerList', $data);

This will return every customer record in your Magento store, which may not be feasible if you have thousands of customers. Loading such large data sets into memory can lead to performance issues and timeouts.

The Solution: Pagination

The best approach is to use pagination, allowing you to retrieve a fixed number of records at a time. Unfortunately, the customerCustomerList method in Magento API V2 does not support pagination directly.

However, we can work around this limitation by using a combination of filters and a custom script on the server side.

Step-by-Step Implementation

  1. Filtering by Date: The StackOverflow post you mentioned suggests filtering customers by their creation date. While this won’t limit the total number of results, it can help narrow down the scope if your customer list is large due to historical data.

  2. Server-side Scripting: To implement true pagination, you’ll need to create a custom script on the Magento server that accepts page size and offset parameters. This script will then return the required subset of customers based on those specifications.

  3. API Method for Pagination: Create a new method in your API configuration file (app/code/core/Mage/Api/etc/api.xml or app/code/local/Mage/Api/etc/api.xml if you’re using local code pool) to handle this functionality:

<method name="customerCustomerListPaged">
    <acl>default, all</acl>
    <resources>
        <resource id="Magento_Customer::manage_customers" />
    </resources>
    <actions>
        <action key="read">Allow</action>
    </actions>
    <parameters>
        <parameter name="sessionId" type="string">
            <description>Session ID</description>
        </parameter>
        <parameter name="pageSize" type="integer">
            <description>Number of items per page</description>
        </parameter>
        <parameter name="currentPage" type="integer">
            <description>Current page number</description>
        </parameter>
    </parameters>
</method>
  1. Create the Server-side Function: In your Magento module’s controller or model, create a function to handle this method:
public function customerCustomerListPaged()
{
    $sessionId = (string)$this->getSession();
    $pageSize = (int)$this->getRequest()->getParam('pageSize');
    $currentPage = (int)$this->getRequest()->getParam('currentPage');

    $offset = ($currentPage - 1) * $pageSize;

    // Assuming you have a custom model or collection to retrieve customers
    $customers = Mage::getModel('customer/customer')->getCollection()
        ->setPageSize($pageSize)
        ->setCurPage($currentPage);

    return $this->responseFactory->create()->setData($customers);
}
  1. Call the New Method in Your API:
$api_soap_url = 'http://localhost/magento/api/v2_soap?wsdl=1';

$client = new SoapClient($api_soap_url);
$session_id = $client->__soapCall('login', array($user, $pw));

$data = array(
    'sessionId' => $session_id,
    'pageSize' => 50, // Set your desired page size
    'currentPage' => 1 // Set the current page number
);

$result = $client->__soapCall('customerCustomerListPaged', $data);

Conclusion

While Magento API V2 doesn’t support pagination out-of-the-box for certain methods like customerCustomerList, we can achieve this functionality with a combination of filters and custom server-side scripting.

This approach ensures that you only retrieve the necessary data for each page, enhancing performance and reducing memory usage. It’s a simple yet effective way to manage large datasets without rewriting complex classes or using unnecessary database queries.

Remember, pagination is a best practice in web development, not just a workaround for Magento API limitations. It provides users with an intuitive interface to navigate through data sets, improving the overall user experience.

Don’t hesitate to experiment and customize this solution further based on your specific requirements. If you need more advanced filtering or sorting options, you might want to look into creating custom resource models in Magento.

Happy coding! Let me know if you have any other questions. You can also check out our other articles on Laravel Company for more insights on Magento development and API best practices.

Best, [Your Name]

Catalog Performance API Orders

Our network