Using multiple elementTypes in Element API I am currently trying to create an autocomplete search with the element-api. I need results from two different elementTypes:


Product::class
Entry::class


How do I get results out of both elementTypes with only one endpoint?

Thats my working Product::class element-api.php config file:

return [
    'endpoints' =&gt; [
        'search.json' =&gt; function() {
        // settings
        $phrase = Craft::$app-&gt;request-&gt;getParam('query');

        $criteria = [
            'search' =&gt; 'title:'.$phrase,
        ];

            return [
                'elementType' =&gt; Product::class,
                'criteria' =&gt; $criteria,
                'paginate' =&gt; false,
                'transformer' =&gt; function(Product $product) {
                return [
                    'title' =&gt; $product-&gt;title,
                    'url' =&gt; $product-&gt;url,
                ];
            },
          ];

        },
    ]
];
The results query could not be the same, you have to separate them.
use craft\commerce\elements\Product;
use craft\elements\Entry;

return [
    'endpoints' =&gt; [
        'search/product.json' =&gt; function () {
            // settings
            $phrase   = Craft::$app-&gt;request-&gt;getParam('query');
            $criteria = [
                'search' =&gt; 'title:' . $phrase,
            ];

            return [
                'elementType' =&gt; Product::class,
                'criteria'    =&gt; $criteria,
                'paginate'    =&gt; false,
                'transformer' =&gt; function (Product $product) {
                    return [
                        'title' =&gt; $product-&gt;title,
                        'url'   =&gt; $product-&gt;url,
                    ];
                },
            ];
        },
        'search/news.json' =&gt; function () {
            // settings
            $phrase   = Craft::$app-&gt;request-&gt;getParam('query');
            $criteria = [
                'search' =&gt; 'title:' . $phrase,
            ];

            return [
                'elementType' =&gt; Entry::class,
                'criteria'    =&gt; $criteria,
                'paginate'    =&gt; false,
                'transformer' =&gt; function (Entry $entry) {
                    return [
                        'title' =&gt; $entry-&gt;title,
                        'url'   =&gt; $entry-&gt;url,
                    ];
                },
            ];
        },
    ]
];

UPDATED: 18/12/2020
Anyway, I've found a new way that allows you o use the multiple entries in an endpoint.
&lt;?php

use craft\commerce\elements\Product;
use craft\elements\Entry;

return [
    'endpoints' =&gt; [
        'search.json' =&gt; function () {
            // settings
            $phrase         = \Craft::$app-&gt;request-&gt;getParam('query');
            $articles       = Entry::find()-&gt;search($phrase)-&gt;limit(10)-&gt;all();
            $articleObjects = [];

            if ($articles) {
                $articleTemplateName = ''; // your article template path
                foreach ($articles as $article) {
                    $articleObjects[] = \Craft::$app-&gt;getView()-&gt;renderTemplate($articleTemplateName, ['post' =&gt; $article]);
                }
            }

            return [
                'elementType' =&gt; Product::class,
                'criteria'    =&gt; [
                    'search' =&gt; 'title:' . $phrase,
                ],
                'paginate'    =&gt; false,
                'transformer' =&gt; function (Product $product) {
                    return [
                        'title' =&gt; $product-&gt;title,
                        'url'   =&gt; $product-&gt;url,
                    ];
                },
                'meta'        =&gt; [
                    'news' =&gt; $articleObjects
                ]
            ];
        },
    ]
];