<p>This should work for the images and you can easily adapt to do your categories &amp; kachelgroesse if needed (not tested)...</p>

<pre><code>'api/looks/list' =&gt; [
    'elementType' =&gt; 'Entry',
    'criteria' =&gt; ['section' =&gt; 'looks'],
    'transformer' =&gt; function(EntryModel $entry) {
        $images = [];
        foreach ($entry-&gt;featuredImage as $image) {
        $images[] = $image-&gt;url;
        }
        return [
        'title' =&gt; $entry-&gt;title,
        'id' =&gt; $entry-&gt;id,
        'url' =&gt; $entry-&gt;url,
        'jsonUrl' =&gt; UrlHelper::getUrl("looks/{$entry-&gt;id}.json"),
        'kachelgroesse' =&gt; $entry-&gt;kachelgroesse,
        'category' =&gt; $entry-&gt;category,
        'image' =&gt; $images
        ];
    },
],
</code></pre>

CC BY-SA 3.0
2015-11-26T18:44:12.410Z
this is example from element api docs at github... somehow doesnt work for me, i wonder why... :<
finally i just dump whole json on channel endpoint with some other plugin.
i would really like to filter the json though.
goran
CC BY-SA 3.0
2015-11-26T18:46:16.650Z
also my kachelgroesse is fine btw...even though nested returns all data.
goran
CC BY-SA 3.0
2015-11-27T09:51:57.183Z
At his point, if you want to "filter" things, you have 2 options. Create another endpoint from your API with the the correct criteria before the json is created so you get only the data you need OR whatever you are building on top of your API needs the functionality of filtering json payloads.
Oli
CC BY-SA 3.0
2015-12-03T12:19:06.610Z
what i do now is create endpoint like this:

`{% header "Content-Type: application/json" %}
{% set looks = craft.entries.section('looks').find('') %}
{{ looks | json_expand | raw  }}`
in the index.html of the channel template folder, but it returns all json data.
Where exactly can i filter it before data arrives there?
goran
CC BY-SA 3.0
2015-12-04T17:03:28.117Z
I'm sorry, I have not used json_expand... I'd recommend troubleshooting elementAPI still. It's so simple that you must be making a simple mistake somewhere for it not to work. Anyways, I'm a bit confused though, what are using your API for? When you say filter, are you talking about defining the fields for your payload or filtering which entries you are seeing returned?
Oli
CC BY-SA 3.0
2015-12-07T14:00:35.217Z
we just use it for an app, the client really wanted that cms... 
so there is just pure entries without any website attached to them.
with "filter" i mean reduce the output of json to the data we need,
now it just dumps everything, i tried with prune but same thing there...nested objects in arrays etc dont show up
goran
CC BY-SA 3.0
2015-12-07T14:45:25.453Z
I see, well, Like I said, I'd troubleshoot elementAPI if I was you as it does just what you want and very easily. I don't know json_expand so I can't help any further
Oli
CC BY-SA 3.0
2018-01-24T15:09:38.950Z
Fantastic code here @OlivierBon
Filth
CC BY-SA 3.0
2015-11-12T15:05:50.470Z
2017-06-16T17:43:18.587Z
2017-06-16T17:43:18.587Z
Oli
<p>A similar question regarding images was answered in this ticket I believe: <a href="https://craftcms.stackexchange.com/a/13236/4433">https://craftcms.stackexchange.com/a/13236/4433</a></p>

CC BY-SA 3.0
2017-03-06T02:36:01.773Z
2017-03-06T02:36:01.773Z
2017-04-13T12:51:00.160Z
Ryan
<p>You can do something like this:</p>
<pre><code>'transformer' =&gt; function(craft\elements\Entry $entry) {
        $image = $entry-&gt;photos-&gt;one();

    return [
        'image' =&gt; $image ? $image-&gt;url : null,
    ];
},
</code></pre>
<p>More info <a href="https://github.com/craftcms/element-api" rel="nofollow noreferrer">here.</a></p>

CC BY-SA 4.0
2021-03-01T17:38:47.100Z
2021-03-01T17:38:47.100Z
Alexis Dalai Waldo Jiménez
<pre><code>'api/looks/list' =&gt; [
        'criteria' =&gt; ['section' =&gt; 'looks'],
        'transformer' =&gt; function(EntryModel $entry) {

                //print_r($entry-&gt;getAncestors());
                // $model = $entry-&gt;content-&gt;attributeConfigs["featuredImage"]["model"];
                // echo 'class type: ' . get_class($model);
                // print_r($model);

            return [
                'title' =&gt; $entry-&gt;title,
                'id' =&gt; $entry-&gt;id,
                'url' =&gt; $entry-&gt;url,
                'jsonUrl' =&gt; UrlHelper::getUrl("looks/{$entry-&gt;id}.json"),
                'kachelgroesse' =&gt; $entry-&gt;kachelgroesse,
                'category' =&gt; $entry-&gt;category,
                'image' =&gt; $entry-&gt;image
            ];
        },
    ],
</code></pre>

<p>This is my <code>elementapi.php</code> file and it returns:</p>

