# How to filter fields on API response

**URL:** <https://discourse.getcockpit.com/t/how-to-filter-fields-on-api-response/1312>\
**Category:** Support\
**Created:** [April 2, 2020, 1:00am UTC](https://discourse.getcockpit.com/t/how-to-filter-fields-on-api-response/1312 "2020-04-02T01:00:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![patrickblackjr](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/patrickblackjr/32/548_2.png) [@patrickblackjr](https://discourse.getcockpit.com/u/patrickblackjr)\
**Post date:** [April 2, 2020, 1:00am UTC](https://discourse.getcockpit.com/t/how-to-filter-fields-on-api-response/1312/1 "2020-04-02T01:00:28Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![alessandro92](https://avatars.discourse-cdn.com/v4/letter/a/0ea827/32.png) [@alessandro92](https://discourse.getcockpit.com/u/alessandro92)\
**Post date:** [April 2, 2020, 1:07pm UTC](https://discourse.getcockpit.com/t/how-to-filter-fields-on-api-response/1312/2 "2020-04-02T13:07:27Z")

</div>

Hello the fields field you can find it here the documentation.  
Link: [Mongolite User Manual](https://jeroen.github.io/mongolite/query-data.html#filter-fields)

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.

> [@How to create custom API Endpoints](https://discourse.getcockpit.com/t/how-to-create-custom-api-endpoints/202/8):
>
> as always, it depends wink if you want something re-usable then I would also recommend to do an addon, but if it is something project specific, then custom api entry points are a solution to solve things or provide specific data quickly

---

<div class="post-metadata">

**Author:** ![raffaelj](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/raffaelj/32/331_2.png) [@raffaelj](https://discourse.getcockpit.com/u/raffaelj)\
**Post date:** [April 2, 2020, 4:34pm UTC](https://discourse.getcockpit.com/t/how-to-filter-fields-on-api-response/1312/3 "2020-04-02T16:34:50Z")

</div>

> [@patrickblackjr](#):
>
> I saw the `fields` option but it’s not really documented as to what it does.

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

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

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

```

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

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

```

and your response should look like this:

```auto
...
"entries": [{
  "slug": "first-slug",
}]

```

---

<div class="post-metadata">

**Author:** ![patrickblackjr](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/patrickblackjr/32/548_2.png) [@patrickblackjr](https://discourse.getcockpit.com/u/patrickblackjr)\
**Post date:** [April 4, 2020, 5:11am UTC](https://discourse.getcockpit.com/t/how-to-filter-fields-on-api-response/1312/4 "2020-04-04T05:11:42Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![Koalhack](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/koalhack/32/1102_2.png) [@Koalhack](https://discourse.getcockpit.com/u/Koalhack)\
**Post date:** [June 9, 2022, 1:21pm UTC](https://discourse.getcockpit.com/t/how-to-filter-fields-on-api-response/1312/5 "2022-06-09T13:21:28Z")

</div>

Is this possible to filter fields on get request ?

---

<div class="post-metadata">

**Author:** ![raffaelj](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/raffaelj/32/331_2.png) [@raffaelj](https://discourse.getcockpit.com/u/raffaelj)\
**Post date:** [June 14, 2022, 2:59pm UTC](https://discourse.getcockpit.com/t/how-to-filter-fields-on-api-response/1312/6 "2022-06-14T14:59:09Z")

</div>

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