<p>You're so close! Just one change (see the <code>elementType</code> key, and the added comment)…</p>

<pre><code>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
                ];
            },
        ],
    ]
]
</code></pre>

<blockquote>
  <p><strong>Note:</strong> I'd recommend leaving the <code>use</code> statement as just <code>Product</code>, as I've done above. What you're importing is a class that inherits from the Craft <code>Element</code> 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 <em>Element</em> API! </p>
</blockquote>

CC BY-SA 4.0
2018-12-10T21:55:41.943Z
For those curious folks out there, this is effectively no different than providing a string `'craft\commerce\elements\Product'`, but Craft 3 seems to express a pretty strong preference for the `::class` constant, instead.
August Miller
CC BY-SA 4.0
2018-12-10T21:50:49.867Z
2018-12-10T21:53:46.490Z
2018-12-10T21:53:46.490Z
August Miller
<p>I am trying to get Products using the Element API plugin. I set the 'elementType' to <code>Commerce_Products</code>, <code>CommerceProducts</code>, etc.., but the endpoint keeps returning:</p>

<blockquote>
  <p>Endpoint has an invalid elementType</p>
</blockquote>

<p>This is my endpoint:</p>

<pre><code>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
                ];
            },
        ],
    ]
];
</code></pre>

<p>How do I fix it?</p>

CC BY-SA 4.0
2021-03-12T06:40:41.010Z
How can i return image url for product?
user11651
<plugin-elementapi><plugin-craftcommerce2>
Getting Commerce Products using Element API in Craft 3
# Getting Commerce Products using Element API in Craft 3

(Asked by: username_0 on 2018-12-10)

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' => [
        'api/search' => [
            'elementType' => 'ProductsModel',
            'paginate' => false,
            'pretty' => true,
            'criteria' => [
                'limit' => 15,
                'search' => (Craft::$app->request->getParam('keyName')) ? 'title:'.'*'.Craft::$app->request->getParam('keyName').'*' : ''
            ],
            'transformer' => function(ProductsModel $product) {
                return [
                    'title' => $product->title,
                    'url' => $product->url
                ];
            },
        ],
    ]
];

How do I fix it?

**Tags:** plugin-elementapi, plugin-craftcommerce2

> **Comments:**
> **username_1**: How can i return image url for product?  

---

## 1 answers

### Answer by username_2 on 2018-12-10. Score: 3 | Accepted answer

You're so close! Just one change (see the elementType key, and the added comment)…
use craft\commerce\elements\Product;

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

> **Comments:**
> **username_2**: For those curious folks out there, this is effectively no different than providing a string `'craft\commerce\elements\Product'`, but Craft 3 seems to express a pretty strong preference for the `::class` constant, instead.  

---

