Return full object in response


I want to use kirby as a headless CMS and therefore I am only using the /api endpoint.

When I open this endpoint in the starterkit: http://localhost:8000/api/pages/photography/children I get a list of all the children. However, this response only contains the ids and URLs of the children and I have to query each child again to get the full object.

To get all entries with full data I have now added a custom endpoint with the following code:

'api' => [
        'routes' => [
            'pattern' => 'my-endpoint',
            'action'  => function () {
              $pages = page('photography')
                      ->sortBy('date', 'desc');
               $ret = array();
               foreach($pages as $item) {
                    array_push($ret, $item->content()->data());
               return new Response(json_encode($ret), 'application/json');

This is working, but the cover value inside each entry is returned as a single filename instead of the full cover object.
Currently returned: "cover": "- indiana-jones.jpg"

What I need:

cover: [{filename: "indiana-jones.jpg", dragText: "(image: indiana-jones.jpg)",…}]
0: {filename: "indiana-jones.jpg", dragText: "(image: indiana-jones.jpg)",…}
dragText: "(image: indiana-jones.jpg)"
filename: "indiana-jones.jpg"
icon: {type: "file-image", ratio: "3/2", back: "pattern", color: "#de935f", cover: false,…}
id: "photography/desert/indiana-jones.jpg"
image: {ratio: "3/2", back: "pattern", cover: false,…}
info: ""
link: "/pages/photography+desert/files/indiana-jones.jpg"
text: "indiana-jones.jpg"
type: "image"
url: "http://localhost:8000/media/pages/photography/desert/f3c7ca0d0d-1630926839/indiana-jones.jpg"

Is there any (easy) way to accomplish this? Or is there a URL param which tells the API to return the full objects when I query http://localhost:8000/api/pages/notes/children ? I know that the queries can get slow when I query all the data, but for my project that does not matter.

Thanks for any help!

Welcome to the forum. I would suggest looking into KQL which give you greater options on getting specific things when working headless.

1 Like

But I think even with KQL you have this problem with images?

1 Like

Thanks, with KQL I get the full URL & it is much easier than creating an own endpoint!