Creating Kirby pages via API POST Request

Hi there,

I’m looking to use the Kirby POST API for creating pages. I’m using postman to create a test request, using this endpoint from the Kirby docs

My request looks like this, note the url structure -

const myHeaders = new Headers();
myHeaders.append("Authorization", "••••••");
myHeaders.append("Cookie", "kirby_session=1a2d1f7f76f0adc333e7e92e1776999543008a76%2B1727347035.38c12c1532e7c8a29dae.e2f3741d3c800f33ce7caca7c6c4b59385754b42777985f39c88a479353c8746");

const requestOptions = {
  method: "POST",
  headers: myHeaders,
  redirect: "follow"
};

fetch("https://panel.noodsradio.com/api/pages/shows/children/2024/09/george-s-test-show-new", requestOptions)
  .then((response) => response.text())
  .then((result) => console.log(result))
  .catch((error) => console.error(error));

This is currently responding with a 404. As you can see have a custom folder structure using year and month subfolders to store show entries… is this compatible with the POST endpoint?

Any help on this would be much appreciated.

Thanks

The URL of your API endpoint doesn’t look right. Should be just

https://panel.noodsradio.com/api/pages/shows/children

If the page is shows that you want to add a new child to. You pass the slug for the new page as POST parameter, not in the API endpoint URL.

Thank for the speedy reply @distantnative !

I’ve changed my request as you advised and I’m now able to create a show to the panel.

My request body is as follows

{
    "slug": "2024+09+george-s-test-show-new",
    "template": "show",
    "content": {
        "title": "A new test show 2025"
    }
}

However, this only creates the show in the the root show directory, whereas I would like it be create in a sub folder of shows, in this case /shows/2024/09 i.e this month.

Could you advise how I can achieve this?

Do the subfolders already exist or would you need to create those children pages first?

I got this working by using the following URL structure*

https://exampledomain.com/api/pages/shows+2024+09/children

*this is specific to my content folder structure of course

Would anyone be able to provide further guidance on how to upload and assign files (images) to pages created in Kirby via the above POST request?

Currently I’m trying the following but I’m unable to determine the structure required for the request body

https://stagingpanel.noodsradio.com/api/pages/shows+2024+10+george-test-27th-september-24/files

How are you sending the request? From HTML/JavaScript or from PHP?

In general, the files/s need to end up in $_FILES and optionally a template in the body of the request.

I am attempting to send the request via a Javascript fetch request. Am I right to think that the page needs to be created first using a the above POST request, then the image uploaded, and then the page patched / field updated with the image that was uploaded?

If you could provide any guidance on this it would be much appreciated.

Thanks

Yes that’s correct, those would need to be individual API requests one after the other.

If you are using this in the context of the Panel, you could make use of https://lab.getkirby.com/public/lab/internals/helpers/upload for sending the request for the file.