Building apps
App code
What an app's code looks like - the files of a version, the services the platform hands it, and the rules it is compiled under - for reading what an agent wrote or writing it yourself.
Most apps are written by an agent, and nobody has to read their code. When you want to - to review, to change something by hand, to learn - this page is the map. The complete rules are in the guide every agent reads first (the MCP tool platform_guide), and the demos show each of them in a working app.
The smallest app#
An app is C# that returns a GenHTTP handler. lambda.cs holds statements, like a top-level program, and whatever it returns is served:
C#
return Inline.Create()
.Get(() => new Greeting("Hello from my app!"));
record Greeting(string Text); // routes return records, not anonymous types
The GenHTTP modules - layouts, webservices, controllers, websockets, server-sent events, OpenAPI, authentication and more - are imported in every file, so no using is needed for them.
The files of a version#
| Path | Is |
|---|---|
lambda.cs |
the top-level code: it returns the handler |
any other *.cs, in any folder |
types, compiled with it |
resources/ |
what the app serves and reads while it runs: pages, scripts, styles, pictures, database migrations |
docs/, tests/ |
its documentation and tests |
| anything else | kept with the version, never compiled or served |
In a git clone, lambda.cs is Project.cs, its statements the body of BuildAsync() - see Working with git.
A typical app serves an API and a front end:
C#
return Layout.Create()
.AddService("api/tasks", new TaskService())
.Add(Resources.App("web")); // resources/web/ as a single page application
Link relatively everywhere - in pages, scripts, fetch, redirects: api/tasks, app.css, never /api/tasks. The same app answers below /lambda/<key>/, at a domain of its own and below a draft’s /features/…/.
What the platform hands the code#
Four names work in every file:
| Name | For |
|---|---|
Resources |
the version’s resources/: Resources.App("web"), Resources.Files("media"), Resources.ReadText("data/terms.md") |
Database |
the app’s own database: await Database.GetConnectionAsync() |
Workspace |
its files as data: await Workspace.WriteBytesAsync("uploads/a.jpg", bytes), Workspace.Files("uploads") |
Secret |
its keys and passwords: await Secret.ReadAsync("WEATHER_API_KEY"), Secret.ExistsAsync("…") |
What reaches the app’s data is awaited, and routes may be async. The plain calls of code written before - Database.GetConnection(), Workspace.WriteBytes(…), Secret.Read(…) - still work, and hold the request’s thread while the data is fetched from wherever the installation keeps it.
Records in the database#
The schema is SQL migrations in resources/migrations/, applied by Evolve before anything is served; records go through Entity Framework Core on the connection the platform hands out, one context per request:
resources/migrations/V1__Create_notes.sql
CREATE TABLE notes (id TEXT NOT NULL PRIMARY KEY, text TEXT NOT NULL);
lambda.cs
await using (var connection = await Database.GetConnectionAsync())
{
new Evolve(connection) { Locations = [Resources.Root + "migrations"], IsEraseDisabled = true }.Migrate();
}
return Inline.Create().Get("count", async () =>
{
await using var db = new Notes(await Database.GetConnectionAsync());
return await db.Entries.CountAsync();
});
public class Note
{
public string Id { get; set; } = "";
public string Text { get; set; } = "";
}
public class Notes(DbConnection connection) : DbContext
{
public DbSet<Note> Entries => Set<Note>();
protected override void OnConfiguring(DbContextOptionsBuilder options) => options.UseDatabase(connection);
protected override void OnModelCreating(ModelBuilder model) => model.Entity<Note>().ToTable("notes");
}
- Never change a migration that was applied; a change of the schema is the next file (
V2__Add_done.sql). Prefer adding over renaming or dropping: a rollback runs older code on the newer schema. - Await Entity Framework -
ToListAsync,SaveChangesAsync- in routes that are async. - Write SQL both SQLite and PostgreSQL accept (
TEXT,INTEGER,BOOLEAN, keys the code makes), so the app runs on either.
Rules it is compiled under#
All apps share one process, so the compiler refuses what would reach beyond an app:
- the file system outside
WorkspaceandResources, processes, the environment, reflection anddynamic; - waiting for a task:
.Result,.Wait(),.GetAwaiter().GetResult(),Task.WaitAll- await it instead; - connections of its own to a database, and Entity Framework’s
EnsureCreatedand migrations; - connecting to other servers, until the app may - see Connecting to other servers.
A refusal comes back as a compiler error with file, line and what to do instead. These checks raise the cost of misbehaving; they are not a sandbox.
Live pages without polling#
A page that shows what changes while it is open - a poll, a dashboard, a game - is pushed the change by the server instead of asking on a timer: server-sent events (EventSource.Create() on the server, new EventSource("events") in the browser) when it only listens, a websocket when it talks back. demo-live and demo-game show both.
Tools for agents, and an API description#
An app can offer tools to AI agents over MCP - Tools.Create() with a class whose methods are marked [Tool] - and describe its API as OpenAPI with .AddOpenApi() and a page to try it with .AddScalar(). demo-crud does both: an agent connected to its mcp path keeps its task list.
Runs anywhere#
Download as a .NET project in the editor gives an ordinary project: dotnet run serves it on port 8080, and its Dockerfile builds an image. The database comes along as a SQLite file (none where the installation keeps databases in PostgreSQL), and secrets are read from environment variables of the same name. A git clone is the same project without any data. See Domains, showcase and source.