Help me understand how to structure my API request to retrieve image urls?

I am working on a Headless Kirby CMS project and am testing my requests with Insomnia.

Relevant Information:

  • My Kirby installation uses just one site.yml blueprint file that contains all of my data fields. I am not using pages.
  • I am using the KQL plugin

Basically, I want to return my events_list field data which includes an image file (type: files max: 1). I want to specifically return the url, width, and height of the file.

This JSON body: { "query": "site.files", "select": [ "url", "width", "height ] }

returns all of my files’ urls, widths, and heights, since all of my images are stored in the main content folder (not pages). This is great, because it tells me it is possible to retrieve this information from the API.

and this JSON body: { "query": "site.events_list.toStructure", "select": [ "title", "ticket_link", "date", "image" ] }

returns all of my event data, except for the image it simply says “array”

I want my JSON request to return my title, ticket link, date, and image data (url, width, and height). How do I structure my request?

Someone on another forum suggested this structure:

{ "query": "site.events_list.toStructure", "select": { "title": true, "ticket_link": true, "date": true, "image": { "select": ["url", "width", "height"] } } }

but this returns an error:

{ "status": "error", "message": "The method \"Kirby\\Content\\Field::url()\" is not allowed in the API context", "code": 403, "key": "error.permission", "details": [] }

My API user has the role of admin, so I don’t think that is the issue. Any ideas?

All I want to do is render my images from Kirby on my front-end, but I am so lost.

Hi @pardeek. Welcome to the forum.

I think you need to further resolve the image field inside the structure item.

events_list:
  type: structure
  fields:
     title:
       type: text
     date:
       type: date
     image:
       type: files
       max: 1
{
  "query": "site.events_list.toStructure",
  "select": {
    "title": true,
    "ticket_link": true,
    "date": true,
    "image": {
      "query": "structureItem.image.toFile",
      "select": {
        "url",
        "width",
        "height"
      }
    }
  }
}

based on this issue in the KQL repo: how to select and transform data from structures · Issue #8 · getkirby/kql · GitHub