The smn helper reference

Reference for window.smn, the helper every app can use for variables, the viewer's name, outside services, MCP tools and the app's database, with examples and error codes.

window.smn is a small helper that your app's page can use. It talks to your app's own address (never to other websites) and carries no secrets. The same names work inside server functions as the global smn.

In a plain HTML page you can also add it explicitly:

<script src="/_smn/studio/v1/smn.js"></script>

smn.env()

Returns the app's non-secret variables as an object. See App configuration.

const env = await smn.env();
console.log(env.REGION);

The result is read once per page and reused. Secrets are never included.

smn.viewer()

Returns { displayName } for a signed-in viewer, or null. It answers only for an app that is not public, and only when the owner switched on "Tell the app who is viewing". It never returns an email or an account id, and public visitors are anonymous.

const viewer = await smn.viewer();
const greeting = viewer ? 'Hello, ' + viewer.displayName : 'Hello';

smn.fetch(name, path, init)

Calls a configured endpoint and returns a normal Response.

const response = await smn.fetch('weather', 'forecast?city=Lisbon');
if (!response.ok) throw new Error('The service answered ' + response.status);
const forecast = await response.json();

SeeMyNotes answers with a JSON body { error: { code, message } } and a status when it cannot make the call, for example when the endpoint is unknown or a limit was reached.

smn.mcp(name)

Returns a client for a configured MCP server, or for 'seemynotes' when "Use MCP from SeeMyNotes" is on.

const server = smn.mcp('docs');
const tools = await server.listTools();
const result = await server.callTool('search', { query: 'invoices' });
MethodWhat it does
listTools()Returns the server's tools.
callTool(tool, args)Calls a tool and returns its result. For seemynotes, a request key is added when you do not give one.
request(method, params)Sends any MCP request.

The session starts on first use.

smn.db

The app's database. See App data for what visitors can read and write.

CallWhat it does
smn.db.query(sql, params, { app })Runs one read-only statement. Returns { columns, rows, truncated }. Blobs are { $blob: base64 }.
smn.db.all(sql, params, { app })The same rows as objects keyed by column name.
smn.db.run(operation, params, { requestKey })Runs one of the owner's operations by name. The only way visitors change data.
smn.db.schema({ app })Returns the tables, their columns and the operations.
const rows = await smn.db.all('SELECT title FROM books WHERE author = ?', ['Ursula K. Le Guin']);
await smn.db.run('add_book', { title: 'The Dispossessed', author: 'Ursula K. Le Guin' });
const schema = await smn.db.schema();

Errors

Calls that fail throw an Error with these properties:

Common codes:

CodeMeaning
SQL_ERROR, SQL_INVALID, SQL_PARAMSThe statement or its parameters are wrong.
SQL_NOT_ALLOWEDThe statement is not allowed for visitors, for example a write or a private table.
DATA_NOT_SHAREDAnother app's data is not shared.
QUERY_TIMEOUT, QUERY_TOO_LARGEThe query took too long or answered too much.
RATE_LIMITED, QUOTA_EXCEEDEDA limit was reached; try again later.
STORAGE_QUOTA_EXCEEDED, DATABASE_TOO_LARGEThe app's storage is full.
DATA_DISABLED, DATA_UNAVAILABLE, DATA_BUSYData is paused or busy; try again.
DESTINATION_REFUSED, UPSTREAM_ERROR, UPSTREAM_TIMEOUTThe outside service could not be reached or failed.
CONFIG_INCOMPLETEA connection uses a variable that no longer exists.

Related pages