I work on the APIGOTO team. APIGOTO is an independent OpenAI-compatible gateway, not affiliated with OpenAI, Anthropic or Google. Nothing below needs our gateway: it works with any endpoint that speaks /v1/chat/completions, including a local server.
The goal is a whitelisted method, /api/method/llm_ask, that any client script, workflow or external system can call. It keeps the API key out of client-side JS.
0. Prerequisites
- Server Scripts must be enabled on the bench:
bench set-config -g server_script_enabled 1 - A base URL, an API key, and a model ID taken from
GET <base URL>/models
Test the endpoint from the server first. If this fails, nothing in Frappe will work:
curl https://api.china-aiapi.com/v1/chat/completions \
-H "Authorization: Bearer $API_KEY" -H "Content-Type: application/json" \
-d '{"model":"MODEL_ID","messages":[{"role":"user","content":"Say hi"}]}'
1. A Single DocType for the settings
Create a DocType LLM Settings with Is Single checked, and three fields:
| Label | Fieldname | Type |
|---|---|---|
| Base URL | base_url | Data |
| Model | model | Data |
| API Key | api_key | Password |
Restrict it to System Manager. Fill it in with https://api.china-aiapi.com/v1 (or your local server), MODEL_ID, and your key. A Password field is stored encrypted and is not returned by the REST API.
2. The Server Script
Script Type: API, API Method: llm_ask, Allow Guest: unchecked.
settings = frappe.get_doc("LLM Settings")
prompt = (frappe.form_dict.get("prompt") or "").strip()
if not prompt:
frappe.throw("prompt is required")
resp = frappe.make_post_request(
settings.base_url.rstrip("/") + "/chat/completions",
headers={
"Authorization": "Bearer " + settings.get_password("api_key"),
"Content-Type": "application/json",
},
json={
"model": settings.model,
"max_tokens": 300,
"messages": [
{"role": "system", "content": "Answer briefly. Use the user's language."},
{"role": "user", "content": prompt},
],
},
)
frappe.response["message"] = resp["choices"][0]["message"]["content"]
make_post_request raises on a non-2xx status and returns the parsed JSON, so there is no .json() call.
3. Call it from a form
A Client Script, for example on Issue, that drafts a reply from the description:
frappe.ui.form.on("Issue", {
refresh(frm) {
frm.add_custom_button("Draft reply", () => {
frappe.call({
method: "llm_ask",
args: { prompt: "Draft a polite first reply to: " + (frm.doc.description || "") },
freeze: true,
}).then(r => frappe.msgprint(frappe.utils.escape_html(r.message)));
});
},
});
From outside: POST /api/method/llm_ask with Authorization: token <api_key>:<api_secret> and {"prompt": "..."}.
Version notes
- On recent
develop, the request helpers exposed to server scripts go through a “safe request” wrapper. It blocks private and loopback addresses, ignores proxy environment variables, and applies a default(10, 300)second connect/read timeout. A local LLM onlocalhostor a LAN IP may be refused there, so use a custom app for that case. - On v15,
make_post_requestis the plain helper fromfrappe.integrations.utils, so check your timeout behaviour. - In both cases the call blocks the web worker. For long prompts, move the work to a background job (a custom app with
frappe.enqueue) and write the result to a field.
Troubleshooting
- “Server Scripts are disabled”: the bench config flag from step 0 is missing.
- HTTP 401 in the error log: wrong key, or a trailing space in the Password field.
- HTTP 404:
/v1missing or doubled in Base URL. - “model not found”: Model doesn’t match
/modelsexactly. - Worker timeouts: lower
max_tokens, or use a background job. - Privacy: the prompt text (customer names, ticket content) leaves your server. Decide what you send.
On the gateway side: one base URL for several model families, automatic failover on upstream errors (at most 3 retries per request on another route), a per-request log, pay-as-you-go, BYOK: www.china-aiapi.com
Has anyone moved this into a custom app with frappe.enqueue, and where do you write the result back?