# Fetching multiple resources using Ids

**URL:** <https://discuss.jsonapi.org/t/fetching-multiple-resources-using-ids/1611>\
**Category:** Uncategorized\
**Created:** [June 27, 2019, 3:57pm UTC](https://discuss.jsonapi.org/t/fetching-multiple-resources-using-ids/1611 "2019-06-27T15:57:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pankajverma](https://avatars.discourse-cdn.com/v4/letter/p/6bbea6/32.png) [@pankajverma](https://discuss.jsonapi.org/u/pankajverma)\
**Post date:** [June 27, 2019, 3:57pm UTC](https://discuss.jsonapi.org/t/fetching-multiple-resources-using-ids/1611/1 "2019-06-27T15:57:38Z")

</div>

I have requirement to fetch multiple articles in one response in GET. Is bellow allowed?

[api.xycp.com/articles?filter[id]=111&filter[id]=222&filter[id]=333](http://api.xycp.com/articles?filter%5Bid%5D=111&filter%5Bid%5D=222&filter%5Bid%5D=333)

Am I violating any standard?

---

<div class="post-metadata">

**Author:** ![cmeeren](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.jsonapi.org/cmeeren/32/596_2.png) [@cmeeren](https://discuss.jsonapi.org/u/cmeeren)\
**Post date:** [June 30, 2019, 9:21pm UTC](https://discuss.jsonapi.org/t/fetching-multiple-resources-using-ids/1611/2 "2019-06-30T21:21:15Z")

</div>

Personally I would do

filter[id]=111,222,333

Though depending on the reason for your need to fetch multiple resources by ID, you may consider if the resources should be in some relationship, so that you can just fetch it like a normal relationship.

---

<div class="post-metadata">

**Author:** ![SpirIT](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.jsonapi.org/spirit/32/600_2.png) [@SpirIT](https://discuss.jsonapi.org/u/SpirIT)\
**Post date:** [July 1, 2019, 5:06pm UTC](https://discuss.jsonapi.org/t/fetching-multiple-resources-using-ids/1611/3 "2019-07-01T17:06:19Z")

</div>

Hey, your way wouldn’t work to good, because you always override the value from before.  
(you would have to send is as array ?filter[id]=1&filter[id]=2&filter[id]=3)

I wouldn’t make that comma-separated list inside a filter.  
Either you call the resource-url like:  
/articles/1,2,3

or  
you can implement an advanced filtering called LHS brackets.

In you example:  
/articles?filter[id][in]=1,2,3

Advantage here, beside operator “in” you can implement “startsWith”, “endsWith”, “contains”, gt, gte, lt, lte, xor, etc. what you can think of and what is needed.

In the first case you implement GET /articles/1,2,3  
you can do the same with destroying requests DELETE /articles/1,2,3  
or even changing lots of objects with request PATCH /articles/1,2,3 { publish: true }

And its a legitime restful pattern (It is NOT specified in jsonapi, but neither that it is NOT allowed)  
So in my opinion this is free to you to choose.

---

<div class="post-metadata">

**Author:** ![n2ygk](https://avatars.discourse-cdn.com/v4/letter/n/50afbb/32.png) [@n2ygk](https://discuss.jsonapi.org/u/n2ygk)\
**Post date:** [July 3, 2019, 1:18pm UTC](https://discuss.jsonapi.org/t/fetching-multiple-resources-using-ids/1611/4 "2019-07-03T13:18:42Z")

</div>

The JSONAPI spec specifically doesn’t define the semantics of `filter` but there is a [recommendation](https://jsonapi.org/recommendations/#filtering) that shows an example of the `filter[id]=1,2,3` style and the implication that multiple filter occurrences are ANDed together – there’s really not a way to OR filters that I’ve seen examples of other than list membership.

If you are using Python/Django there is [this implementation](https://django-rest-framework-json-api.readthedocs.io/en/stable/usage.html#djangofilterbackend) which uses something based on the Django `__` (double underscore – remapped to dot) notation, for example: `filter[id.in]=1,2,3` or `filter[id.lte]=3`.

---

<div class="post-metadata">

**Author:** ![maark](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.jsonapi.org/maark/32/184_2.png) [@maark](https://discuss.jsonapi.org/u/maark)\
**Post date:** [July 24, 2019, 3:37am UTC](https://discuss.jsonapi.org/t/fetching-multiple-resources-using-ids/1611/5 "2019-07-24T03:37:26Z")

</div>

> [@SpirIT](#):
>
> /articles/1,2,3

I would avoid that. It isn’t clear what it is. Maybe this is an ID that contains commas? Also, I’m not sure it’s allowed by the spec. It’s definitely very hacky. There are much cleaner solutions to the problem we have here.
