<p>Something like the following should work:</p>

<pre><code>// 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]),
                ];
            },
       ],
    ]
];
</code></pre>

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

<p>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 <code>$asset-&gt;getUrl()</code>, but you'll need to set the <code>generateTransformsBeforePageLoad</code> config setting to <code>true</code> at runtime in order to have Craft actually generate the transforms when the endpoint is requested.</p>

<p>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 <a href="https://craftcms.stackexchange.com/a/13455/1098">cache the assets.json output in some way</a>.</p>

CC BY-SA 3.0
2016-07-11T16:21:57.927Z
Thanks! And can I link to assets.json in another file?
Jesse Winton
CC BY-SA 3.0
2016-07-11T16:27:46.540Z
Not sure I know what you mean by "link to in a file" - the URL to the Element API endpoint would be domain.com/assets.json, and it'll print a JSON object with all your Assets. For your purposes, you should probably cache the output to a static file, though.
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2016-07-11T18:06:42.657Z
For some reason, I get a 500 Internal Server error saying that assets.json doesn't exist.

Also, I realized this code is only getting assets in the `featuredImage` field. Is there a way to modify this to get all assets in the website?
Jesse Winton
CC BY-SA 3.0
2016-07-11T18:13:28.393Z
Just edited the answer; the example code will now pull _all_ Assets in your site; not just the ones related to certain entries. Don't know about the 500 error, sounds weird & the code works fine on my end. Did you put the `elementapi.php` file in your config folder, alongside `general.php`? Did you activate the plugin?
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2016-07-11T18:22:30.640Z
Perfect, thanks. How are you accessing the data on your end? I double checked that the plugin was activated and installed and `elementapi.php` is in my config folder alongside `general.php`. 

Also, what I meant by linking to the JSON file is essentially this: I want to be able to use `assets.json` as the load Manifest for the preloader that I've integrated, so that it will preload assets as they're used. That's done like so:

`queue.loadManifest('assets.json');`

I'm wondering, does linking to the assets file like this actually access the data?
Jesse Winton
CC BY-SA 3.0
2016-07-11T18:45:07.250Z
Element API doesn't create static files – only endpoints, meaning that there is no `assets.json` file, only an URL resource at http://yourwebsite.com/assets.json. Hence my suggestion that you should cache that resource to a static file, as calling the endpoint at every pageload will create a lot of unneccessary overhead.
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2016-07-11T18:47:23.557Z
Great, thanks for your help, this is a great start.
Jesse Winton
CC BY-SA 3.0
2016-07-11T18:48:44.703Z
As for Preload.js; I think it's able to load external manifests, but you might need to give it the full URL, i.e. `loadManifest('http://yourwebsite.com/assets.json')` or just `loadManifest('/assets.json')`. Another issue is whether the Element API payload adheres to the Preload.js manifest format – it probably doesn't, so you probably need to fetch it with AJAX (jQuery) first, loop through it and create a Preload.js compliant manifest, and finally load that.
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2016-07-11T11:05:16.377Z
2016-07-11T20:46:24.087Z
2017-04-13T12:50:59.930Z
Mats Mikkel Rummelhoff
<p>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 <a href="http://www.createjs.com/docs/preloadjs/classes/LoadQueue.html" rel="nofollow">PreloadJS</a>. If you read the docs here, you'll see that the images can be loaded from an external JSON file.</p>

<p>Here's what I've been working with so far:</p>

<pre><code>&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")
                ];
            },
        ]
    ]
];
</code></pre>

<p>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.</p>

<p>Thanks!</p>

CC BY-SA 3.0
2016-07-11T05:01:33.877Z
So have you tried something and it's not working? Can you share your `craft/config/elementapi.php` file? Are you trying to output _every_ asset on the site or just every asset that is in use?
Brad Bell
CC BY-SA 3.0
2016-07-11T10:03:07.840Z
Edited accordingly. Yes, I've been trying a couple of options, but nothing has worked so far, including ideas from these other answers below. Any help on this would be great. Thanks! http://craftcms.stackexchange.com/questions/15015/elementapi-can-i-use-javascript-ajax-to-load-json http://craftcms.stackexchange.com/questions/15015/elementapi-can-i-use-javascript-ajax-to-load-json
Jesse Winton
<plugin-elementapi><json>
Get Assets in JSON with ElementAPI
# Get Assets in JSON with ElementAPI

(Asked by: username_0 on 2016-07-10)

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:
 [
        'assets.json' => [
            'elementType' => 'Entry',
            'criteria' => ['section' => 'news'],
            'transformer' => function (EntryModel $entry) {
$images = [];
        foreach ($entry->featuredImage as $image) {
        $images[] = $image->url;
        }
                return [
                    'image' => $entry->image
                    'jsonUrl' => 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!

**Tags:** plugin-elementapi, json
> **Comments:**
> **username_1**: So have you tried something and it's not working? Can you share your `craft/config/elementapi.php` file? Are you trying to output _every_ asset on the site or just every asset that is in use?  
> **username_0**: Edited accordingly. Yes, I've been trying a couple of options, but nothing has worked so far, including ideas from these other answers below. Any help on this would be great. Thanks! http://craftcms.stackexchange.com/questions/15015/elementapi-can-i-use-javascript-ajax-to-load-json http://craftcms.stackexchange.com/questions/15015/elementapi-can-i-use-javascript-ajax-to-load-json  

---

## 1 answers

### Answer by username_2 on 2016-07-11. Score: 5 | Accepted answer

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

One small gotcha: By default, Element API will paginate results. To avoid that, you can add a 'paginate' => 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->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.

> **Comments:**
> **username_0**: Thanks! And can I link to assets.json in another file?  
> **username_2**: Not sure I know what you mean by "link to in a file" - the URL to the Element API endpoint would be domain.com/assets.json, and it'll print a JSON object with all your Assets. For your purposes, you should probably cache the output to a static file, though.  
> **username_0**: For some reason, I get a 500 Internal Server error saying that assets.json doesn't exist.  Also, I realized this code is only getting assets in the `featuredImage` field. Is there a way to modify this to get all assets in the website?  
> **username_2**: Just edited the answer; the example code will now pull _all_ Assets in your site; not just the ones related to certain entries. Don't know about the 500 error, sounds weird & the code works fine on my end. Did you put the `elementapi.php` file in your config folder, alongside `general.php`? Did you activate the plugin?  
> **username_0**: Perfect, thanks. How are you accessing the data on your end? I double checked that the plugin was activated and installed and `elementapi.php` is in my config folder alongside `general.php`.   Also, what I meant by linking to the JSON file is essentially this: I want to be able to use `assets.json` as the load Manifest for the preloader that I've integrated, so that it will preload assets as they're used. That's done like so:  `queue.loadManifest('assets.json');`  I'm wondering, does linking to the assets file like this actually access the data?  
> **username_2**: Element API doesn't create static files – only endpoints, meaning that there is no `assets.json` file, only an URL resource at http://yourwebsite.com/assets.json. Hence my suggestion that you should cache that resource to a static file, as calling the endpoint at every pageload will create a lot of unneccessary overhead.  
> **username_0**: Great, thanks for your help, this is a great start.  
> **username_2**: As for Preload.js; I think it's able to load external manifests, but you might need to give it the full URL, i.e. `loadManifest('http://yourwebsite.com/assets.json')` or just `loadManifest('/assets.json')`. Another issue is whether the Element API payload adheres to the Preload.js manifest format – it probably doesn't, so you probably need to fetch it with AJAX (jQuery) first, loop through it and create a Preload.js compliant manifest, and finally load that.  

---

