<p>It's not the most economical way to do it but if you don't have hundreds and hundreds of entries, this should do it for you:</p>

<pre class="lang-php prettyprint-override"><code>&lt;?php
use craft\elements\Entry;

function getChildren($entries){
    foreach ($entries as $entry) {
        $items[] = [
            'title' =&gt; $entry-&gt;title,
            'slug' =&gt; $entry-&gt;slug,
            'level' =&gt; $entry-&gt;level,
            'children' =&gt; $entry-&gt;hasDescendants ? getChildren($entry-&gt;children) : false
        ];
    }
    return $items;
}

return [
    'endpoints' =&gt; [
        'structure.json' =&gt; function() {
            return [
                'elementType' =&gt; Entry::class,
                'criteria' =&gt; [
                    'section' =&gt; 'structure',
                    'orderBy' =&gt; 'lft asc',
                    'level' =&gt; 1,
                    'limit' =&gt; null
                ],
                'transformer' =&gt; function(Entry $entry) {
                    return [
                        'title' =&gt; $entry-&gt;title,
                        'slug' =&gt; $entry-&gt;slug,
                        'level' =&gt; $entry-&gt;level,
                        'children' =&gt; $entry-&gt;hasDescendants ? getChildren($entry-&gt;children) : false
                    ];
                },
            ];
        }
    ]
];

</code></pre>

CC BY-SA 4.0
2019-11-27T09:12:41.403Z
Oli - you're a legend! Thank you so much for helping with this. Apologies for not getting back to you sooner.
Jim Pannell
CC BY-SA 4.0
2019-10-14T15:52:02.117Z
2019-10-14T15:52:02.117Z
Oli
<p>I'm experimenting with using Craft in a headless way and am successfully using the Element API to return all the data required to show individual pages.</p>

<p>However, to be able to show sub navigation and where a particular page fits into my site's 'structure' hierachy, I'd like to pull out the entire page hierarchy and then use that info in my application.</p>

<p>Does anyone know how to go about that using the Element API?</p>

<p>Thanks in advance for any clues.</p>

<p>Cheers,</p>

<p>Jim</p>

<craft3><plugin-elementapi>
How to return a hierarchical list of a site's page structure using the ElementAPI
# How to return a hierarchical list of a site's page structure using the ElementAPI

(Asked by: username_0 on 2019-10-10)

I'm experimenting with using Craft in a headless way and am successfully using the Element API to return all the data required to show individual pages.

However, to be able to show sub navigation and where a particular page fits into my site's 'structure' hierachy, I'd like to pull out the entire page hierarchy and then use that info in my application.

Does anyone know how to go about that using the Element API?

Thanks in advance for any clues.

Cheers,

Jim

**Tags:** craft3, plugin-elementapi

---

## 1 answers

### Answer by username_1 on 2019-10-14. Score: 2 | Accepted answer

It's not the most economical way to do it but if you don't have hundreds and hundreds of entries, this should do it for you:
 $entry->title,
            'slug' => $entry->slug,
            'level' => $entry->level,
            'children' => $entry->hasDescendants ? getChildren($entry->children) : false
        ];
    }
    return $items;
}

return [
    'endpoints' => [
        'structure.json' => function() {
            return [
                'elementType' => Entry::class,
                'criteria' => [
                    'section' => 'structure',
                    'orderBy' => 'lft asc',
                    'level' => 1,
                    'limit' => null
                ],
                'transformer' => function(Entry $entry) {
                    return [
                        'title' => $entry->title,
                        'slug' => $entry->slug,
                        'level' => $entry->level,
                        'children' => $entry->hasDescendants ? getChildren($entry->children) : false
                    ];
                },
            ];
        }
    ]
];

> **Comments:**
> **username_0**: Oli - you're a legend! Thank you so much for helping with this. Apologies for not getting back to you sooner.  

---

