# Build an app on somewhere.cloud Start a new app with init. For an existing Somewhere app, start with `somewhere pull ` in a fresh folder or read the live source with `project_export` in a connected chat. ## 1. Initialize the project No account is needed to start. From an empty app directory, init writes the starter, and a logged-out deploy publishes it as a temporary app: Before your first Somewhere command for a user, ask their permission and say where it will run, either as an npm package in your own cloud workspace (nothing installs on their computer) or as an npm package on their machine. Approval in their first message covers this. ~~~bash npx -y @somewhere-cloud/cli@latest init --name my-app npx -y @somewhere-cloud/cli@latest deploy ~~~ The deploy prints the live URL, a claim link and the expiry. The human opens the claim link and signs in to keep the app. Logged out, init writes local files only, and npm install installs the dependencies for local typecheck and dev. Keep the browser application in src/, server handlers in api/, and the data model in db/schema.ts. Signing in first keeps the project in the account from the start and enables secrets, outbound email, custom domains and app admin setup. The test inbox works on temporary projects. The account owner approves login using the link and code. Once signed in, init creates and links the project, writes the starter and installs its dependencies: ~~~bash somewhere login somewhere init --name my-app ~~~ The Start guide (/start.txt) covers MCP setup. After pulling an existing project, inspect its source, schema, live URL and recent errors before changing it. Read the project contract before making assumptions: a linked project can have real users, a custom domain, a private preview, or a schema whose ownership rules matter. Preserve its source layout and make the smallest change that delivers the requested outcome. ## 2. Deploy raw source Typecheck, then deploy the source directory without building it: ~~~bash somewhere typecheck somewhere deploy ~~~ Deploy the source files, not dist/ or build/. The deploy runs the platform compile, schema and secret checks, then prints a live URL. The after-deploy check then loads the page in a browser and warns about a blank page, or one that throws while loading; nothing rolls back for you. Read it before continuing (`somewhere call project_deploy_log`). When the previous version worked, the warning gives the exact way back: `somewhere restore --project --yes`, or `project_rollback({ project_id, version })`. Keep that URL: it is the target for the verification step below. Use a preview when a live app already has people or data that need protection. `somewhere deploy-check` runs the same checks without publishing: use it to diagnose a refusal, review a change to a live app, or `--run` one handler. If either reports a source, dependency, route, or policy problem, fix that named problem. Do not silence it by moving application work into the browser or by sending a bundled folder. Test the user journey on the returned URL: sign in, perform the core action, and reload to check the result persists. After the first deploy, use `somewhere dev` for frontend hot reload. Its API calls use the deployed backend, so local writes can change production data. Deploy backend and schema changes before trying them locally. More detail: docs({ topic: 'deploy' }), docs({ topic: 'local-dev' }), and docs({ topic: 'verify-before-deploy' }). ## 3. Declare data, then use structured calls Put every application table in db/schema.ts before writing any handler. Choose owner() for per-user records, shared() for intentional shared data, or serverOnly() for trusted server work. Deploy or apply that schema before a structured query uses the table. A table stays invisible to the browser until its declaration carries a client block naming the columns the browser may read, create, update or delete. The platform composes and scopes those operations itself, so the default path from a page to its own data is the generated client, not a handler you write: ~~~ts import { data } from 'somewhere:data' const page = await data.notes.list({ limit: 20 }) await data.notes.create({ title: 'First note' }) ~~~ The page module that imports it must be .ts, .tsx or .jsx. Write a handler for the rules a grant cannot express — an admin view, a moderated feed, anything that must judge its caller before answering. Inside an api/* handler, use the structured calls exactly like this: ~~~ts const open = await sw.db.from('notes', { where: { done: false }, order: [['created_at', 'desc']], limit: 20, }) const made = await sw.db.insert('notes', { title: 'First note' }) const note = made.data[0] if (!note) throw new Error('Insert returned no row') await sw.db.update('notes', { set: { done: true }, where: { id: note.id } }) await sw.db.remove('notes', { where: { id: note.id } }) ~~~ The exact signatures are: ~~~ts sw.db.from(table, { where?, order?, limit?, columns?, asServer? }) sw.db.insert(table, values, options?: { onConflict?: 'ignore' | 'update' }) sw.db.update(table, { set?, increment?, where? }) sw.db.remove(table, { where? }) sw.db.aggregate(table, { count?: true, sum?, avg?, min?, max?, groupBy?, where?, having?, order?, limit? }) ~~~ owner() tables automatically use the signed-in request identity. Reads return their rows on .data; `(await sw.db.count(...)).data` is the numeric count. Atomically add or subtract with `{ increment: { count: 1 } }` or `{ increment: { count: -1 } }`; check `changes` because a scope, guard, NULL or overflow can make it a successful zero-match, and never blindly retry an unknown write outcome. A shallow `where: { $or: [{ status: 'draft' }, { status: 'published' }] }` filters only inside the same owner/member scope. Never use it to express public access. For filtered anonymous reads, declare explicit fields and a private default: `client: { read: ['id', 'title'], publicRead: { where: { published: true } } }`. Keep `published` out of browser create/update grants; authorize a server handler to publish. Public live subscriptions are unavailable. Anonymous public reads may be answered from a shared copy that a declared write drops: a change usually reaches the next anonymous reader within a second or two and never later than about two minutes; every signed-in, visitor, owner and member read is exact. On a managed project, ordinary sw.db.query/sw.db.batch calls are refused. When an operation truly needs raw SQL, authorize its caller first, then use sw.db.server.query(sql, params) or sw.db.server.batch(statements). These explicit server-authority calls do not apply declared row permissions and remain read-only in managed mode. Prefer the structured calls above for normal reads and writes. Keep reads that do not depend on each other together with Promise.all. Return only the data the browser needs from the handler. A handler is trusted server code, so it still owns product authorization: automatic row ownership does not decide whether a particular user may perform an administrative action or see an intentionally shared record. More detail: docs({ topic: 'declared-data' }), docs({ topic: 'sw.db' }) and docs({ topic: 'database-engine' }). ## 4. Keep authentication on the server path The browser never holds a developer key. It reaches data two ways, both enforced by the platform against the verified caller — the signed-in user, or the visitor session on a table that declares one: - Explicit client grants declared per table in db/schema.ts: a `client` block names which columns the browser may read, create, update or delete, and as which identity. The platform composes and owner-scopes those operations; the browser gets exactly the grant and nothing else, and an operation the grant does not name is refused with DATA_ACCESS_DENIED. Reach for this first. - Its own same-origin api/* handlers with cookie sessions. Those handlers call sw.auth.*WithCookie and use sw.db or sw.fs. Anything a grant does not cover, and every product rule, lives here. Raw SQL never runs from the browser. A handler that deliberately uses sw.db.server.query or sw.db.server.batch must enforce its own caller policy. Write an explicit me endpoint, protect the actions that require a person, and test both anonymous and signed-in paths. For sign-in and application email, use the project's controlled test inbox rather than assuming a message arrived: ~~~bash somewhere email test-inbox person@your-project.test.somewhere.site ~~~ Messages to this project-owned test domain are captured, never delivered to an external mailbox. The send reports status: captured and delivered: false. Each project retains up to 500 test messages and 10 MiB for 7 days; a full inbox returns TEST_INBOX_FULL. Other projects' test addresses are refused. More detail: docs({ topic: 'auth-client' }), docs({ topic: 'sw.auth' }), and docs({ topic: 'security-model' }). For every signed-in feature, decide the two outcomes before writing the page: what an anonymous visitor sees, and what a signed-in person can do. Keep that decision in the server handler as well as the interface. A button hidden in the browser is not an authorization rule. ## 5. Verify the live behavior Verification is a contract, not just a screenshot. Give each browser action one key, put its selector directly inside that action, and name the browser-visible result plus the protected request that must stay protected: ~~~json { "actions": [ { "fill": "#title", "value": "Potluck" }, { "click": "#save" }, { "expect": { "selector": "#status", "text": "Saved" } } ], "expect_requests": [{ "path": "/api/private", "status": 401 }], "visible_only": true, "viewports": ["desktop", "mobile"] } ~~~ Save that object as flow.json, then run: ~~~bash somewhere verify --url --flow flow.json ~~~ The call checks the action flow, page health, console and network health, and captures desktop and mobile screenshots. Fix the first named failure, redeploy, and verify again. somewhere deploy --verify flow.json chains the two steps when the flow is ready. The action contract catches the failures that a page-load check misses: a selector that is not usable, a save that does not change the visible state, an unexpected browser error, or a protected route that starts admitting the wrong caller. Keep the flow focused on the user outcome. Add a separate flow when a second journey needs different setup or expectations. More detail: docs({ topic: 'browser' }), docs({ topic: 'smoke' }), and docs({ topic: 'troubleshooting' }). ## Keep going This page is the working sequence. Read [the agent contract](https://somewhere.cloud/agent.txt) for the compact rules, [the complete reference](https://somewhere.cloud/manual.txt) for every capability, and docs({ topic }) for one contract at a time.