API related questions

I need your help to address some questions I have, regarding the tools that October CMS offers to build API endpoints.

Let’s say I have this basic setup:

A content type “BlogPost” defined via Tailor blueprint:

uuid: 0195da17-262d-425a-997e-843243dd25d4
handle: Content\BlogPost
type: stream
name: Blog Post
drafts: true

primaryNavigation:
    label: Content
    icon: octo-icon-file
    order: 100

navigation:
    icon: icon-bookmark
    parent: Content\BlogPost
    order: 10

fields:

    basic_data_fields:
        tab: Meta Data
        type: mixin
        source: Content\BasicData
    author:
        tab: Meta Data
        label: Author or Speaker
        type: entries
        maxItems: 1
        source: Content\Author
    main_image:
        tab: Meta Data
        label: Main image
        type: fileupload
        mode: image
        maxFiles: 1
    categories:
        tab: Meta Data
        label: Categories
        commentAbove: 'Enter the categories this post belongs to'
        type: entries
        source: Content\Category    
    content:
        tab: Content
        label: Full text
        type: richeditor
        span: adaptive

This content type references “Category” content type:


uuid: 482b0668-d7d2-41cf-87cb-9b96cae75548
handle: Content\Category
type: structure
name: Category
drafts: false

structure:
    maxDepth: 3

navigation:
    icon: icon-tree
    parent: Content\BlogPost
    order: 5

fields:
    description:
        label: Description

And finally, there is a page in the active template, to define the endpoint:

title = "Blog Posts"

layout = "default"

url = "/api-v1/blogposts/all/:orderby?id/:order?asc/:ipp?100"

[collection]

handle = "Content\BlogPost"

==

{% set posts = collection.orderBy(this.param.orderby, this.param.order).paginate(this.param.ipp) %}

{% do response({

data: posts

}) %}

Given the above setup, these questions arise:

  1. Why some fields are missing in the response?

For example, a GET request like:

http://localhost/api-v1/blogposts/all

It returns a response that does not contain the categories each post belongs to. Also, some other fields are missing, like “main_image”. The absence of fields seems to be random, because in other content types I have defined the same fileupload field, and the corresponding API response does contain the image file path.

  1. How can I control/limit the fields that are included in the response? For instance, how can I return the “id” and the “status” fields only?

  2. I notice that there are several fields that are automatically created for each Tailor content type (site_id, slug, is_enabled, etc.) Is there some way to modify/customize these internal fields?

Your contributions to answer at least one of these questions will be greatly appreciated.

Best regards.

You can use the .with('categories') to eager load the categories.

Thanks. It works fine.

I already found the answers for my own questions. Most of them are in the documentation itself. Looking at the examples was helpful too.
Consider this solved.