# Querying multiple collections is giving - Collection not found

**URL:** <https://discourse.getcockpit.com/t/querying-multiple-collections-is-giving-collection-not-found/1631>\
**Category:** Support\
**Created:** [September 12, 2020, 2:52pm UTC](https://discourse.getcockpit.com/t/querying-multiple-collections-is-giving-collection-not-found/1631 "2020-09-12T14:52:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nithinkashyapn](https://avatars.discourse-cdn.com/v4/letter/n/d26b3c/32.png) [@nithinkashyapn](https://discourse.getcockpit.com/u/nithinkashyapn)\
**Post date:** [September 12, 2020, 2:52pm UTC](https://discourse.getcockpit.com/t/querying-multiple-collections-is-giving-collection-not-found/1631/1 "2020-09-12T14:52:17Z")

</div>

I am trying to query multiple collections as shown -

`/api/collections/get/(authors|posts)`

But i keep getting the error like -

```
{
    "error": "Collection not found"
}

```

Can anyone help me where i am going wrong? The individual APIs work correctly

---

<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:** [September 15, 2020, 8:37am UTC](https://discourse.getcockpit.com/t/querying-multiple-collections-is-giving-collection-not-found/1631/2 "2020-09-15T08:37:01Z")

</div>

This is not possible by default and it would only make sense if they have similar fields. Otherwise the filters wouldn’t work correctly.

If you need to query multiple collections with on api request, you have to write your own api endpoint.

What’s your actual goal? Maybe I can point you in the right direction.

---

<div class="post-metadata">

**Author:** ![nithinkashyapn](https://avatars.discourse-cdn.com/v4/letter/n/d26b3c/32.png) [@nithinkashyapn](https://discourse.getcockpit.com/u/nithinkashyapn)\
**Post date:** [September 16, 2020, 1:39pm UTC](https://discourse.getcockpit.com/t/querying-multiple-collections-is-giving-collection-not-found/1631/3 "2020-09-16T13:39:20Z")

</div>

Hi,

I am creating a landing page which draws information from multiple collections like carousel, comments, and posts. One way how i got it to work in a single query is by using CockpitQL and calling a GraphQL API with `graphql-request`. Is there any other way to get the same output?

The documentation tells querying multiple is possible - [https://getcockpit.com/documentation/api](https://getcockpit.com/documentation/api) so only got confused

---

<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:** [September 16, 2020, 5:52pm UTC](https://discourse.getcockpit.com/t/querying-multiple-collections-is-giving-collection-not-found/1631/4 "2020-09-16T17:52:56Z")

</div>

> [@nithinkashyapn](#):
>
> The documentation tells querying multiple is possible

Ah, I see. This is regex style is only _to give permission_ to fetch multiple collections with single api keys. You can’t fetch them this way.

> [@nithinkashyapn](#):
>
> Is there any other way to get the same output?

You can create a custom api endpoint.

> [@How to create custom API Endpoints](https://discourse.getcockpit.com/t/how-to-create-custom-api-endpoints/202/5):
>
> In Cockpit you can create a custom api entry point pretty easy: Create the following file /config/api/custom.php we now do a custom query in that file: \<?php // very simple code here $test = $this-\>param('test'); if (!$test) { return false; } return $test; now you can query /api/custom?token=xxx&test=foo,bar,baz if you want to have the api public (without token) then create the file in a subfolder named public: /config/api/public/custom.php now you can query /api/public/custom?tes…

sample:

```php
<?php
$authors = cockpit('collections')->find('authors');
$posts = cockpit('collections')->find('posts');
return compact('authors', 'posts');

```

---

<div class="post-metadata">

**Author:** ![artur](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/artur/32/4_2.png) [@artur](https://discourse.getcockpit.com/u/artur)\
**Post date:** [September 16, 2020, 6:08pm UTC](https://discourse.getcockpit.com/t/querying-multiple-collections-is-giving-collection-not-found/1631/5 "2020-09-16T18:08:15Z")

</div>

Possible solution:

create a file `config/api/public/multirequest.php`

```auto
<?php

/**

    <requests> parameter should look like:

    {
        "posts": {
            "endpoint": "/api/collections/get/posts",
            "params": {"lang": "de"}
        },
        "footer": {
            "endpoint": "/api/singletons/get/footer",
            "params": {"lang": "de"}
        }
    }

 */

$requests = $this->param('requests');

if (!$requests) {
    $this->stop(['error' => 'requests parameter is missing'], 412);
}

$ret = [];
$cockpit = cockpit();

foreach ($requests as $k => $req) {

    if (!isset($req['endpoint'])) continue;

    $request = new Lime\Request([
        'request' => array_merge([], $req['query'] ?? [], $req['post'] ?? [], $req['params'] ?? []),
        'post' => $req['post'] ?? [],
        'cookies' => $req['cookies'] ?? [],
        'query' => $req['query'] ?? [],
        'files' => $request->files ?? [],
        'server' => $req['server'] ?? [],
        'headers' => $req['headers'] ?? [],
    ]);

    try {

        $r = $cockpit->run($req['endpoint'], $request, false);

        $body = $r->body;
        $mime = $r->mime;

        if (\is_array($body) || \is_object($body)) {
            $mime = 'json';
        }

        $ret[$k] = [
            'status' => $r->status,
            'mime' => $mime,
            'body' => $body,
        ];

    } catch(\Throwable $e) {
        
        $ret[$k] = [
            'error' => $e->getMessage(),
            'file' => $e->getFile(),
            'line' => $e->getLine(),
        ];
    }
}

return $ret;

```

now you can query multiple api endpoints by requesting `/api/public/multirequest`
