API to return only certain fields depending on query

I have found various ways to have the cockpit GET api return only certain fields in a collection, but these ways seem to apply globally, essentially hiding certain fields of the collection (or even all collections). But we want to have different calls return different combinations of fields, for the given collection. The goal is to minimize the amount of (contextually irrelevant) data sent over the wire, so we do not want to place the filtering in the browser or have it affect all GETs from the collection.

We query Cockpit from Nuxtjs using axios with API calls like
axios.get(${env.cockpit.apiUrl}/collections/get/ourcollection?token=${env.cockpit.apiToken}&simple=1&filter[priority]=critical);

Is there any way to use a parameter on the api url that would help us restrict which fields are returned?

Thanks!
Alan

1 Like

Just use the fields filter:

/api/collections/get/ourcollection?token=xx&simple=1&filter[priority]=critical&fields[critical]=1&fields[title]=1

Now your entries should contain critical, title and _id. If you want to omit _id, you have to add &fields[_id]=0.

If you use a positive projection fields[title]=1, only specified fields and _id are returned.
If you use a negative projection fields[title]=0, all fields except title are returned.

Be aware, that filtering on an excluded field doesn’t work.

2 Likes

Thanks. That helped immensely.
A