Create a custom API endpoint with everything but body content?

How would I create a custom API endpoint with everything but body_content?

Essentially, I want an index of all my posts – but I don’t want to query the body_content. (I’ll query that when I hit the actual page.)

I know I can create a custom API endpoint in my config folder – but I’m not sure of the correct syntax for this.

I have a custom endpoint that queries unpublished entries:

<?
return cockpit('collections')->find('posts', [ 'filter'=>array_merge($this->param('filter', []), ['published'=>false])]);
?>

Would removing the body from the response be similar to this? Essentially, I want to query:

postID, slug, title, and excerpt.

This should do the trick:

<?

return cockpit('collections')->find('posts', [ 
   'filter' => array_merge($this->param('filter', []), ['published'=>false]),
   'fields' => ['body_content' => 0] // get all fields except the body
]);

Greets,
Artur

1 Like

Awesome – thank you!

I changed the ‘-1’ to zero — and it worked perfectly. Thank you!!

Ahhh yes, sorry for that!

Sorry for my ignorance, but when I try this in version v2 it throws me an error, maybe it’s an import or something similar, but if someone could share a simple example of a CRUD in a custond end poind I would really appreciate it, I come from DIRETUS 8 a tool in php also very powerful but already outdated

Sorry for my ignorance, but when I try this in version v2 it throws me an error, maybe it's an import or something similar, but if someone could share a simple example of a CRUD in a custond end poind I would really appreciate it, I come from DIRETUS 8 a tool in php also very powerful but already outdated

In v2:

<?php

return $this->module('content')->items('posts', [ 
   'filter' => array_merge($this->param('filter', []), ['published'=>false]),
   'fields' => ['body_content' => 0] // get all fields except the body
]);
1 Like

I really appreciate it, something so simple saved me hours of reading the code, hopefully at some point I can return the favor

1 Like