KScript docs
EN

Projects and kscript.json

Starting a project: kscript make project

kscript make project sets up a new web app, with the same app as a desktop window, and everything wired together:

Shell
kscript make project my-app --port 3000
cd my-app
kscript serve            # http://localhost:3000, from kscript.json
kscript desktop.kscript  # the desktop app, in a window
kscript compile          # build it as a program: dist/mac-arm64/My App.app, dist/win-x64/My App.exe...
kscript test             # runs the tests in tests/
kscript check            # checks every page and script in the project
kscript fmt              # tidies every script and page in the project

What it makes:

File What it is
kscript.json Settings for serve, check, test and, with the desktop app, the app section kscript compile reads. Its config section holds the app's own settings, read as $config.
index.html, about.md, _layout.html, _components/site-nav.kscript, site.css The site: two pages, the layout they share and a navigation component.
tasks.html A to-do list in a <live> block, with no JavaScript.
lib/tasks.kscript The to-do rules (add, tick, remove, clear, count), shared by the page, the desktop app, the API and the tests.
desktop.kscript The same to-do list as a desktop app: a Window { } block.
routes/api.kscript, controllers/tasks.kscript A JSON API for the tasks under /api (KScript.Http). While kscript serve runs, its docs are at /_api (OpenAPI).
lib/greeting.kscript, scripts/hello.kscript A function and a command-line script that uses it.
tests/tasks.test.kscript, tests/greeting.test.kscript Tests for the rules and the script, run with kscript test.
.vscode/extensions.json, .gitignore Asks VS Code to install the KScript extension; keeps built apps (dist/) out of git.
AGENTS.md, CLAUDE.md, .kscript/agents/ Docs for coding agents, made from the project's code and kept up to date by the editor (see Coding agents).

Scripts, tests, routes and the desktop app are written in the .kscript format. --syntax xml writes them as XML (.ks.xml) instead, and records "make": { "syntax": "xml" } in kscript.json so that later kscript make script and kscript make test follow suit. Pages stay HTML either way.

--no-desktop leaves out desktop.kscript and the app section, for a web site only. --no-http leaves out the API. kscript make project . sets up the current folder; existing files are never replaced without --force. kscript make project --help lists the template's options.

A bare project: --bare

kscript make project my-tool --bare makes just kscript.json, main.kscript (a script) and tests/main.test.kscript, plus the VS Code recommendation and the agent docs (AGENTS.md): no pages, desktop app or API. Use it for command-line tools and scripts. --syntax xml works here too. --bare is short for --template bare; kscript make project --help --bare lists its options.

Add pieces to a bare project with options, in any mix:

CommandWhat it does
kscript make project my-app --bare --web --apiAn empty website and JSON API.
kscript make project my-api --bare --apiJust the API: routes/api.kscript, controllers/hello.kscript and its test.
kscript make project my-app --bare --desktopA script plus an empty desktop app (a Window block)

--web adds index.html, _layout.html and site.css; --api adds routes/api.kscript (a Routes file served under /api), controllers/hello.kscript and tests/hello.test.kscript; --desktop adds desktop.kscript and the app section of kscript.json. kscript.json gets serve settings (and --port) whenever there is a website or API.

An API: --template api

kscript make project shop --template api makes a JSON API with nothing else to set up: routes, controllers, the app's own middleware, a model in a database, sign-up with API tokens, caching and tests.

