Multidomain setup API endpoints

Hello :slight_smile:

I am using a multidomain setup, and ideally trying to grab site content from every main domain and display it on a certain template.

Template controller

<?php

return function ($site) {

    $req = Remote::get('https://other-main-domain-url/api/kirby-users');

    return [
        'controllerData' => $req,
    ];
};

Custom plugin folder and its index.php file:

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

Php template file:

<?php
        dump($controllerData);
    ?>

It returns:
Server Error 401 - Authorization Required

This server could not verify that you are authorized to access the document requested. Either you supplied the wrong credentials (e.g., bad password), or your browser doesn’t understand how to supply the credentials required.

I tried enabling the allowInsecure property, also tried doing it via config file directly. Couldn’t find in the docs where exactly I am missing an authorisation part where this api call can be allowed, maybe you can help?

Thanks!

If you use API endpoints, you need to authenticate. That is just as true for custom endpoints as the default endpoints.

See example here: Authentication | Kirby CMS

If you use a normal route (not within the api array), you would not run into this.

I did check these tutorials, but for example, this email and password authentication - which email and password am I supposed to pass there - the ones used for panel login account? I am so lost in this authentication process, and maybe it’s just me, but I feel like the docs is not fully helping out, sorry :slight_smile:

Basically I would ideally like to, on other domains on a certain template, fetch the children pages of the main domain template (same template). For example, I have the main domain with a page news-overview and a few news articles as its children, and I want on another domain when I go to the news-overview page on that domain to fetch the children from the main domain and list them there.

Thanks again!

Yes, exactly, you need to use the credentials of an existing user, password and user email.

Here is an example of how to do it in a REST client: Beyond Kirby : Headless CMS - #4 by texnixe

And it’s here in the docs how to do it: Authentication | Kirby CMS

Please let me know what is not clear about this example which I already linked to above?