How to return a hierarchical list of a site's page structure using the ElementAPI
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:

&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
                    ];
                },
            ];
        }
    ]
];