How can I create a REST API to reuse methods in client scripts in ERPNext?

Hi everyone,

I’m working on a project in ERPNext and I need to reuse certain methods across multiple client scripts for different DocTypes. I’m considering creating a REST API to centralize these methods so that they can be called from the client scripts, but I’m unsure about the best way to approach this within the Frappe/ERPNext framework.

Could someone guide me on how to create a REST API for this purpose? I’d appreciate any advice on how to structure the code and ensure that the API is both secure and efficient.

Thanks in advance!

Hi there,

If you’re looking to re-use custom code, you’re probably more interested in Frappe’s RPC interface than its REST methods.

You can define arbitrary API endpoints in Frappe in two primary ways:

  • Write a Server Script with the type set to API. This is all done directly from the desk, and these methods can be triggered from the client using frappe.call in javascript.

  • If you’ve built a private app, expose any arbitrary method using the @frappe.whitelist() decorator.

Both are described here:
https://frappeframework.com/docs/user/en/guides/basics/frappe_ajax_call

1 Like