RPC and REST
Expose Elixir functions with @rpc, and read records through /api/rec.
An RPC is Frappe's whitelisted method. Put it in lib/rpc/ in a module named <App>.Rpc.<Module>, and mark each function with @rpc. Unmarked functions are not reachable.
Write an RPC
CRM's Holiday List form has two buttons. Each calls an RPC:
defmodule Crm.Rpc.CrmHolidayList do
use Cardinal.Rpc
alias Crm.Controllers.CRMHolidayList
@rpc requires: {:write, "CRM Holiday List"}, args: [doc: :map]
def get_weekly_off_dates(_ctx, %{doc: doc}), do: CRMHolidayList.weekly_off_dates(doc)
@rpc requires: {:write, "CRM Holiday List"}, args: [doc: :map]
def clear_table(_ctx, %{doc: doc}), do: {:ok, CRMHolidayList.clear_table(doc)}
endThe first answers POST /api/rpc/crm.crm_holiday_list.get_weekly_off_dates. The path is <app>.<module in snake case>.<function>.
The function takes the ctx and a map of the declared arguments with atom keys. It returns {:ok, value}, {:ok, value, effects} or {:error, reason}, and runs in the request's transaction.
@rpc options
| Option | Values |
|---|---|
requires: | Required. :guest, :user, {:role, "Sales Manager"} or {:read, "CRM Lead"} |
args: | Each argument's type: :string, :integer, :boolean, :list, :map, :any, or {:optional, type} |
method: | :post (default) or :get. A GET reads its arguments from the query string |
txn: | false runs the function outside the request's transaction, for calls that wait on another system |
limit: | {count, seconds}, a rate limit per user, or per client address for a guest |
A missing requires: fails the compile. A missing, mistyped or undeclared argument is a 422 before the function runs.
@rpc requires: :user, args: [rectype: :string, name: :string, lost_reason: :string, lost_notes: {:optional, :string}]
def mark_lost(ctx, %{rectype: rectype, name: name, lost_reason: reason} = args) doCall it
From the desk, through the sdk:
import { request } from '@cardinal/sdk'
const doc = (await request<{ data: unknown }>('POST', '/api/rpc/crm.crm_holiday_list.clear_table', { doc })).dataFrom Elixir, with the same checks: Cardinal.Rpc.call(ctx, "gate.mappers.make_sale", %{source_name: name}).
REST routes for records
Every RecType gets these routes with no code. URL-encode the RecType name (/api/rec/Sales%20Order).
| Route | Does |
|---|---|
GET /api/rec/:rectype | List. Query: fields, filters, or_filters, limit, offset as JSON, order_by as text. 20 rows by default |
POST /api/rec/:rectype | Insert |
GET /api/rec/:rectype/count | Count |
GET /api/rec/:rectype/:name | Read, as {data, recinfo} |
PUT /api/rec/:rectype/:name | Save. The body must carry the modified it loaded, or it is a 409 |
DELETE /api/rec/:rectype/:name | Delete |
POST /api/rec/:rectype/:name/submit, /cancel, /amend, /discard | Docstatus moves |
POST /api/rec/:rectype/:name/rename | {new_name, merge} |
GET /api/meta/:rectype | The compiled RecType |
curl -H "Authorization: token $KEY:$SECRET" \
'http://mysite.localhost:4000/api/rec/CRM%20Task?fields=["name","status"]&filters=[["status","=","Todo"]]'Success is {"data": ...}. Every failure is {"error": {"type", "message", "fields"}}, where fields lists {path, code, message}.
| Type | Status |
|---|---|
Validation | 422 |
Authentication | 401 |
Permission | 403 |
NotFound | 404 |
TimestampMismatch, LinkExists, DuplicateEntry | 409 |
RateLimited | 429 |
Authenticate with a session cookie, or with Authorization: token KEY:SECRET. A System Manager makes a key with Generate Keys on a User's form.