api/crud
>

return function () {
    // the endpoints are user-side modules: the admin docs show the text only
    $demo = MAIN_TYPE_USER ? module_safe('api_demo') : null;
    $on = $demo instanceof yf_api_demo && ($demo->ENABLED ?? main()->is_demo_website());
    $intro = '<p class="docs-demo-intro">A JSON API module built on <code>yf_api_controller</code>: <code>GET /api/api_demo/items</code> lists,
        <code>POST</code> adds (name and price are validated on the server: a wrong value answers <b>422</b> with a message per field),
        <code>PATCH</code> / <code>DELETE /api/api_demo/item?item_id=<id></code> change and delete. The items live in your session only.
        Every write sends the session CSRF token in the <code>X-CSRF-Token</code> header; without it the answer is <b>403</b>.
        Tick <b>Envelope</b> to send <code>Accept: application/vnd.yf.api+json</code> and get <code>{ok, data, error, meta}</code>.
        Source: <code>plugins/docs/modules/yf_api_demo.class.php</code>; guide: <a href="' . url('/@object/misc/json_endpoints') . '#json-controller">JSON endpoints</a>.</p>';
    if ( ! $on) {
        return $intro . '<div class="alert alert-info">' . (MAIN_TYPE_USER ? '' : 'Open this demo in the docs of the site (user side). ')
            . 'The demo module answers on a demo website only (<code>main.DEMO_WEBSITE</code>), or with '
            . '<code>$PROJECT_CONF[\'api_demo\'][\'ENABLED\'] = true</code>.</div>';
    }
    $btn = 'btn btn-default btn-outline-secondary btn-xs btn-sm';
    $html = '<div id="docs-api-demo" data-items="' . _prepare_html(url('/api/api_demo/items'), 0) . '" data-item="' . _prepare_html(url('/api/api_demo/item'), 0) . '"'
        . ' data-reset="' . _prepare_html(url('/api/api_demo/reset'), 0) . '" data-token="' . _prepare_html(_class('csrf_guard')->session_token(), 0) . '">'
        . '<form class="form-inline docs-api-demo-add" autocomplete="off">'
        . '<input name="name" class="form-control input-sm form-control-sm" maxlength="80" placeholder="Name" aria-label="Name"> '
        . '<input name="price" class="form-control input-sm form-control-sm" inputmode="decimal" placeholder="Price" aria-label="Price"> '
        . '<button type="submit" class="btn btn-primary btn-xs btn-sm">Add</button> '
        . '<button type="button" class="' . $btn . '" data-act="reset">Reset</button> '
        . '<button type="button" class="' . $btn . '" data-act="no-token" title="POST without the CSRF token">Add without token</button> '
        . '<label class="checkbox-inline form-check-label"><input type="checkbox" data-act="envelope"> Envelope</label></form>'
        . '<table class="table table-bordered table-condensed table-sm"><thead><tr><th>Id</th><th>Name</th><th>Price</th><th></th></tr></thead><tbody></tbody></table>'
        . '<p>Last answer: <code class="docs-api-demo-status"></code></p><pre class="docs-api-demo-out"></pre></div>';
    js(<<<'JS'
(function () {
    var box = document.getElementById('docs-api-demo');
    if (!box) { return; }
    var tbody = box.querySelector('tbody'), out = box.querySelector('.docs-api-demo-out'), st = box.querySelector('.docs-api-demo-status');
    var env = box.querySelector('[data-act="envelope"]'), form = box.querySelector('form');
    var call = function (method, base, params, body, noToken) {
        var url = new URL(base, location.href);
        Object.keys(params || {}).forEach(function (k) { url.searchParams.set(k, params[k]); });
        var headers = { Accept: env.checked ? 'application/vnd.yf.api+json' : 'application/json' };
        if (method !== 'GET' && !noToken) { headers['X-CSRF-Token'] = box.dataset.token; }
        if (body !== undefined) { headers['Content-Type'] = 'application/json'; }
        return fetch(url, { method: method, headers: headers, credentials: 'same-origin', body: body === undefined ? undefined : JSON.stringify(body) })
            .then(function (res) {
                return res.text().then(function (text) {
                    text = text.replace(/^\)\]\}',\n/, '');
                    st.textContent = method + ' ' + url.pathname + url.search + ' -> ' + res.status;
                    out.textContent = text;
                    var data = null;
                    try { data = text ? JSON.parse(text) : null; } catch (e) { data = null; }
                    if (data && env.checked && Object.prototype.hasOwnProperty.call(data, 'ok')) { data = data.data; }
                    return { status: res.status, data: data };
                });
            });
    };
    var cell = function (tr, text) { var td = document.createElement('td'); td.textContent = text; tr.appendChild(td); return td; };
    var load = function () {
        var keep = [out.textContent, st.textContent];
        return call('GET', box.dataset.items, { per_page: 50 }).then(function (r) {
            tbody.textContent = '';
            ((r.data && r.data.items) || []).forEach(function (it) {
                var tr = document.createElement('tr');
                cell(tr, it.id); cell(tr, it.name); cell(tr, it.price);
                var td = cell(tr, '');
                var plus = document.createElement('button');
                plus.type = 'button'; plus.className = 'btn btn-default btn-outline-secondary btn-xs btn-sm'; plus.textContent = '+1';
                plus.title = 'PATCH: price + 1'; plus.setAttribute('aria-label', 'Price plus 1');
                plus.addEventListener('click', function () {
                    call('PATCH', box.dataset.item, { item_id: it.id }, { price: (parseFloat(it.price) + 1).toFixed(2) }).then(load);
                });
                var del = document.createElement('button');
                del.type = 'button'; del.className = 'btn btn-danger btn-xs btn-sm'; del.textContent = '×';
                del.title = 'DELETE'; del.setAttribute('aria-label', 'Delete');
                del.addEventListener('click', function () { call('DELETE', box.dataset.item, { item_id: it.id }).then(load); });
                td.appendChild(plus); td.appendChild(document.createTextNode(' ')); td.appendChild(del);
                tbody.appendChild(tr);
            });
            if (keep[1]) { out.textContent = keep[0]; st.textContent = keep[1]; }
        });
    };
    form.addEventListener('submit', function (e) {
        e.preventDefault();
        call('POST', box.dataset.items, {}, { name: form.elements['name'].value, price: form.elements['price'].value }).then(function (r) { if (r.status === 201) { form.reset(); } return load(); });
    });
    box.querySelector('[data-act="reset"]').addEventListener('click', function () { call('POST', box.dataset.reset).then(load); });
    box.querySelector('[data-act="no-token"]').addEventListener('click', function () {
        call('POST', box.dataset.items, {}, { name: 'No token', price: '1' }, true).then(load);
    });
    load();
})();
JS);
    return $intro . $html;
};

A JSON API module built on yf_api_controller: GET /api/api_demo/items lists, POST adds (name and price are validated on the server: a wrong value answers 422 with a message per field), PATCH / DELETE /api/api_demo/item?item_id=<id> change and delete. The items live in your session only. Every write sends the session CSRF token in the X-CSRF-Token header; without it the answer is 403. Tick Envelope to send Accept: application/vnd.yf.api+json and get {ok, data, error, meta}. Source: plugins/docs/modules/yf_api_demo.class.php; guide: JSON endpoints.

IdNamePrice

Last answer: