Your first full-stack app
Build a small expense tracker with a server API, a reactive UI and a Chart.js chart from NuGet.
This guide builds Spend Tracker, a small full-stack app where you log expenses and see where your money goes. You will use the main pieces of WebSharper along the way:
- a sitelet that serves the page,
- remote procedure calls (
[<Rpc>]) that let the browser call server code as if it were local, - reactive UI with
Var,ViewandListModel, bound to an HTML template, - a JavaScript library binding from NuGet (
WebSharper.ChartJs) to draw a chart.
![]()
Want to see it first? Try the finished app right here on this page.
WebSharper 11 preview
This guide targets the WebSharper 11 preview, which needs the .NET 11 SDK. You also need Node.js: WebSharper uses npm packages for JavaScript libraries, Vite to serve them while you develop, and esbuild to bundle them for release.
Create the app
Install the WebSharper 11 templates (see Getting started for what each template does), then create a client-server application:
Open the URL printed in the terminal. You get a page with a text box that sends your input to the server and shows the reversed text. That round trip is already a working full-stack app. Here is where its pieces live:
| File | What it holds |
|---|---|
Site.fs | The endpoints (EndPoint) and the server-side page for each one. |
Remoting.fs | Server functions marked [<Rpc>], callable from the browser. |
Client.fs | Code marked [<JavaScript>], compiled to JavaScript and run in the browser. |
Main.html | The HTML template. ws-hole, ws-var, ws-onclick and ${...} mark the places your code fills in. |
wsconfig.json | WebSharper compiler settings (see wsconfig.json). |
Stop the app with Ctrl+C before you continue.
Define the data and the server API
Replace the contents of Remoting.fs with an Expense record and three server functions. The store is in memory, which keeps the guide short; swap it for a database later.
Expense is marked [<JavaScript>] so the browser can use the same type. The [<Rpc>] functions run on the server;
WebSharper generates the client-side stubs and the JSON serialization for their arguments and results.
Lay out the page
Replace the contents of Main.html with the page below, and add a stylesheet for it as wwwroot/site.css
(files in wwwroot are served as they are):
The page keeps the holes the template's Site.fs fills (${Title}, MenuBar, Body and scripts), and adds an
ExpenseForm template for the home page. Each ws-* attribute and ${...} placeholder in it becomes a typed method
on the generated template class: ws-var="Description" becomes .Description(var), ws-onclick="OnAdd" becomes
.OnAdd(handler), and the nested ws-template="Row" becomes its own MainTemplate.Row() class.
Make the list reactive
Replace Client.Main in Client.fs with the code below. Keep the Templates module at the top of the file as it is.
There is no code that updates the table or the total by hand:
- a
Varholds a value and aViewobserves it. The form fields areVars bound withws-var, so typing updates them and setting them (description.Value <- "") clears the input. - a
ListModelis a reactive collection.Doc.BindSeqCachedrenders aRowper item and re-renders only the rows that change. total,count,topCategoryandcanAddare derived views. The Add button is disabled until there is a description and a positive amount.
Finally, in Site.fs, the heading now lives in the template, so make the home page body just the client-side part:
Run the app with dotnet run: you can now add and delete expenses, and the summary cards keep up.
Add a chart binding from NuGet
WebSharper talks to JavaScript libraries through bindings: NuGet packages that describe a library's API in .NET types. Add the Chart.js binding, and Chart.js itself from npm:
The binding imports Chart.js as an ES module (chart.js/auto). In development, Vite serves it from node_modules; in a
Release build, esbuild bundles it into your page's script. Browse the other bindings in the WebSharper bindings catalog.
Draw spending by category
Add a Charts module to Client.fs, between Templates and Client, and open the namespaces it needs at the top of the file:
Then fill the template's Chart hole, just before .Doc() in Client.Main:
This is the general recipe for driving any JavaScript widget from WebSharper's reactive model:
on.afterRendercreates the widget once its element is in the page. Chart.js needs a mounted<canvas>.View.Sinkruns a callback with the current value and again on every change. Here it regroups the expenses, writes the new labels, values and colours into the chart's data, and callschart.Update().
Type = ChartType.Doughnut tells Chart.js which chart type's defaults to use, so the legend lists the categories.
PieDoughnutChartOptions adds the doughnut-specific options, such as the size of the hole (Cutout), and ColorOf gives
each category the same colour as its pill in the table.
Run dotnet run again. Adding or deleting an expense now updates the table, the total and the chart together.
Build for release
A Release build runs npm install and the esbuild step from esbuild.config.mjs, which bundles your client code,
WebSharper's runtime and Chart.js into one script per page under out/wwwroot/Scripts/WebSharper/. Run the published app with:
Try it live
Here is the finished app. This page has no server behind it, so the live version swaps the [<Rpc>] functions of
Remoting.fs for a Server module with the same signatures that keeps the expenses in the browser. The Charts and
Client code is the same as above.
Where to go next
- Sitelets and Routing: add pages, for example a monthly summary at
/summary. - Remoting: more on
[<Rpc>]functions, including how to use the request context and authentication. - Reactive UI and Templating: the full
Var/Viewand template API. - Bundling and wsconfig.json: control how your client code is built and bundled.
- Resources and the Interface Generator: wrap a JavaScript library that has no binding yet.