Custom API endpoint with access to $kirby

Hi,

I’m fairly new to kirby and am struggeling with extending the api.

I followed the docs at https://getkirby.com/docs/reference/plugins/extensions/api and get the api route to work if it’s not a callback. Once I copy over the code from the second example (with access to $kirby) the endpoint at /api/my-endpoint returns a 404.

Should I take care of additional settings when using the callbacks option?

Is there a different way (other than the callbacks) of accessing the site and or pages within a custom api route (or data provider) ?

(My local dev setup is the php built in server startet up with router.php, if that makes a difference)

Thanks!

Turns out the example in the docs is not correct, this works for me:

<?php 
Kirby::plugin('my/api', [
    'api' => [
        'routes' => function($kirby) {
            return [
                [
                  'pattern' => 'my-endpoint',
                  'action'  => function () use ($kirby) {
                    return [
                      'users' => $kirby->users()->count()
                    ];
                  }
                ]
            ];
        }
    ]

]);
1 Like

works for me, too!

thanks for the quick solution!

1 Like