<p>If you really want the entries for the <em>currently logged in user</em>, having a parameter for <code>userId</code> in your endpoint URL doesn't really make sense.</p>

<p>Not tested, but something like this should work:</p>

<pre><code>'api/v1/user.json' =&gt; function () {

    $author = craft()-&gt;userSession-&gt;getUser();

    if (!$author) {
        HeaderHelper::setHeader(array('status' =&gt; 404));
        craft()-&gt;end();
    }

    return [
        'elementType' =&gt; ElementType::Entry,
        'criteria' =&gt; [
            'section' =&gt; 'jobs',
            'authorId' =&gt; $author-&gt;id,
        ],
    ];
}
</code></pre>

<p>Notice that the above example endpoint would be available at <code>http://example.com/api/v1/user.json</code>, where your example code would create an endpoint with a required <code>userId</code> parameter, i.e. <code>http://example.com/api/v1/user/12345.json</code>.</p>

<p>Also notice that the currently logged in user is pulled via the <a href="https://craftcms.com/classreference/services/UserSessionService" rel="nofollow noreferrer">UserSessionService</a> (<code>craft()-&gt;userSession</code>). </p>

<p>Finally, note that you should use <code>'authorId' =&gt; $author-&gt;id</code> in your criteria – not <code>'author' =&gt; $author</code>.</p>

CC BY-SA 3.0
2017-09-17T21:03:31.287Z
2017-09-17T21:09:19.883Z
2017-09-17T21:09:19.883Z
Mats Mikkel Rummelhoff
<p>How do I use the elementapi to get the list of entries for the logged in user?</p>

<p>I've tried a couple of options to no avail, like </p>

<pre><code>'api/v1/user/&lt;userId:\d+&gt;.json' =&gt; function($userId) {

            $author = craft()-&gt;users-&gt;getUserById($userId);

            return [
                'elementType' =&gt; 'Entry',
                'criteria' =&gt; [
                    'section' =&gt; 'jobs',
                    'author' =&gt; $author
                ],
...
</code></pre>

<p>as an example of one I thought should have worked but it just returns everything.</p>

<elementcriteriamodel><plugin-elementapi>
getting the current user entries from elementapi
# getting the current user entries from elementapi

(Asked by: username_0 on 2017-09-17)

How do I use the elementapi to get the list of entries for the logged in user?
I've tried a couple of options to no avail, like 
'api/v1/user/.json' => function($userId) {

            $author = craft()->users->getUserById($userId);

            return [
                'elementType' => 'Entry',
                'criteria' => [
                    'section' => 'jobs',
                    'author' => $author
                ],
...

as an example of one I thought should have worked but it just returns everything.

**Tags:** elementcriteriamodel, plugin-elementapi

---

## 1 answers

### Answer by username_1 on 2017-09-17. Score: 2 | Accepted answer

If you really want the entries for the currently logged in user, having a parameter for userId in your endpoint URL doesn't really make sense.
Not tested, but something like this should work:
'api/v1/user.json' => function () {

    $author = craft()->userSession->getUser();

    if (!$author) {
        HeaderHelper::setHeader(array('status' => 404));
        craft()->end();
    }

    return [
        'elementType' => ElementType::Entry,
        'criteria' => [
            'section' => 'jobs',
            'authorId' => $author->id,
        ],
    ];
}

Notice that the above example endpoint would be available at http://example.com/api/v1/user.json, where your example code would create an endpoint with a required userId parameter, i.e. http://example.com/api/v1/user/12345.json.
Also notice that the currently logged in user is pulled via the UserSessionService (craft()->userSession). 
Finally, note that you should use 'authorId' => $author->id in your criteria – not 'author' => $author.


---

