Get Assets in JSON with ElementAPI I'm trying to integrate the ElementAPI plugin into my site. Essentially, I need to output all of the assets within the website into a JSON file that I can link to in the load manifest of PreloadJS. If you read the docs here, you'll see that the images can be loaded from an external JSON file.

Here's what I've been working with so far:

&lt;?php
namespace Craft;

return [
    'endpoints' =&gt; [
        'assets.json' =&gt; [
            'elementType' =&gt; 'Entry',
            'criteria' =&gt; ['section' =&gt; 'news'],
            'transformer' =&gt; function (EntryModel $entry) {
$images = [];
        foreach ($entry-&gt;featuredImage as $image) {
        $images[] = $image-&gt;url;
        }
                return [
                    'image' =&gt; $entry-&gt;image
                    'jsonUrl' =&gt; UrlHelper::getUrl("assets/data.json")
                ];
            },
        ]
    ]
];


So, to sum up, what I'm needing to do is build an API that will get a list of all the assets used in my site and output them into a JSON file that I can link to in a JS file as part of the PreloarJs plugin. This would make my preloader dynamic and allow me to keep my javascript external, rather than getting the assets inline, which I have been doing.

Thanks!
Something like the following should work:

// elementapi.php
return [
    'endpoints' =&gt; [
        'assets.json' =&gt; [
            'paginate' =&gt; false,
            'elementType' =&gt; ElementType::Asset,
            'criteria' =&gt; [
                'limit' =&gt; null,
            ],
            'transformer' =&gt; function(AssetFileModel $asset) {
                // Set `generateTransformsBeforePageLoad` to `true` to have Craft generate transforms at runtime
                craft()-&gt;config-&gt;set('generateTransformsBeforePageLoad', true);
                return [
                    'title' =&gt; $asset-&gt;title,
                    'url' =&gt; $asset-&gt;url,
                    'thumb' =&gt; $asset-&gt;getUrl(['width' =&gt; 100, 'height' =&gt; 100]),
                ];
            },
       ],
    ]
];


One small gotcha: By default, Element API will paginate results. To avoid that, you can add a 'paginate' =&gt; false, parameter to your endpoint.

Another small gotcha: If you want to return transformed Assets (e.g. a thumbnail version), you can pass a named or inline (array) transform to $asset-&gt;getUrl(), but you'll need to set the generateTransformsBeforePageLoad config setting to true at runtime in order to have Craft actually generate the transforms when the endpoint is requested.

Finally: Depending on the number of Assets and Entries, you might see some performance issues with this method (especially if you're going to include transforms and don't paginate the results), so it'll probably be a good idea to cache the assets.json output in some way.