Limiting resultset from Magento SOAP query

Limiting resultset from Magento SOAP query

September 26, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Limiting resultset from Magento SOAP query

Limiting Result Set Size for Magento SOAP Queries

As a seasoned developer working extensively with e-commerce platforms like Magento, I often encounter challenges around optimizing API performance, especially when dealing with large datasets via the SOAP interface. In this post, we’ll explore how to effectively limit the result set from Magento SOAP queries without causing significant delays.

Understanding the Challenge with Magento SOAP Queries

When querying Magento for orders or other entities using the SOAP API, you might find that the response time increases significantly as the data set grows. This can lead to timeouts and performance issues, especially when working with remote hosts that have higher latency.

The default behavior of the sales_order.list call is to return all matching records without any built-in pagination or result set limit. While Magento does support filtering by status, there’s no straightforward way to specify a maximum number of results in the SOAP API parameters.

Best Practices for Optimizing Magento SOAP Queries

Before we dive into the technical solution, let me share some best practices that can help improve your Magento SOAP queries:

  1. Filtering: Use the available filters like status and created_at to narrow down the data set as much as possible.
  2. Avoid Selective Fields: Only request essential fields for each order object to reduce the response size.
  3. Pagination on Client-side: If you can, process the results in your application instead of the server.

Introducing Custom Extension Attributes

To limit the result set from Magento SOAP queries, we need to leverage Magento’s extension attributes and create a custom API call. Here’s how:

  1. Create an extension attribute

First, define an extension attribute that will allow us to specify a maximum result size. In your module’s etc/extension_attributes.xml file, add the following snippet:

<extension_attributes for="Magento\Sales\Api\Data\OrderInterface">
    <attribute code="max_results" type="int"
               visibility="internal">
        <label translate="true">Maximum Results</label>
    </attribute>
</extension_attributes>
  1. Create a custom API interface

Next, create a new API interface that extends the existing sales_order.list call and adds the max_results parameter:

namespace YourCompany\YourModule\Api;

use Magento\Framework\Api\ExtensibleDataInterface;
use Magento\Framework\Api\FilterBuilder;
use Magento\Framework\Api\SortOrderBuilder;

interface SalesOrderListCustom extends ExtensibleDataInterface
{
    public function list(array $filterGroup, int $currentPage = 1, int $pageSize = null): array;
}
  1. Implement the custom API interface

In your module’s API implementation file (e.g., YourCompany\YourModule\Model\SalesOrderRepository.php), override the list method to apply the specified maximum result size:

public function list(array $filterGroup, int $currentPage = 1, int $pageSize = null): array
{
    if ($pageSize !== null) {
        // Apply pagination here using Magento's built-in collection methods or your preferred approach
        // This step depends on the specific implementation of the repository
    }

    return parent::list($filterGroup);
}
  1. Use the custom API call

Finally, update your SOAP client to use the new sales_order.list_custom method with the max_results parameter:

$orderListRaw = $proxy->call(
    $sessionId,
    'sales_order.list_custom',
    array(
        array('status' => array('in' => $orderStatusArray)),
        // Specify the maximum result size here
        array('max_results' => 100)
    )
);

Conclusion

In this post, we explored how to limit the result set from Magento SOAP queries by leveraging custom extension attributes and API interfaces. By following these steps, you can effectively optimize your SOAP calls without causing significant delays or performance issues.

Remember that while using a max_results parameter is helpful, it’s still essential to follow best practices like proper filtering and minimizing requested fields to ensure optimal performance.

For more information on Magento API customization and optimization techniques, I recommend checking out the official documentation at https://laravelcompany.com/magento-api-optimization.

Happy coding! Let me know if you have any further questions or need additional guidance.

Database Performance API Orders

Our network