$npx -y skills add Impertio-Studio/Frappe_Claude_Skill_Package --skill frappe-core-apiUse when building ERPNext/Frappe API integrations (v14/v15/v16) including REST API, RPC API, authentication, webhooks, and rate limiting. Covers external API calls, endpoint design, token/OAuth2/session authentication. Keywords: API integration, REST endpoint, webhook, token auth
| 1 | # Frappe API Patterns |
| 2 | |
| 3 | > Deterministic patterns for REST, RPC, and webhook integrations with Frappe. |
| 4 | |
| 5 | --- |
| 6 | |
| 7 | ## Decision Tree |
| 8 | |
| 9 | ``` |
| 10 | What do you need? |
| 11 | ├── CRUD on documents (external client) |
| 12 | │ ├── v14: REST /api/resource/{doctype} |
| 13 | │ └── v15+: REST /api/v2/document/{doctype} (new) or /api/resource/ (still works) |
| 14 | │ |
| 15 | ├── Call custom server logic (external client) |
| 16 | │ └── RPC: POST /api/method/{dotted.path.to.function} |
| 17 | │ |
| 18 | ├── Notify external systems on document events |
| 19 | │ └── Webhooks (configured in UI or via DocType) |
| 20 | │ |
| 21 | ├── Client-side calls (JavaScript in Frappe desk) |
| 22 | │ ├── frappe.xcall() — async/await (RECOMMENDED) |
| 23 | │ └── frappe.call() — callback/promise pattern |
| 24 | │ |
| 25 | └── Authentication method? |
| 26 | ├── Server-to-server integration → Token Auth (RECOMMENDED) |
| 27 | ├── Third-party app / mobile → OAuth 2.0 |
| 28 | ├── Browser session (short-lived) → Session/Cookie Auth |
| 29 | └── Quick scripting / testing → Token Auth |
| 30 | ``` |
| 31 | |
| 32 | --- |
| 33 | |
| 34 | ## Authentication Methods |
| 35 | |
| 36 | ### Token Auth (RECOMMENDED for integrations) |
| 37 | |
| 38 | ```python |
| 39 | headers = { |
| 40 | 'Authorization': 'token api_key:api_secret', |
| 41 | 'Accept': 'application/json', |
| 42 | 'Content-Type': 'application/json' |
| 43 | } |
| 44 | ``` |
| 45 | |
| 46 | Generate keys: User > Settings > API Access > Generate Keys. ALWAYS store API secret immediately — it is shown only once. |
| 47 | |
| 48 | ### Basic Auth (alternative token format) |
| 49 | |
| 50 | ```python |
| 51 | import base64 |
| 52 | credentials = base64.b64encode(b'api_key:api_secret').decode() |
| 53 | headers = {'Authorization': f'Basic {credentials}'} |
| 54 | ``` |
| 55 | |
| 56 | ### OAuth 2.0 (third-party apps) |
| 57 | |
| 58 | ``` |
| 59 | # Step 1: Authorization redirect |
| 60 | GET /api/method/frappe.integrations.oauth2.authorize |
| 61 | ?client_id={id}&response_type=code&scope=openid all |
| 62 | &redirect_uri={uri}&state={random} |
| 63 | |
| 64 | # Step 2: Exchange code for token |
| 65 | POST /api/method/frappe.integrations.oauth2.get_token |
| 66 | grant_type=authorization_code&code={code} |
| 67 | &redirect_uri={uri}&client_id={id} |
| 68 | |
| 69 | # Step 3: Use bearer token |
| 70 | Authorization: Bearer {access_token} |
| 71 | |
| 72 | # Refresh token |
| 73 | POST /api/method/frappe.integrations.oauth2.get_token |
| 74 | grant_type=refresh_token&refresh_token={token}&client_id={id} |
| 75 | ``` |
| 76 | |
| 77 | ### Session/Cookie Auth |
| 78 | |
| 79 | ```python |
| 80 | session = requests.Session() |
| 81 | session.post(url + '/api/method/login', json={'usr': 'email', 'pwd': 'pass'}) |
| 82 | # Subsequent requests use session cookie automatically |
| 83 | ``` |
| 84 | |
| 85 | Session cookies expire after ~3 days. NEVER use for long-running integrations. |
| 86 | |
| 87 | --- |
| 88 | |
| 89 | ## REST API: Resource CRUD |
| 90 | |
| 91 | ### Endpoints |
| 92 | |
| 93 | | Operation | Method | v14 Endpoint | v15+ v2 Endpoint | |
| 94 | |-----------|--------|--------------|------------------| |
| 95 | | List | GET | `/api/resource/{doctype}` | `/api/v2/document/{doctype}` | |
| 96 | | Create | POST | `/api/resource/{doctype}` | `/api/v2/document/{doctype}` | |
| 97 | | Read | GET | `/api/resource/{doctype}/{name}` | `/api/v2/document/{doctype}/{name}` | |
| 98 | | Update | PUT | `/api/resource/{doctype}/{name}` | PATCH `/api/v2/document/{doctype}/{name}` | |
| 99 | | Delete | DELETE | `/api/resource/{doctype}/{name}` | DELETE `/api/v2/document/{doctype}/{name}` | |
| 100 | | Copy | — | — | GET `/api/v2/document/{doctype}/{name}/copy` [v15+] | |
| 101 | | Doc Method | — | — | POST `/api/v2/document/{doctype}/{name}/method/{method}` [v15+] | |
| 102 | |
| 103 | **ALWAYS** include `Accept: application/json` header — without it, Frappe MAY return HTML. |
| 104 | |
| 105 | ### List Parameters |
| 106 | |
| 107 | | Parameter | Type | Description | Default | |
| 108 | |-----------|------|-------------|---------| |
| 109 | | `fields` | JSON array | Fields to return | `["name"]` | |
| 110 | | `filters` | JSON array | AND conditions | none | |
| 111 | | `or_filters` | JSON array | OR conditions | none | |
| 112 | | `order_by` | string | Sort expression | `modified desc` | |
| 113 | | `limit_start` | int | Pagination offset | `0` | |
| 114 | | `limit_page_length` | int | Page size | `20` | |
| 115 | | `limit` | int | Alias for limit_page_length [v15+] | — | |
| 116 | | `debug` | bool | Show SQL in response | `false` | |
| 117 | |
| 118 | ### Filter Operators |
| 119 | |
| 120 | ```python |
| 121 | filters = [["status", "=", "Open"]] |
| 122 | filters = [["amount", ">", 1000]] |
| 123 | filters = [["status", "in", ["Open", "Pending"]]] |
| 124 | filters = [["date", "between", ["2024-01-01", "2024-12-31"]]] |
| 125 | filters = [["reference", "is", "set"]] # NOT NULL |
| 126 | filters = [["reference", "is", "not set"]] # IS NULL |
| 127 | filters = [["name", "like", "%INV%"]] |
| 128 | filters = [["status", "not in", ["Cancelled"]]] |
| 129 | ``` |
| 130 | |
| 131 | Full operator list: `=`, `!=`, `>`, `<`, `>=`, `<=`, `like`, `not like`, `in`, `not in`, `is`, `between`. |
| 132 | |
| 133 | ### Pagination Pattern |
| 134 | |
| 135 | ```python |
| 136 | import json, requests |
| 137 | |
| 138 | def get_all_records(doctype, headers, base_ur |