I have created multiple collections and entries to those collection i want to fetch all entries in one API call is there any way i can do that

I have created multiple collections and entries to those collection i want to fetch all entries in one API call is there any way i can do that


Hey @Kamlesh_Singh

Yes, you can achieve that in 1 API call by developing a custom API in any of the content type collection controller.

Below is an example on how to achieve that:

In /api/content-type/routes/content-type.js file:

{
    method: 'POST',
    path: '/your-api-name',
    handler: 'api::collectionName.collectionName.fetchAllData',
}

and add the function inside /api/content-type/controllers/content-type.js file:

fetchAllData(): async (ctx) => {

    const collection1 = await strapi.db.query('api::collection-1.collection-1').findMany();
    
    const collection2 = await strapi.db.query('api::collection-2.collection-2').findMany();

    ctx.send({
      status: 200,
      data: {
        'collection1_data': collection1,
        'collection2_data': collection2 
      }
    })
}

Set permission of the custom API from roles and you are good to go for executing the custom api.

Hope this information helps you :slight_smile:

Thanks

HI @Shekhar thanks for the reply. but in my case collections are not getting created by me(sorry for the miscommunication in the post) so I don’t know the collection name that the third person is going to create.

I have been using Contentful CMS for the past 2-3 days. and they have one global API that sends back the whole data of the collections that are being created inside one space. I want to get similar response

I don’t know about Contentful CMS but Afaik this is the only way in Strapi which can be done in a custom manner.

Do you have the access to Strapi Admin? If yes, you can find the collection name from here