Getting Commerce Products using Element API in Craft 3 I am trying to get Products using the Element API plugin. I set the 'elementType' to Commerce_Products, CommerceProducts, etc.., but the endpoint keeps returning:


  Endpoint has an invalid elementType


This is my endpoint:

use craft\commerce\elements\Product as ProductsModel;

return [
    'endpoints' =&gt; [
        'api/search' =&gt; [
            'elementType' =&gt; 'ProductsModel',
            'paginate' =&gt; false,
            'pretty' =&gt; true,
            'criteria' =&gt; [
                'limit' =&gt; 15,
                'search' =&gt; (Craft::$app-&gt;request-&gt;getParam('keyName')) ? 'title:'.'*'.Craft::$app-&gt;request-&gt;getParam('keyName').'*' : ''
            ],
            'transformer' =&gt; function(ProductsModel $product) {
                return [
                    'title' =&gt; $product-&gt;title,
                    'url' =&gt; $product-&gt;url
                ];
            },
        ],
    ]
];


How do I fix it?
You're so close! Just one change (see the elementType key, and the added comment)…

use craft\commerce\elements\Product;

return [
    'endpoints' =&gt; [
        'api/search' =&gt; [
            'elementType' =&gt; Product::class, // This provides Element API the fully qualified Element namespace + class.
            'paginate' =&gt; false,
            'pretty' =&gt; true,
            'criteria' =&gt; [
                'limit' =&gt; 15,
                'search' =&gt; (Craft::$app-&gt;request-&gt;getParam('keyName')) ? 'title:'.'*'.Craft::$app-&gt;request-&gt;getParam('keyName').'*' : ''
            ],
            'transformer' =&gt; function(Product $product) {
                return [
                    'title' =&gt; $product-&gt;title,
                    'url' =&gt; $product-&gt;url
                ];
            },
        ],
    ]
]



  Note: I'd recommend leaving the use statement as just Product, as I've done above. What you're importing is a class that inherits from the Craft Element class… “Model” has some other specific connotations in Craft + Yii, and even though at a lower level, Elements are still models, I think it's a good idea to leave them named as-is… after all, this is the Element API!