Shell
kscript make project shop --template api
cd shop
kscript serve      # http://localhost:8080/api/health, and the API's docs at /_api
kscript test       # 12 tests, against a database in memory
File What it is
routes/api.kscript The routes, under /api: GET /health, POST /register and /tokens (throttled), GET and DELETE /me, and a Resource for /notes behind the auth middleware.
controllers/notes.kscript The notes resource: index, store, show, update and destroy, each answering with JSON. The NoteInput type it imports checks what is posted (422 when it doesn't fit), and a note that isn't there (or isn't yours) is a 404.
controllers/accounts.kscript Sign-up, tokens and closing an account, with KScript.Auth.
models/note.kscript The Note model, whose table is made the first time it is used.
requests/note-input.kscript, requests/sign-up.kscript, requests/sign-in.kscript The typed request bodies, one type per file, that the controllers import.
middleware/request-id.kscript The app's own middleware: every response gets an X-Request-Id header.
lib/cache.kscript The app's cache, whose store and time limit come from cache in kscript.json.
kscript.json, kscript.test.json, config.kscript The app's settings: the database and cache in kscript.json's config section, an in-memory database for tests in kscript.test.json, and config.kscript for the ones read from environment variables (DATABASE_URL, CACHE_STORE).
tests/ Tests that call the API the way a client would, with @api(/api/notes) (see Calling your own API).

A web app with its API: --template fullstack

kscript make project my-site --template fullstack makes a site with a home page, layout, API health endpoint and a test for that endpoint. Choose the database, sign-in, caching and Docker options for your project.

Pass --examples or select Include the Notes example? in the wizard to add a live Notes page, a CRUD API and their tests. The page calls the API through lib/notes-client.kscript with @api(/api/notes).

Database, sign-in, cache and Docker

The api and fullstack templates share these options:

Option Default What it does
--db sqlite|postgres|mysql|none sqlite The database configured in kscript.json's config section. With none, the Notes example uses a JSON file (notes.file in the config). With postgres or mysql the app connects to that server (DATABASE_URL overrides the url, and --docker adds it to the compose file), while kscript test still uses SQLite in memory.
--auth, --no-auth on (api), off (fullstack) Sign-in. In the api template: POST /api/register, /api/tokens, GET /api/me, and notes that belong to who is signed in. In the fullstack template it also runs kscript make auth for the sign-in pages; the pages call the API as whoever is viewing them.
--cache, --no-cache on Adds cache configuration and helpers. The Notes example uses them to cache the list and answer repeat requests with 304 Not Modified.
--docker off Adds a Dockerfile, .dockerignore and compose.yaml, with a Postgres or MySQL service for those databases, and make up / make down.
--port 8080 The port kscript serve uses.

Options can be written --db postgres or --db=postgres, and a yes/no option takes yes/no (--cache no) but never the folder name, so kscript make project --docker shop makes shop.

Adding Docker to an existing site

Run kscript make docker anywhere in a project. It adds Dockerfile, compose.yaml and .dockerignore beside kscript.json. Preview the files with --dry-run. If any output already exists, the command stops before writing. Use --force to replace those files. The same kit is available when creating a bare site.

Shell
kscript make project portal --bare --web --api --port 4820 --docker -n
cd portal
docker compose up --build

Open http://localhost:4820. The container serves the project's configured serve.root and uses serve.port. If you change the port, regenerate the kit or update the published port, EXPOSE and health check together.

Compose 2.17+ and BuildKit build KScript for Linux ARM64 or AMD64 from the released CLI's Git revision, using pinned multi-architecture .NET base images. A development build requires a source checkout. For a modified or unpublished KScript build, set KSCRIPT_SOURCE to its checkout.

Shell
KSCRIPT_SOURCE=/absolute/path/to/kscript docker compose up --build

The app runs as an unprivileged user with a read-only application directory. Compose keeps KScript.Storage, authentication state and project-relative SQLite files in the app-state volume at /var/lib/kscript. The volume survives container replacement. KSCRIPT_PROJECT_DATA_ROOT places configured relative auth and SQLite paths under that folder. For an absolute path or a separate database server, configure the corresponding mount or connection for your deployment. Restart kscript serve when changing storage paths so it refreshes the private files excluded from public serving. Secrets can be supplied as runtime environment variables. The generated .dockerignore excludes dotfiles, common credential files, local databases and runtime state. Add any project-specific secret filenames before building.

Where a container keeps its data

The generated Dockerfile sets two folders on the app-state volume:

Variable Set to What it holds
KSCRIPT_STORAGE_ROOT /var/lib/kscript/storage Every app's app folders: KScript.Auth users, sessions and API tokens kept in its default folder, @storage() files and the file cache
KSCRIPT_PROJECT_DATA_ROOT /var/lib/kscript/project Relative paths in kscript.json for auth data (http.auth.folder) and SQLite databases, which would otherwise be inside the read-only /app

Both need to be on a volume. Without one, everything in them is lost each time the container is recreated, which happens on every docker compose up --build. kscript serve in a container prints a warning when either variable is missing.

Docker files from before 0.27

Docker files made by an earlier kscript make docker mount only app-data:/app/data, so users and sessions stay in the container's home folder and every account is lost when the container is recreated. To keep your own changes to those files, such as an added Postgres service, update them by hand instead of using --force:

dockerfile
# Dockerfile: make the folder, owned by the app's user, and point KScript at it
RUN install -d -o app -g app -m 700 /var/lib/kscript
ENV KSCRIPT_STORAGE_ROOT=/var/lib/kscript/storage KSCRIPT_PROJECT_DATA_ROOT=/var/lib/kscript/project
YAML
# compose.yaml
services:
  app:
    volumes:
      - app-state:/var/lib/kscript
volumes:
  app-state:

Accounts made before the change are inside the old container and go when it is replaced. To keep them, copy them out of the running container first: docker compose cp app:/home/app/.local ./kscript-backup. Each app's files are in share/<app> and state/<app> there. Once the new container is up, copy them into /var/lib/kscript/storage/<App name>/data and /var/lib/kscript/storage/<App name>/state.

Running the development site in Docker

Run kscript serve in a local project with a Compose file to start its app service. Use --no-docker to serve on the host. It bind-mounts the project read-only at /app and defaults to the development environment. Open the site and edit pages, styles, or route files locally. The browser reloads when the container sees the edits. Use --no-reload to refresh manually. Restart the service after changing startup configuration or image dependencies. Existing Compose data volumes still apply. For a named or anonymous volume mounted under the source directory, such as /app/data, KScript creates the local mountpoint directory before starting Compose.

Use serve.docker in kscript.json to configure local Docker serving.

JSON
{
  "serve": {
    "docker": {
      "compose_file": "compose.yaml",
      "service": "app",
      "mount": "/app",
      "build": true
    }
  }
}

"docker": "auto" detects a Compose file during local serving and is the default. Set "docker": true to require Compose locally or "docker": false to require --docker. With the object form, use enabled: "auto" for detection with custom settings. Host-specific options such as --port select the host in auto mode. Set mount to the app directory in a custom image. Set mount: null to serve the files baked into the image, or build: false to reuse an existing image. Use kscript serve --docker --env test to select test settings. Compose controls the published ports and the service command. Ctrl+C stops the development app container. Its dependencies can remain running, and docker compose down stops them.

Use COMPOSE_PROJECT_NAME=myapp-test kscript serve --docker --env test to put test services and their Compose-managed volumes in a separate project. Use a separate host port in the test Compose file if both sites run at once.

The generated Dockerfiles and kscript serve --docker apply pending migrations to the container's configured database before serving. Startup stops if a migration fails. To apply migrations manually, run docker compose exec app kscript db migrate --force when the service is running, or docker compose run --rm app kscript db migrate --force before startup. Inside Docker, live reload still watches the mounted source. Run kscript agents on the host to refresh generated guidance.

docker compose up --build serves the source copied into the generated image. For a custom image that invokes kscript serve directly, set KSCRIPT_SERVE_IN_DOCKER=1 inside the container so it starts the web server.

The setup wizard

kscript make project at a terminal with no options asks for everything instead: the folder (unless you gave one), the kind of project, and each of that template's options, with its default ready for Enter. Give any option, or -n (--no-interaction), and nothing is asked: what you didn't give takes its default, which is what scripts and CI want. When the input or output isn't a terminal, nothing is asked either.

The Makefile

Every new project has a Makefile, so common commands are short: make serve, make test, make check, make fmt, make agents, make compile, and make routes, make desktop and make app when the project has an API or desktop app. Any other target is passed to kscript make, with name= as the name and args= for options:

Shell
make page name=contact                          # kscript make page contact
make component name=Hero args="--props title"   # kscript make component Hero --props title

make comes with macOS (in the Xcode command line tools) and Linux. On Windows, install it (winget install GnuWin32.Make, scoop install make or choco install make) or run the kscript commands directly. Set KSCRIPT=/path/to/kscript to use another kscript.

Changing what new projects get

Every file kscript make project writes comes from a project template, and you can change it:

CommandWhat it does
kscript make templates projectOutside a project: copies it to ~/.kscript/templates/project.
kscript make templates project --globalThe same, from inside a project.
kscript make templates projectInside a project: copies it to _templates/project.
kscript make templates project bareThe bare template, to project-bare instead of project.
kscript make project my-app --template ../starters/apiAny folder.

--template takes a folder or a template name. A name such as bare (or your own, such as api) is looked up as project-<name> in the same places as the default template, which lives in project, and then among the built-in templates (web, api, bare and fullstack). kscript make project uses the first template it finds: the --template folder, then _templates/project in the project the new one is made inside (handy for a folder of several apps), then ~/.kscript/templates/project (or $KSCRIPT_TEMPLATES/project), then the built-in one. Edit, add or delete files in the template folder to change what new projects get.

  • Write scripts as .kscript files and refer to other scripts with {{ ext }} (import "lib/tasks{{ ext }}"). With --syntax xml, each .kscript file is converted to XML and named .ks.xml, and {{ ext }} is .ks.xml; {{#if xml}} tells the formats apart. A .ks.xml file in the template is copied as it is.
  • Each file is a template with the same {{ }} placeholders as kscript make templates: {{ title }} is the project name as a title (My App), {{ kebab }} is my-app, and every option is a value ({{ port }}, {{#if desktop}} … {{/if}}).
  • A file can start with a --- make header. when: desktop copies it only when the option is on, unless: desktop only when it is off, and to: docs/{{ kebab }}.md writes it somewhere else. when and unless take the same tests as {{#if}}: when: db is none, unless: db in sqlite, none. Two files can write the same path when only one of them is copied (controllers/notes-file.kscript with when: db is none and to: controllers/notes.kscript).
  • A name ending in .stub loses the ending: the template keeps kscript.json.stub so that editors and kscript check don't read it as a real project.
  • kscript-template.json describes the template and is not copied. options lists each option with its default and description (an option that defaults to true is turned off with --no-name), a question for the setup wizard, and choices when it takes one of a few values (--db oracle is then an error that lists them). next holds the lines printed under "Next:", notes lines printed as notes (a line that comes out empty is left out), and make the kits made in the new project once its files are written: "make": ["{{#if auth}}auth{{/if}}"] runs kscript make auth. An option the template doesn't list is reported, so a typo such as --no-dekstop doesn't go unnoticed.
JSON
// _templates/project/kscript-template.json
{
  "description": "Our standard web app",
  "options": {
    "desktop": { "default": true, "description": "Add the desktop app" },
    "api": { "default": false, "description": "Add an API page", "question": "Add an API page?" },
    "db": { "default": "sqlite", "choices": ["sqlite", "none"], "description": "Where data is kept" },
    "port": { "default": 8080, "description": "The port kscript serve uses" }
  },
  "next": ["kscript serve   http://localhost:{{ port }}", "{{#if api}}kscript serve, then open /api{{/if}}"]
}

Edit this page on GitHub