How to filter fields on API response

Is there a built in way to filter the fields provided by the API response of a collection? I’m using Nuxt.js to statically generate a site and it needs a list of the slugs in an “slug”: “this-is-a-slug” type format. Can I filter the API results to ONLY show that field that are built into the API?

I saw the fields option but it’s not really documented as to what it does.

If not, any tips on a solution?

1 Like

Hello the fields field you can find it here the documentation.
Link: Mongolite User Manual

But this won’t return you an array of slugs anyway.

If you want to have custom bees you have to create in the “config” folder an “api” folder with your custom files inside that return the data as you want.

Let’s say, your collection pages has two fields: title and slug.

If you fetch your data /api/collections/get/pages, you’ll receive

...
"entries": [{
  "title": "first entry",
  "slug": "first-slug",
  "_id": "qweasdyxc123"
}]

If you want only the slug field, send the fields filter

{
  "fields": {
    "slug": true,
    "_id": false
  }
}

and your response should look like this:

...
"entries": [{
  "slug": "first-slug",
}]
1 Like

Beautiful. I did it with just some JavaScript for the time being, but that makes my job much easier if I can do it with the initial request :slight_smile:

Is this possible to filter fields on get request ?

Sure. Just convert the filter into a query string:
https://domain.tld/api/collections/get/pages?fields[slug]=1&fields[_id]=0