How to use the Element API plugin only when logged in? I want to use the Element API plugin, but the content must only be visible when users are logged in. How I do this for a section? Is it possible to use requirelogin or currentUser for this?
If you want to restrict the whole API, you can add some code to the top of your /config/elementapi.php file (right below namespace Craft;) that checks for a logged in user, and ends the request with an appropriate status if the user isn't logged in:

&lt;?php
namespace Craft;

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


Note that you can replace the 404 with another HTTP status code if you want to; pick whatever feels appropriate to your app.

If you want to restrict specific endpoints, but leave others open, the best approach is probably to wrap the above code in a function, i.e. 

&lt;?php 
namespace Craft;

function requireLogin() {
    $user = craft()-&gt;userSession-&gt;getUser();
    if (!$user) {
        HeaderHelper::setHeader(array('status' =&gt; 404));
        craft()-&gt;end();
    }
}

return [
    ...


Then, it's simply a matter of calling this function from your restricted endpoints' transformer callbacks:

'news.json' =&gt; [
    'elementType' =&gt; 'Entry',
    'criteria' =&gt; ['section' =&gt; 'news'],
    'transformer' =&gt; function(EntryModel $entry) {

        // This endpoint should be restricted
        requireLogin();

        return [
            'title' =&gt; $entry-&gt;title,
            ...
        ];

    },
],
...