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();
nameis the endpoint's name (lowercase letters, digits and-).pathis added to the endpoint's base URL. A leading/is ignored.inittakes the usualfetchoptions such asmethod,headersandbody. Only a few harmless headers reach the other service; cookies and authorization from the visitor never do. Your configured headers and query are added on the server.
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' });
| Method | What 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.
| Call | What 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. |
paramsis a list for?placeholders, or an object for named ones. A list of numbers is treated as a vector.appis optional: the id of another of the owner's apps that shares its data.Operation names use lowercase letters, digits and
_.runadds a request key, so retrying with the same key never writes twice.
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:
error.code: a short code, such asRATE_LIMITED.error.status: the HTTP status.error.details: extra information when there is some.
Common codes:
| Code | Meaning |
|---|---|
SQL_ERROR, SQL_INVALID, SQL_PARAMS | The statement or its parameters are wrong. |
SQL_NOT_ALLOWED | The statement is not allowed for visitors, for example a write or a private table. |
DATA_NOT_SHARED | Another app's data is not shared. |
QUERY_TIMEOUT, QUERY_TOO_LARGE | The query took too long or answered too much. |
RATE_LIMITED, QUOTA_EXCEEDED | A limit was reached; try again later. |
STORAGE_QUOTA_EXCEEDED, DATABASE_TOO_LARGE | The app's storage is full. |
DATA_DISABLED, DATA_UNAVAILABLE, DATA_BUSY | Data is paused or busy; try again. |
DESTINATION_REFUSED, UPSTREAM_ERROR, UPSTREAM_TIMEOUT | The outside service could not be reached or failed. |
CONFIG_INCOMPLETE | A connection uses a variable that no longer exists. |