API Call from Mobile App - Result JSON

I’m searching for a backend for a mobile app. So I found KIRBY! I’ve watched a video how a site is set up. And I really liked the flexibility! I’ve downloaded KIRBY, and made it run on my machine.

But now I’m searching for a tutorial, how to read the data with an API call and get a JSON as a result.

I just found this:

This does not help me a lot!

Can anybody give me a link with how to get started with API calls?

Thank you!

A good starting point would be this page: Beyond Kirby | Kirby CMS

It shows you the different ways for a headless setup.

Usually, using the KQL plugin helps you with making queries, but KQL doesn’t support methods that change data, so it’s only for reading data.

Thank you for the very fast answer. I will have a look at your link right now. I do just want to read data so it should be perfect.

Thank you! Loading data works perfect with KQL.

But now let’s say I’ve a page. In the text file of the page I’ve defined a variable telling me this page has an icon.

As I know the URL of the page and also know the filename of the icon, I thought I could load the image directly. It works in the browser but the URL changes while loading.

http://localhost:8080/test_page/0_icon.png

Is it event possible to load the image directly from the URL in an app?

Another question, is it possible to use KIRBY’s thumb creator within an HTTP_REQUEST so I can use it in my app?

The field value doesn’t provide the url to the file, so you would have to either serve the files directly from the content folder instead of from the media folder, or make the connection between the field value and the images in the page (by querying the images of the page), see https://kql.getkirby.com/ (then add the filename to the images query).

Must admit that I’m not really familiar with KQL. And I think there will be improvements to image handling once KQL makes it into the Kirby core.

As regards thumbs, I created a simple example route here: Permalinks for resized images - #3 by texnixe

But as stated in the post, this approach either needs authentication or at least some security handling.

I’ve built in the route with the thumbserver from your other post. When I call the URL with a specific image in the browser like:

http://localhost:8080/thumbserver/jvwbXRXYkVBcrp21?width=400&height=400&crop=center&format=png

The thumb is generated but the image is not displayed and the Webserver crashes.

Could you please post the code you used?

<?php

return [
  // API 
  'kql' => [
    'auth' => false
  ],
  
  // debug
  'debug'  => true,

  // routes
  'routes' => [
      [
      'pattern' => 'portfolio',
      'action'  => function () {
        return page('home');
      }
    ],
    [
      'pattern' => 'thumbserver/(:any)',
      'action'  => function ($fileUuid) {

        $file = site()->index()->files()->findBy('uuid', 'file://' . $fileUuid);
        
        if ($file) {
          $width  = get('width') ?? 200;
          $height = get('height') ?? 200;
          $format = get('format') ?? 'jpg';
          $crop   = get('crop') ?? false;

          return Response::file($file->thumb([
                'width'  => $width,
                'height' => $height,
                'crop'   => $crop,
                'format' => $format,
              ]
            )->url());
        }
        return null;
      },
    ]
  ]
]
?>

What I meant is where and how you call this route url?

I just call the URL in the Browser! As described, the thumb is generated and can be found. BUT the local Server crashes and no picture shows up in the browser

The UUID is correct.

Ok, I forgot to include the use statement from the top of the file :see_no_evil:

use Kirby\Http\Response;

Without that, you have to call the FQN

Sorry for the maybe stupid question, were should I put the line of code?

Before the return statement at the top of the file

Thank you very much, I’ll try it tomorrow! I will for sure buy KIRBY tomorrow. GREAT SUPPORT!

1 Like

Sale is still running until Monday (July 10)…

1 Like

As promised I bought my license!

The THUMBSERVER works on my uberspace account but also with your include it does not work on my local machine. There is a change now it takes a long time the server is responsive again but it does not crash anymore.

Mac Book M1 - OS Ventura

php: 8.2
I’ve installed: ImageMagick

Are there any more dependencies?

You might need to set the bin location on your local config depending on how you installed imagemagick. For example, i use Valet as a local server and installed image magic via homebrew.

Since this installs IM to a nonstandard location that would be expected on a real websever, you need to set the bin option to tell Kirby were to find the IM binary. If you pull up a terminal and run the following command:

which convert

It will print the location of the IM binary, which for me is /opt/homebrew/bin/convert

then in your config, decalare this in the thumbs settings.

'thumbs' => [
  'driver' => 'im',
  'bin' => '/opt/homebrew/bin/convert'
],
1 Like

Only if you use the im thumbs driver, of course.

Thank you for your answer.

I’ve followed your instructions same issue. I also do not think it’s a matter of the location of IM because the thumbs are generated and can be found in the folder. They do just not open and the server crashes.

I did not manage to geht the FileInfo extension of php to install. Maybe this is the problem?

Regarding your local setup, what sort of development environment are you using (for the server, I mean, you already mentioned Mac OS)?