Field Sorting Capability in Frappe Rest Api

-Is there a sorting capability in Frappe Rest Api?

I want, for example, to get a list of Customers ordered by name.

ERPNextInstance/api/resource/Customer?fields[“*”]

and get the json response ordered by Customer Name

Thanks in advance

Not sure if this is the best way, but here’s how I’d do it:

You might try building a server-side RPC call that queries the table directly - you could then use the usual SQL to order or format the data any way you wanted.

Here’s a very simple routine I put in my api.py for my custom app that queries the Files table,

@frappe.whitelist()
def getFilesDict():
document = frappe.db.sql(“select * from tabFile order by file_name desc”, as_dict=True)
return document

I can then call it with api/method/customapp.api.getFilesDict

2 Likes

As of now you cannot explicitly define any sorting options for the default ReST APIs. You do receive them by the order of creation at the moment. Any sorting has to be handled by the client thereafter.

6 Likes

As per the documentation, it is possible to sort the API results:
GET /api/resource/:doctype?order_by={field_name}%20{asc|desc}

Note: There must be a URL encoded space “%20” separating field_name and sort order.