<p>Try</p>

<pre><code>return [
'endpoints' =&gt; [
    'products.json' =&gt; [
        'elementType' =&gt; 'Commerce_Product',
        'criteria' =&gt; [
            // 'category' =&gt; 'products', // randomly trying stuff here!
            // 'section' =&gt; 'products',
            // 'type' =&gt; 'products',
        ],
        'transformer' =&gt; function(Commerce_ProductModel $product) {
            return [
                'title' =&gt; $product-&gt;title,
                'url' =&gt; $product-&gt;url,
            ];
        },
    ]
]]
</code></pre>

CC BY-SA 3.0
2017-03-23T00:16:29.007Z
Thanks, but that's giving me the classic: No element type exists by the type “Commerce_Products”.
Chilly
CC BY-SA 3.0
2017-03-23T00:17:41.980Z
Ah! drop the 's' ! : Commerce_Product

thank you!
Chilly
CC BY-SA 4.0
2018-10-02T09:33:54.130Z
If you are using Commerce 2, the `Commerce_Product` should be `Product` and add a `use craft\commerce\elements\Product` at the top of your file.
Luke Holder
CC BY-SA 3.0
2017-03-23T00:07:59.520Z
2017-03-23T04:49:07.210Z
2017-03-23T04:49:07.210Z
Luke Holder
<p>For Commerce 2</p>

<pre><code>use craft\commerce\elements\Product;
return [
    'endpoints' =&gt; [
    'products.json' =&gt; [
        'elementType' =&gt; Product::class,
        'transformer' =&gt; function(Product $product) {
            return [
                'title' =&gt; $product-&gt;title,
                'url' =&gt; $product-&gt;url,
            ];
        },
    ],
    ]
];
</code></pre>

CC BY-SA 4.0
2018-10-08T02:59:08.923Z
2018-10-08T02:59:08.923Z
Jack V
<p>The <a href="https://github.com/craftcms/element-api/tree/v1#elementtype-required" rel="nofollow noreferrer">elementApi</a> docs state you can use custom elementTypes via plugins.  I am using craft commerce which states it creates the custom elementType '<a href="https://craftcommerce.com/docs/purchasables" rel="nofollow noreferrer">purchasable</a>'</p>

<p>I've tried:</p>

<pre><code>return [
'endpoints' =&gt; [
    'category.json' =&gt; [
        'elementType' =&gt; 'purchasable',
        'criteria' =&gt; [
            // 'category' =&gt; 'products', // randomly trying stuff here!
            // 'section' =&gt; 'products',
            // 'type' =&gt; 'products',
        ],
        'transformer' =&gt; function(EntryModel $entry) {
            return [
                'title' =&gt; $entry-&gt;title,
                'url' =&gt; $entry-&gt;url,
            ];
        },
    ]
]]
</code></pre>

<p>I've tried many variations on the word purchasable, as we as random guesses like 'product', 'item' etc etc</p>

<p>I should mention I am able to JSON back with all categories, Entries etc etc</p>

<p>I am sure this is possible, as craft, craftcommerce and elementApi are all made by the same guys!</p>

<p>Any help would be amazing</p>

CC BY-SA 3.0
2017-03-23T00:04:53.253Z
What's the class name of your purchasable?
Brad Bell
<plugin-craftcommerce><plugin-elementapi>
Get 'purchasable' from ElementAPI
# Get 'purchasable' from ElementAPI

(Asked by: username_0 on 2017-03-22)

The elementApi docs state you can use custom elementTypes via plugins.  I am using craft commerce which states it creates the custom elementType 'purchasable'
I've tried:
return [
'endpoints' => [
    'category.json' => [
        'elementType' => 'purchasable',
        'criteria' => [
            // 'category' => 'products', // randomly trying stuff here!
            // 'section' => 'products',
            // 'type' => 'products',
        ],
        'transformer' => function(EntryModel $entry) {
            return [
                'title' => $entry->title,
                'url' => $entry->url,
            ];
        },
    ]
]]

I've tried many variations on the word purchasable, as we as random guesses like 'product', 'item' etc etc
I should mention I am able to JSON back with all categories, Entries etc etc
I am sure this is possible, as craft, craftcommerce and elementApi are all made by the same guys!
Any help would be amazing

**Tags:** plugin-craftcommerce, plugin-elementapi
> **Comments:**
> **username_1**: What's the class name of your purchasable?  

---

## 2 answers

### Answer by username_2 on 2017-03-23. Score: 3 | Accepted answer

Try
return [
'endpoints' => [
    'products.json' => [
        'elementType' => 'Commerce_Product',
        'criteria' => [
            // 'category' => 'products', // randomly trying stuff here!
            // 'section' => 'products',
            // 'type' => 'products',
        ],
        'transformer' => function(Commerce_ProductModel $product) {
            return [
                'title' => $product->title,
                'url' => $product->url,
            ];
        },
    ]
]]

> **Comments:**
> **username_0**: Thanks, but that's giving me the classic: No element type exists by the type “Commerce_Products”.  
> **username_0**: Ah! drop the 's' ! : Commerce_Product  thank you!  
> **username_2**: If you are using Commerce 2, the `Commerce_Product` should be `Product` and add a `use craft\commerce\elements\Product` at the top of your file.  

---

### Answer by username_3 on 2018-10-08. Score: 1

For Commerce 2
use craft\commerce\elements\Product;
return [
    'endpoints' => [
    'products.json' => [
        'elementType' => Product::class,
        'transformer' => function(Product $product) {
            return [
                'title' => $product->title,
                'url' => $product->url,
            ];
        },
    ],
    ]
];


---