<pre><code>{
  "data": [
    {
      "title": "SHINY AND RED LIPS",
      "id": "281",
      "url": "http://craft.dev:8888/looks/shiny-and-red-lips",
      "jsonUrl": "http://craft.dev:8888/looks/281.json",
      "kachelgroesse": {
        "0": {
          "label": "mittel",
          "value": "mittel",
          "selected": true
        }
      },
      "category": {
        "0": {
          "label": "Make-Up",
          "value": "Make-Up",
          "selected": true
        }
      },
      "image": {}
    }
  ],
}
</code></pre>

<p>There is no image returning.  I am requesting the list of all entries and their image/asset location from the API.</p>

<assets><plugin-elementapi>
How can I get image locations in a list of entries using the Element API plugin?
# How can I get image locations in a list of entries using the Element API plugin?

(Asked by: username_0 on 2015-11-09)

'api/looks/list' => [
        'criteria' => ['section' => 'looks'],
        'transformer' => function(EntryModel $entry) {

                //print_r($entry->getAncestors());
                // $model = $entry->content->attributeConfigs["featuredImage"]["model"];
                // echo 'class type: ' . get_class($model);
                // print_r($model);

            return [
                'title' => $entry->title,
                'id' => $entry->id,
                'url' => $entry->url,
                'jsonUrl' => UrlHelper::getUrl("looks/{$entry->id}.json"),
                'kachelgroesse' => $entry->kachelgroesse,
                'category' => $entry->category,
                'image' => $entry->image
            ];
        },
    ],

This is my elementapi.php file and it returns:
{
  "data": [
    {
      "title": "SHINY AND RED LIPS",
      "id": "281",
      "url": "http://craft.dev:8888/looks/shiny-and-red-lips",
      "jsonUrl": "http://craft.dev:8888/looks/281.json",
      "kachelgroesse": {
        "0": {
          "label": "mittel",
          "value": "mittel",
          "selected": true
        }
      },
      "category": {
        "0": {
          "label": "Make-Up",
          "value": "Make-Up",
          "selected": true
        }
      },
      "image": {}
    }
  ],
}

There is no image returning.  I am requesting the list of all entries and their image/asset location from the API.

**Tags:** assets, plugin-elementapi


---

## 3 answers

### Answer by username_1 on 2015-11-12. Score: 3

This should work for the images and you can easily adapt to do your categories & kachelgroesse if needed (not tested)...
'api/looks/list' => [
    'elementType' => 'Entry',
    'criteria' => ['section' => 'looks'],
    'transformer' => function(EntryModel $entry) {
        $images = [];
        foreach ($entry->featuredImage as $image) {
        $images[] = $image->url;
        }
        return [
        'title' => $entry->title,
        'id' => $entry->id,
        'url' => $entry->url,
        'jsonUrl' => UrlHelper::getUrl("looks/{$entry->id}.json"),
        'kachelgroesse' => $entry->kachelgroesse,
        'category' => $entry->category,
        'image' => $images
        ];
    },
],

> **Comments:**
> **username_0**: this is example from element api docs at github... somehow doesnt work for me, i wonder why... :< finally i just dump whole json on channel endpoint with some other plugin. i would really like to filter the json though.  
> **username_0**: also my kachelgroesse is fine btw...even though nested returns all data.  
> **username_1**: At his point, if you want to "filter" things, you have 2 options. Create another endpoint from your API with the the correct criteria before the json is created so you get only the data you need OR whatever you are building on top of your API needs the functionality of filtering json payloads.  
> **username_0**: what i do now is create endpoint like this:  `{% header "Content-Type: application/json" %} {% set looks = craft.entries.section('looks').find('') %} {{ looks | json_expand | raw  }}` in the index.html of the channel template folder, but it returns all json data. Where exactly can i filter it before data arrives there?  
> **username_1**: I'm sorry, I have not used json_expand... I'd recommend troubleshooting elementAPI still. It's so simple that you must be making a simple mistake somewhere for it not to work. Anyways, I'm a bit confused though, what are using your API for? When you say filter, are you talking about defining the fields for your payload or filtering which entries you are seeing returned?  
> **username_0**: we just use it for an app, the client really wanted that cms...  so there is just pure entries without any website attached to them. with "filter" i mean reduce the output of json to the data we need, now it just dumps everything, i tried with prune but same thing there...nested objects in arrays etc dont show up  
> **username_1**: I see, well, Like I said, I'd troubleshoot elementAPI if I was you as it does just what you want and very easily. I don't know json_expand so I can't help any further  
> **username_2**: Fantastic code here @username_1vierBon  

---

### Answer by username_3 on 2017-03-06. Score: 1

A similar question regarding images was answered in this ticket I believe: <https://craftcms.stackexchange.com/a/13236/4433>


---

### Answer by username_4 on 2021-03-01. Score: 0

You can do something like this:
'transformer' => function(craft\elements\Entry $entry) {
        $image = $entry->photos->one();

    return [
        'image' => $image ? $image->url : null,
    ];
},

More info here.


---

