cardinal

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:

crm/lib/rpc/crm_holiday_list.ex
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)}
end

The 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

OptionValues
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) do

Call 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 })).data

From 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).

RouteDoes
GET /api/rec/:rectypeList. Query: fields, filters, or_filters, limit, offset as JSON, order_by as text. 20 rows by default
POST /api/rec/:rectypeInsert
GET /api/rec/:rectype/countCount
GET /api/rec/:rectype/:nameRead, as {data, recinfo}
PUT /api/rec/:rectype/:nameSave. The body must carry the modified it loaded, or it is a 409
DELETE /api/rec/:rectype/:nameDelete
POST /api/rec/:rectype/:name/submit, /cancel, /amend, /discardDocstatus moves
POST /api/rec/:rectype/:name/rename{new_name, merge}
GET /api/meta/:rectypeThe 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}.

TypeStatus
Validation422
Authentication401
Permission403
NotFound404
TimestampMismatch, LinkExists, DuplicateEntry409
RateLimited429

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.

On this page