Element API — Endpoint has an invalid elementType I'm trying the Element API for the first time and getting this error message {"error":{"code":404,"message":"Endpoint has an invalid elementType"}}

I cannot for the life of me figure out what I'm missing... I've read the docs, followed the Mijingo "Creating an Element API video", read Andrew Welch's post (https://nystudio107.com/blog/lazy-loading-with-the-element-api-vuejs) and hunted through Stack Exchange but haven't came across any fixes.

This is from my current element-api.php file:

&lt;?php
namespace Craft;

return [
    'endpoints' =&gt; [
        'api/news.json' =&gt; [
            'elementType' =&gt; 'Entry',
            'criteria' =&gt; ['section' =&gt; 'news'],
            'transformer' =&gt; function(EntryModel $entry) {
                return [
                    'title' =&gt; $entry-&gt;title,
                    'body' =&gt; (string) $entry-&gt;newsText
                ];
            }
        ],
    ]
];


Previously, I tried changing the code slightly to:

&lt;?php

use craft\elements\Entry;
use craft\helpers\UrlHelper;

return [
    'endpoints' =&gt; [
        'news.json' =&gt; function() {
            return [
                'elementType' =&gt; Entry::class,
                'criteria' =&gt; ['section' =&gt; 'news'],
                'transformer' =&gt; function(Entry $entry) {
                    return [
                        'title' =&gt; $entry-&gt;title,
                        'url' =&gt; $entry-&gt;url,
                        'jsonUrl' =&gt; UrlHelper::url("news/{$entry-&gt;id}.json"),
                        'newsTitle' =&gt; $entry-&gt;newsTitle,
                    ];
                },
            ];
        },
    ]
];


but still no luck. With the second block of code the error message I get is Template not found: api/news.json

Apologies if I'm missing something completely obvious, this is the first time using the plugin, and I don't know PHP, but following along with the Mijingo video helped me understand what was going on.

If anyone has any suggestions to fix this that would be greatly appreciated!

Cheers
Your second example has the correct elementType value (Entry::class with a corresponding Entry import at the top of the file). Alternatively you could do



'elementType' =&gt; 'craft\elements\Entry',


The error you’re getting in that example is because you changed the endpoint to just news.json rather than api/news.json. So the request isn’t getting routed to this endpoint in the first place.