Beyond Kirby : Headless CMS

Hello, I really would like to use Kirby as a headless CMS and put Next.js (or React.js) for front-end part.
I tried headless-getting-started, but I think I don’t get the point of “Set the user credentials under the Basic dropdown and set the query to POST using the local url to the headless system, for example: https://headless.test/api/query” at this article, so that I keep getting 404 error at “insomnia” application.:smiling_face_with_tear:

Do you know how to solve this problem?
And back to “Beyond Kirby”, I only can find “Vue starterkit”, not for Next.js, thus can I ask how to use Next.js with Kirby?
I’m struggling with this cuz there are a few information about this, so if someone knows, please let me know:)
\ Thank you /

You have to add your user credentials in the Basic tab (the one before Query), i.e. your username and password.

Re. Next.js, maybe this helps: Kirby as Headless CMS with Next JS | Medium (haven’t read it).

With the dev version of KQL, you can use it without authentication. Basic auth is not really intended for authentication via JavaScript.

Thank you for answering!
And you mean this? I tried many things but sadly I couldn’t find the solution yet. :sweat_smile:

And even though I’ve used the dev version of KQL, they require these kinda authentication keys.
'auth' => ['xxx', 'xxx'],
Should I put the same (temporary) user credentials like above ones on Insomnia?

Re: and about the article you suggested, I checked it before but I will check it once again.
Thanks!

This is the Query tab, where it says Basic, you have to enter a real username and password.

Oh how silly I was, now it works :sweat_smile:

And I have one more question about the connection between php and JS.
Following the above link, I used the “guzzle” instead of ‘axios’ like this code(below).
But the ‘query’ doesn’t accept arrays so I couldn’t get the other pages’ informations.
Do you know how can I get all pages datas and fetch this api to JS file?

on index.php

$response = $client->request('POST', $api, [
...
'json' => [
        'query' => 'page("home").children',
]
])

$body = $response->getBody();
$data = json_decode($body);
$posts = $data->result->data;

Hi,

I’ve got the same problem. I followed the documentation for Kirby headless (Going Headless | Kirby CMS) and when i created the new folder (for front), with Guzzle example code, I can’t get the same result as expected (“a simple list of the pages and content in the Photography pages”).
Instead I have the following message :


Warning: Attempt to read property "data" on array in /Users/***/Sites/***-front/index.php on line 36

Warning: foreach() argument must be of type array|object, null given in /Users/***/Sites/***-front/index.php on line 39

However, everything works when I do api tests in Insomnia.

Anyway, is there any specific documentation for using headless kirby for a React app? Right now it’s hard to set up for a Kirby newbie…

Thank you very much.


Here is the index.php code in mysite-front folder :

<?php

require __DIR__ . '/vendor/autoload.php';

use GuzzleHttp\Client;

$client = new GuzzleHttp\Client();

$api = 'http://mysite.test/api/query/';

// Create a POST request with Guzzle using KQL
$response = $client->request('POST', $api, [
    'verify' => false,
    'auth' => ['myid', 'mypassword'],
    'json' => [
        'query' => 'page("photography").children',

        'select' => [
            'url' => true,
            'title' => true,
            'text' => 'page.text.markdown',
            'images' => [
                'query' => 'page.images',
                'select' => [
                    'url' => true
                ],
            ],
        ],    
    ],

]);

// Handle the incoming data
$body = $response->getBody();
$data = json_decode($body);
$posts = $data->result->data;

// Loop over the result
foreach ($posts as $post) {
    echo '<h1>' . $post->title . '</h1>';
    echo $post->text;
    $images = $post->images;
    foreach ($images as $image) {
        echo  '<img width="150" src="' . $image->url . '"/>';
    }
}

Should be

$posts = $data->result;

@jimbobrjames Did your code ever work like described? Or is the error due to changes in KQL?

Wonderful. You’re right. Now I just have to think carefully to understand how to use all this with a js framework. Difficult task…And maybe a bad idea? (I really like implementing frontend with React)

I’d say that depends on your reasoning for implementing your front-end with React (or any other reactive JS framework).

I’m not aware of any React examples in the wild.

However, if you know React, fetching data from a headless Kirby should work in the same way as you would fetch data from any other API.

For a general headless setup, also check out: GitHub - johannschopplich/kirby-headless-starter: 🦭 Headless Kirby starter – KQL, Express-esque middlewares & more

Yes, i just ran my project from the cookbook guide and it works fine with:

$posts = $data->result->data;

The code worked at the time of going to press.

I cant seem to find a specific version in the KQL plugin used when I wrote the guide, but i do think a new version of the plugin has been published since.

You’re right! Thank you so much for your help.