Every call to an API function must handle 403
-
What you'll see
Page code calls an
/API/function withfetchor$.ajaxand handles only success. The visitor clicks Save and nothing happens, or gets «Something went wrong», or is sent to sign in — while the server answered with exactly what was wrong. Or an AI agent wrote the call, tried it once with valid input, and never saw the 403. -
What's actually happening
Docly answers every error that is raised on purpose with 403 — not 400, 404, 405 or 422. The status says only that the call was rejected; the body says why. One endpoint on developers.docly.net, three different rejections:
GET /API/Mod 403 {"ResultCode":0,"Message":"File is a required parameter!"} GET /API/Mod?file=/no-such-file 403 {"ResultCode":0,"Message":"Not found!"} GET /API/Mod?file=/%23/site.json 403 {"ResultCode":0,"Message":"Access denied!"}A missing parameter, a missing document and denied access get the same status. Code that reads 403 as «no access», or does not read it at all, gets all three wrong.
What answers 403
Raised by Body throw new Error("…")in your script{"ResultCode":0,"Message":"…"}docly.assert("…", code){"ResultCode":code,"Message":"…"}throw { … }with your own objectYour object, unchanged A docly function that refuses the call: invalid input, no access, a limit {"ResultCode":n,"Message":"…"}Any other failure while a docly function runs {"ResultCode":1,"Message":"…"}The site's security level, before the visitor has passed the check {"ResultCode":-213,"Message":"Verification required."}What does not
Situation Answer A publication that requires sign-in, and the session has ended 401{"ResultCode":401,"Message":"Login required.","LoginRequired":true}A fault in the script itself: a syntax error, or a TypeError such as reading a property of undefined 500with an HTML error page, not JSONA request body over 30 MB, or a form with too many fields 413or400A
returnis always200, whatever the object says. A script that returns{ ok: false }for the errors it expects still needs the 403 branch in its callers: the docly functions it calls and the security check answer 403 on their own. -
What to do
- Write the 403 branch together with the success branch, in every call. Not later, and not only where you expect a failure: any API function can answer 403.
- Show
Message. It is PascalCase, like the rest of the Docly format. When the script throws its own object, read your own fields instead. Write the text you throw for the visitor, since this is where it ends up. - Branch on
ResultCodeor your own fields, never on the text.-213means reload the page: the page view gets the security check, and the clearance then covers the API calls. - 403 is not «signed out». Do not send the visitor to sign in on a 403. A session that has ended is
401withLoginRequired. - Read the body as text before you parse it. A 500 is an HTML page, and
r.json()throws on it — then the visitor gets nothing at all.
Put it in one helper per site, and call every API function through it:
// One helper for every call to an API function on the site. async function callApi(name, fields) { const r = await fetch('~/API/' + name, { method: 'POST', body: new URLSearchParams(fields) }); const text = await r.text(); let data = null; try { data = JSON.parse(text); } catch { } // a 500 is HTML, not JSON if (r.ok) return data; // The security check, or a session that has ended: reload, so the // page view passes the check or signs in. const checkNeeded = r.status === 403 && data?.ResultCode === -213; const signedOut = r.status === 401 && data?.LoginRequired; if (checkNeeded || signedOut) { location.reload(); return new Promise(() => { }); // never settles: the page reloads } // 403: the call was rejected, and Message says why. const message = data?.Message || data?.error || 'The request failed.'; const error = new Error(message); error.status = r.status; error.resultCode = data?.ResultCode; error.data = data; throw error; } // Every call handles both outcomes. saveButton.addEventListener('click', async () => { try { const order = await callApi('orders/save', { id, quantity }); showSaved(order); } catch (e) { // "Quantity must be at least 1." - the text the script threw showError(e.message); } });With jQuery, the 403 arrives in the error callback:
$.post('~/API/orders/save', { id, quantity }) .done(order => showSaved(order)) .fail(xhr => showError(xhr.responseJSON?.Message || 'The request failed.'));Test the rejection, not just the success. Call the function with a missing or invalid value and check that the page shows the message. A test with valid input never reaches the 403 branch.
Last reviewed 03 Dec 2026