# BrewPage Brew Lambda: server code of your page, build locally, publish Brew Lambda: one file lambda.js runs on BrewPage's server for one page or site, never in the browser; visitors never see it. Use it only where Page data list rules end (https://brewpage.app/llms-page-data.txt): react to a save or comment, refuse or fix a save, 1-2 own actions. No lambda.js for pages that only save and show entries (guestbook, poll, sign-up list): Page data list rules do that alone. 1) Folder: index.html (page, with the state.js tag), lambda.js (server code), state.json (Page data rules, {} = all private). Code-only lists (slots): {"collections":{"bookings":{"mode":"shared","write":"author"}}}, else visitors save there too. 2) Run locally, Node 22+: npx brewpage-data@0.2.1 dev -> open http://localhost:8787/ (not file://). Viewer: http://localhost:8787/_bp/dev/ Test: save on the page or the viewer's Controls tab; runs, logs, notices show there and in the terminal. Nothing reaches BrewPage (notices are printed); internet fetch only with --network (else unavailable). 3) lambda.js, plain JavaScript (step 6) plus bp: export async function onEntry(event, bp) { // after a visitor's save if (event.collection !== "bookings") return; await bp.notify.owner({title: "New booking", text: event.entry.value.name}); } export function beforeWrite(event) { if (!event.value.name) return {refuse: "Enter your name."}; } // nothing = accept; {value} = changed export const endpoints = { async book(req, bp) { return {ok: true}; } }; // at most 2; page: await bp.lambda("book", {slot}) 4) Handlers: one ES module, named exports onEntry, onComment, beforeWrite, endpoints; no import or require; <= 32 KiB. onEntry(event, bp): after a visitor save; event {type:"entry", id, collection, op:"created"|"updated", entry:{id, key, value, version, createdAt, updatedAt}, visitor:{id, signedIn, role}, page:{type, id, url}}. Your own bp writes never fire it. onComment(event, bp): after a new visitor or member comment; event {type:"comment", id, comment:{id, parentId, author, authorName, text, createdAt}, visitor, page}; needs visitor comments: public pages, else manage page Comments. beforeWrite(event, bp): before a visitor save (the visitor waits, 300 ms); event {type:"write", collection, key, op, value, visitor, page}; return nothing or null = accept, {refuse: "text"} = refused with your text, {value: newValue} = save this instead; other returns, throws, timeouts = generic refusal. Only bp.data.get/list/count (no fetch, writes, notify). endpoints: export const endpoints = { name(req, bp) {...} }; at most 2 keys, name a-z first then letters, digits or _; req {body (null if none sent), visitor, page}; return = the JSON answer. Page call: const answer = await bp.lambda("name", body) (needs the state.js tag); errors throw bp.StateError: check e.code, show e.message; a save refused by beforeWrite throws e.code "refused", e.message = your text. 5) bp calls (all async, this page or site only; errors throw LambdaError {code, message}; fetch errors: TypeError with e.code): bp.data.get(list, key) -> author value or null bp.data.list(list, {where, visitor, order, limit, cursor}) -> {items:[{id,key,value,visitor,createdAt,updatedAt,version}], next}; every writer's entries; where = equality on <= 3 top-level fields; limit 1-100 bp.data.count(list, {where, countBy}) -> {count, by?}; countBy = a top-level field, by = {fieldValue: count} bp.data.set(list, key, value) -> {id, key, version} (author entry, upsert) bp.data.create(list, key, value) -> {id, key, version}, or null when the key exists (atomic: use it to take a slot once); key chars A-Z a-z 0-9 _ . - (2026-12-05T10-00: no : or space) bp.data.add(list, value) -> {id, key, version} (random key) bp.data.remove(list, key) -> true/false (author entry); visitor entry: bp.data.removeById(list, entry.id) bp.data.files(list, {limit, cursor}) -> {items:[{id,url,name,contentType,sizeBytes,visitor,createdAt}], next}; bytes: fetch(item.url) bp.comments.list({limit, cursor}) -> {items:[comment], next}; bp.comments.reply(commentId or null, text) -> {id} (posts as the owner); bp.comments.remove(commentId) -> true/false bp.notify.owner({title, text, link?}) -> {sent: true}; title <= 80, text <= 500 chars; owner's bell notice; link: absolute URL on this page's host (event.page.url + "#x") or it throws bp.site.list() -> [{path, contentType, sizeBytes}]; read one: fetch("./path") bp.site.write(path, text) -> {path, sizeBytes}; sites only, .json/.txt/.csv files bp.account.list({type, cursor}) -> {items:[{type,namespace,id,title,url,createdAt,expiresAt}], next}; type = "html" (page), "site", "file", "json" or "kv", optional: omitted = every type, in profile order; bp.account.read(ref) -> {type, contentType, text}; bp.account.data(ref).list/get/count; needs owner switch "Reads your other pages" 6) Plain JavaScript: fetch (absolute: public https:// port 443 only; relative: own files; body: string or URLSearchParams), URL, URLSearchParams, Headers, Request, Response, console.log/info/warn/error (= run log), crypto.randomUUID/getRandomValues, TextEncoder/Decoder, structuredClone, atob, btoa, Intl.DateTimeFormat, Intl.NumberFormat (toLocale*String ignore locales), JSON, Date, Math, Promise, Map, Set, RegExp. Not available: setTimeout/setInterval, import/require, eval/new Function, process, Buffer, file system, XMLHttpRequest, WebSocket, WebAssembly. 7) Limits per run: endpoint 2 s, beforeWrite 300 ms, events 10 s; 30 bp/fetch calls (beforeWrite 5); fetch 5 per event run, 3 per endpoint run, none in beforeWrite; 10 data writes, 3 notices; memory 16 MiB; answers <= 64 KiB; endpoint body <= 16 KiB; values <= 16 KiB (state maxDocBytes <= 64 KiB). 8) Errors: run errors, logs, limit hits: viewer's Runs tab (published: manage page or read_publication view lambda); a failed event run never undoes the visitor's save. 9) Publish (needs a free BrewPage profile): npx brewpage-data@0.2.1 publish with a profile token in BREWPAGE_OWNER_TOKEN, or MCP publish with content or files, state and lambda (the text of lambda.js); give the link and the manageUrl (private), CLI: its receipts file. Full reference (errors, limits, owner API, local SDK): https://brewpage.app/llms-lambda-full.txt Guide: https://brewpage.app/lambda MCP: get_guide topic lambda