# Filtering API by category

**URL:** https://farmos.discourse.group/t/filtering-api-by-category/1405
**Category:** Development
**Tags:** support-request
**Created:** [November 12, 2022, 4:05pm UTC](https://farmos.discourse.group/t/filtering-api-by-category/1405 "2022-11-12T16:05:44Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![pat](https://yyz2.discourse-cdn.com/free1/user_avatar/farmos.discourse.group/pat/32/1050_2.png) [@pat](https://farmos.discourse.group/u/pat)
#### Post date: [November 12, 2022, 4:05pm UTC](https://farmos.discourse.group/t/filtering-api-by-category/1405/1 "2022-11-12T16:05:44Z")

</div>

How do I, if possible, filter my activity logs by category?

![bilde](https://global.discourse-cdn.com/free1/uploads/farm_os/original/1X/6f34fb7cdb15cc16a5176cf9d1751dc55408f152.png)

log/activity?filter[category]=71  
71 is my drupal inernal id for the category

---

<div class="post-metadata">

### Author: ![mstenta](https://yyz2.discourse-cdn.com/free1/user_avatar/farmos.discourse.group/mstenta/32/4_2.png) [@mstenta](https://farmos.discourse.group/u/mstenta)
#### Post date: [November 12, 2022, 4:26pm UTC](https://farmos.discourse.group/t/filtering-api-by-category/1405/2 "2022-11-12T16:26:53Z")

</div>

You can use: `/api/log/activity?filter[category.name]=Animals` to filter by the name of the category.

---

<div class="post-metadata">

### Author: ![mstenta](https://yyz2.discourse-cdn.com/free1/user_avatar/farmos.discourse.group/mstenta/32/4_2.png) [@mstenta](https://farmos.discourse.group/u/mstenta)
#### Post date: [November 12, 2022, 4:28pm UTC](https://farmos.discourse.group/t/filtering-api-by-category/1405/3 "2022-11-12T16:28:24Z")

</div>

This has a lot of examples: [Filtering | JSON:API module | Drupal Wiki guide on Drupal.org](https://www.drupal.org/docs/core-modules-and-themes/core-modules/jsonapi-module/filtering)

---

<div class="post-metadata">

### Author: ![pat](https://yyz2.discourse-cdn.com/free1/user_avatar/farmos.discourse.group/pat/32/1050_2.png) [@pat](https://farmos.discourse.group/u/pat)
#### Post date: [November 12, 2022, 4:45pm UTC](https://farmos.discourse.group/t/filtering-api-by-category/1405/4 "2022-11-12T16:45:32Z")

</div>

Perfect. Thanks

---

<div class="post-metadata">

### Author: ![paul121](https://avatars.discourse-cdn.com/v4/letter/p/54ee81/32.png) [@paul121](https://farmos.discourse.group/u/paul121)
#### Post date: [November 12, 2022, 5:12pm UTC](https://farmos.discourse.group/t/filtering-api-by-category/1405/5 "2022-11-12T17:12:37Z")

</div>

`filter[category.drupal_internal__id]=71` if you have the ID!

---

<div class="post-metadata">

### Author: ![pat](https://yyz2.discourse-cdn.com/free1/user_avatar/farmos.discourse.group/pat/32/1050_2.png) [@pat](https://farmos.discourse.group/u/pat)
#### Post date: [November 12, 2022, 9:02pm UTC](https://farmos.discourse.group/t/filtering-api-by-category/1405/6 "2022-11-12T21:02:00Z")

</div>

> [@paul121](#):
>
> `filter[category.drupal_internal__id]=71`

I knew there was a way for that too… 🙂

---

<div class="post-metadata">

### Author: ![pat](https://yyz2.discourse-cdn.com/free1/user_avatar/farmos.discourse.group/pat/32/1050_2.png) [@pat](https://farmos.discourse.group/u/pat)
#### Post date: [December 31, 2022, 9:19am UTC](https://farmos.discourse.group/t/filtering-api-by-category/1405/7 "2022-12-31T09:19:59Z")

</div>

> [@mstenta](#):
>
> This has a lot of examples: [Filtering | JSON:API module | Drupal Wiki guide on Drupal.org](https://www.drupal.org/docs/core-modules-and-themes/core-modules/jsonapi-module/filtering)

I’ve been trying to learn more about filters, but I seem to miss something.

I would say this is correct filter for between dates, but all I get is **“Allowed conjunctions: AND, OR.”**

```auto
msg.payload =
    "filter[filtergroup][group][conjuction]=AND"+

    "filter[today][condition][path]=timestamp&" +
    "filter[today][condition][operator]=%3E&"+
    "filter[today][condition][value]=" + unixTimestamp + "&"+
    "filter[today][condition][memberof]=filtergroup&"+

    "filter[tomorrow][condition][path]=timestamp&" +
    "filter[tomorrow][condition][operator]=%3C&" +
    "filter[tomorrow][condition][value]=" + (unixTimestamp+84000) + "&"+
    "filter[tomorrow][condition][memberof]=filtergroup";

```

---

<div class="post-metadata">

### Author: ![mstenta](https://yyz2.discourse-cdn.com/free1/user_avatar/farmos.discourse.group/mstenta/32/4_2.png) [@mstenta](https://farmos.discourse.group/u/mstenta)
#### Post date: [January 3, 2023, 7:34pm UTC](https://farmos.discourse.group/t/filtering-api-by-category/1405/8 "2023-01-03T19:34:14Z")

</div>

Hmm the only little thing I notice is that your example uses `memberof` but the [Drupal.org](http://Drupal.org) example uses `memberOf` (capital “O”). Does that make a difference?

---

<div class="post-metadata">

### Author: ![pat](https://yyz2.discourse-cdn.com/free1/user_avatar/farmos.discourse.group/pat/32/1050_2.png) [@pat](https://farmos.discourse.group/u/pat)
#### Post date: [January 3, 2023, 10:50pm UTC](https://farmos.discourse.group/t/filtering-api-by-category/1405/9 "2023-01-03T22:50:22Z")

</div>

> [@mstenta](#):
>
> (capital “O”). Does that make a difference?

Nah.. Then I get **“You must provide a valid filter condition. Check that you have set the required keys for your filter.”** (Error 500)

---

<div class="post-metadata">

### Author: ![mstenta](https://yyz2.discourse-cdn.com/free1/user_avatar/farmos.discourse.group/mstenta/32/4_2.png) [@mstenta](https://farmos.discourse.group/u/mstenta)
#### Post date: [January 4, 2023, 12:37pm UTC](https://farmos.discourse.group/t/filtering-api-by-category/1405/10 "2023-01-04T12:37:25Z")

</div>

Have you tried testing these filters in the browser address bar, or only in Node Red? It may be worth getting it working in the browser as a baseline first, just to be sure there isn’t anything weird going on (formatting, escaping, or otherwise). I don’t expect there would be, but probably better to start simple.

---

<div class="post-metadata">

### Author: ![mstenta](https://yyz2.discourse-cdn.com/free1/user_avatar/farmos.discourse.group/mstenta/32/4_2.png) [@mstenta](https://farmos.discourse.group/u/mstenta)
#### Post date: [January 4, 2023, 12:38pm UTC](https://farmos.discourse.group/t/filtering-api-by-category/1405/11 "2023-01-04T12:38:22Z")

</div>

Oh wait are you missing an `&` after the `AND`?

---

<div class="post-metadata">

### Author: ![mstenta](https://yyz2.discourse-cdn.com/free1/user_avatar/farmos.discourse.group/mstenta/32/4_2.png) [@mstenta](https://farmos.discourse.group/u/mstenta)
#### Post date: [January 4, 2023, 12:44pm UTC](https://farmos.discourse.group/t/filtering-api-by-category/1405/12 "2023-01-04T12:44:17Z")

</div>

AH! I found the issue… you spelled `conjunction` wrong. 🙂

---

<div class="post-metadata">

### Author: ![mstenta](https://yyz2.discourse-cdn.com/free1/user_avatar/farmos.discourse.group/mstenta/32/4_2.png) [@mstenta](https://farmos.discourse.group/u/mstenta)
#### Post date: [January 4, 2023, 12:45pm UTC](https://farmos.discourse.group/t/filtering-api-by-category/1405/13 "2023-01-04T12:45:10Z")

</div>

This appears to work in the browser address bar: `?filter[filtergroup][group][conjunction]=AND&filter[today][condition][path]=timestamp&filter[today][condition][operator]=%3E&filter[today][condition][value]=1672835969&filter[today][condition][memberOf]=filtergroup&filter[tomorrow][condition][path]=timestamp&filter[tomorrow][condition][operator]=%3C&filter[tomorrow][condition][value]=1672922369&filter[tomorrow][condition][memberOf]=filtergroup`

---

<div class="post-metadata">

### Author: ![pat](https://yyz2.discourse-cdn.com/free1/user_avatar/farmos.discourse.group/pat/32/1050_2.png) [@pat](https://farmos.discourse.group/u/pat)
#### Post date: [January 4, 2023, 1:08pm UTC](https://farmos.discourse.group/t/filtering-api-by-category/1405/14 "2023-01-04T13:08:41Z")

</div>

> [@mstenta](#):
>
> s to work in the browser address bar:

Ahh… it’s always in the details. Thanks.

---

<div class="post-metadata">

### Author: ![pat](https://yyz2.discourse-cdn.com/free1/user_avatar/farmos.discourse.group/pat/32/1050_2.png) [@pat](https://farmos.discourse.group/u/pat)
#### Post date: [January 4, 2023, 4:50pm UTC](https://farmos.discourse.group/t/filtering-api-by-category/1405/15 "2023-01-04T16:50:22Z")

</div>

> [@mstenta](#):
>
> This appears to work in the browser address bar: `?filter[filtergroup][group]`

Hmm.. if I run this filter in the browser, it doesn’t filter anything.  
How is that possible? Filters has worked there before.

It works now from Node-Red at least.

Actually, it would be a nice feature if the filter in farmOS could generate such filters.  
I tried that at first, but that only works for the table view.

---

<div class="post-metadata">

### Author: ![mstenta](https://yyz2.discourse-cdn.com/free1/user_avatar/farmos.discourse.group/mstenta/32/4_2.png) [@mstenta](https://farmos.discourse.group/u/mstenta)
#### Post date: [January 4, 2023, 5:08pm UTC](https://farmos.discourse.group/t/filtering-api-by-category/1405/16 "2023-01-04T17:08:12Z")

</div>

Ah… by “this works” I meant “this doesn’t error”. I didn’t actually examine the results. Maybe my timestamps were wrong or something. As long as it’s working in Node Red now that’s what matters. 👍

> [@pat](#):
>
> Actually, it would be a nice feature if the filter in farmOS could generate such filters.  
> I tried that at first, but that only works for the table view.

Yea those are two different systems. Views (which we defined) vs JSON:API (which comes “out of the box”). No relation, I’m afraid.

---

<div class="post-metadata">

### Author: ![pat](https://yyz2.discourse-cdn.com/free1/user_avatar/farmos.discourse.group/pat/32/1050_2.png) [@pat](https://farmos.discourse.group/u/pat)
#### Post date: [January 4, 2023, 10:08pm UTC](https://farmos.discourse.group/t/filtering-api-by-category/1405/17 "2023-01-04T22:08:25Z")

</div>

> [@mstenta](#):
>
> As long as it’s working in Node Red now that’s what matters. 👍

Yes indeed. But I run filters in url sometimes. And now it seems not working.

logs/activity?filter[status]=pending  
Works on the API, but not in the log view.

It’s not critical.

---

<div class="post-metadata">

### Author: ![mstenta](https://yyz2.discourse-cdn.com/free1/user_avatar/farmos.discourse.group/mstenta/32/4_2.png) [@mstenta](https://farmos.discourse.group/u/mstenta)
#### Post date: [January 5, 2023, 4:28pm UTC](https://farmos.discourse.group/t/filtering-api-by-category/1405/18 "2023-01-05T16:28:27Z")

</div>

Ah sorry I should have been more specific… that query string I pasted above “works” when you tack it onto the end of an _API endpoint_ (eg: `/api/log/activity`). It does NOT work if you tack it onto the end of the UI path `/logs/activity`.

> Yea those are two different systems. Views (which we defined) vs JSON:API (which comes “out of the box”). No relation, I’m afraid.

---

<div class="post-metadata">

### Author: ![pat](https://yyz2.discourse-cdn.com/free1/user_avatar/farmos.discourse.group/pat/32/1050_2.png) [@pat](https://farmos.discourse.group/u/pat)
#### Post date: [January 5, 2023, 4:30pm UTC](https://farmos.discourse.group/t/filtering-api-by-category/1405/19 "2023-01-05T16:30:24Z")

</div>

Ah… thanks for clarifying

---

<div class="post-metadata">

### Author: ![Zack](https://avatars.discourse-cdn.com/v4/letter/z/f6c823/32.png) [@Zack](https://farmos.discourse.group/u/Zack)
#### Post date: [June 27, 2023, 9:41pm UTC](https://farmos.discourse.group/t/filtering-api-by-category/1405/20 "2023-06-27T21:41:22Z")

</div>

Hey I’ve been trying to learn API filtering too and thought this was a good spot for this question.  
I am having issues when filtering for log categories that don’t have parents or have as the parent.

“/api/taxonomy\_term/log\_category?filter[parent.name][value]=Admin%20Work”  
Works as expected to gives me all categories with the parent Admin Work but I am trying to filter for all Parent categories.

I would expect “/api/taxonomy\_term/log\_category?filter[parent.id][value]=virtual” to work but I get no results or errors.

Anyone have any thoughts?

[Next page](https://farmos.discourse.group/t/filtering-api-by-category/1405.md?page=2)
