Custom API rows in financial report templates ​

A row in a Financial Report Template with Data Source set to Custom API gets its values from a Python method in an installed Frappe app. Use it only when Account Data and Calculated Amount rows cannot express the line, for example a management metric that your custom app calculates.

This page is for developers. The method runs on the ERPNext server. It is not a call to an external HTTP endpoint, although your method can call one.

Row settings ​

FieldValue
Data SourceCustom API
API Method PathThe dotted path of the method, in the form app.module.method, for example my_app.financial_report_api.get_budget_variance. Required.
Line ReferenceOptional. Set it when Calculated Amount rows use the value.
Reverse SignMultiplies each returned value by -1.
Value Type, Hide If Zero, Hidden Line (Internal Use Only), Include in ChartsWork as on other rows.

Method requirements ​

  • The method is defined at module level in an app installed on the site.
  • The method is whitelisted and allows GET requests: @frappe.whitelist(methods=["GET"]). GET-only methods are treated as read-only. ERPNext rejects other methods when you save the template and when the report runs.
  • Server Scripts are not supported.

Signature ​

ERPNext calls the method with three keyword arguments:

ArgumentTypeContent
filtersdictThe report filters, such as company, from_fiscal_year, period_start_date, finance_book.
periodslist of dictThe report periods in column order. Each has key, label, from_date, and to_date.
rowFinancial Report Row documentThe template row being calculated.

Return value ​

Return a list of numbers with one value per period, in the same order as periods, for example [1000.0, 1200.0, 1150.0].

Example ​

python
import frappe
from frappe.utils import flt


@frappe.whitelist(methods=["GET"])
def get_budget_variance(filters, periods, row):
	values = []
	for period in periods:
		variance = calculate_variance(  # your own calculation
			company=filters.get("company"),
			from_date=period.get("from_date"),
			to_date=period.get("to_date"),
		)
		values.append(flt(variance))
	return values

Processing order ​

ERPNext calculates Custom API rows first, then Account Data rows, then Calculated Amount rows. A Custom API method therefore cannot use the values of other template rows. A formula row can use the value of a Custom API row through its Line Reference.

Errors ​

SituationResultFix
The path has no dotSave fails: "API Method Path should be in format: app.module.method"Enter the full dotted path.
The method is not whitelisted or does not allow GETSave fails: "Method '...' must be whitelisted and permit GET requests"Add @frappe.whitelist(methods=["GET"]).
The method cannot be importedSave fails: "Could not validate API Method Path: ..."Check the path and that the app is installed.
The method raises an error while the report runsThe row shows 0 for every period, and the Error Log has an entry "Custom API Error: ..."Read the Error Log, and test the method with the same filters.

A row of zeros is a failed calculation, not a real result. Check the Error Log before you share the report.

Good practice ​

  • Keep the method read-only. Never create or change documents while a report runs.
  • Check permissions and validate filter values. Do not return data that the report user must not see.
  • Keep queries bounded, and test periods with no data, different periodicities, several companies, foreign currencies, and long date ranges.
  • Reconcile the result with an independent calculation before you publish the template.