How to retrieve collections via Silverstripe RESTful API

I’m working with Silverstripe’s Restfulserver module and need help with fetching multiple records at once. I know I can send a POST request to create new items and use GET with an ID to fetch individual records, but I can’t figure out how to get a whole collection.

For instance, I want to hit /products and get back all my product records in some sensible order. Right now I’ve built a custom method in my page controller that handles this, but I’m wondering if there’s a more RESTful approach available in Silverstripe.

Also, is it possible to add query parameters to filter the results? Something like /products?category=electronics&price_min=100 to get products within certain criteria? I’d love to know if Silverstripe’s REST implementation supports this kind of filtering out of the box.

yep, you can get collections via restfulserver by hitting /api/v1/YourDataObject without an ID. but be warned, filtering options are limited. it mainly supports pagination, so if you want more specific filters, you’ll have to customize it or build new endpoints.

Yeah, Restfulserver handles collection retrieval with GET requests to /api/v1/YourModelName, but watch out for permissions. Your DataObject needs proper canView() permissions set up - otherwise you’ll get empty results even with $api_access = true. I ran into this exact problem where my API kept returning blank collections until I figured out the default permission model was blocking everything. The filtering options are pretty basic though - you get pagination and that’s about it. If you need advanced filtering, I’d build a custom API endpoint with Silverstripe’s Controller class instead. Way more control over queries and parameters while keeping it RESTful.

Yes, you can retrieve a collection using Silverstripe’s Restfulserver by sending a request to /api/v1/Product without appending an ID. Ensure that the DataObject in question has $api_access set to true, which allows access to your Product records. However, be aware that native filtering options are somewhat limited. While you can implement basic pagination through query parameters like ?start=0&limit=20, more specific filters such as category and price will require custom modifications. I faced a similar challenge and opted to extend the RestfulServer class to create tailored filtering logic, which involved overriding the index method in your API controller. Although this requires more upfront effort, it ultimately grants you greater control over how results are filtered.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.