Quick Start
greetings.stof
#[main]
fn welcome() {
pln('Lets do cool Stof together!');
}Output
This guide introduces the concept of a data runtime and shows you how to get started using TypeScript in under 5 minutes.
Prerequisites
- Bun installed (
curl -fsSL https://bun.sh/install | bash) - Node.js also works - replace
bun runwithnpx tsxthroughout
Targeting a browser instead?
This guide runs in Bun/Node, where Stof's WASM loads automatically. Building for a browser or a bundler like Vite or webpack instead? See Initializing in a Browser or Bundler on the Install page first.
Install
mkdir stof-quickstart && cd stof-quickstart
bun init -y
bun add @formata/stof
Or, NPM works just fine:
npm i @formata/stof
Hello, World! (JSON-style)
Create index.ts:
index.ts
import { stofAsync } from '@formata/stof';
const doc = await stofAsync`
{
"name": "Stof"
"hello": ():str => \`Hello, \${self.name}!\`
}`;
console.log(await doc.call('hello'));
bun run index.ts
Hello, Stof!
Hello, World!
Functions are data components in Stof, just like fields! This index.ts is the same as the one above.
index.ts
import { stofAsync } from '@formata/stof';
const doc = await stofAsync`
name: "Stof"
fn hello() -> str {
\`Hello, \${self.name}!\`
}`;
console.log(await doc.call('hello'));
bun run index.ts
Hello, Stof!
Data Runtime
Modern data contexts are dynamic and travel. A data runtime gives you a living interchange layer that stays sandboxed & reliable between services.
index.ts
import { StofDoc } from '@formata/stof';
// add data however its already defined (JSON, YAML, TOML, Stof, Binary, etc.)
const doc = await StofDoc.parse({
name: 'John Doe',
email: 'john@example.com',
age: 42
});
// sandboxed, can only see & manipulate itself
const api = `
fn set_height(height?: cm) {
height = height ?? (6ft + 2in) as cm;
self.height = height.round(2);
}
fn first_name() -> str {
let names = self.name.split(' ');
const first = names[0];
self.first = first;
first
}`;
doc.parse(api);
await doc.call('set_height');
await doc.call('first_name');
console.log(doc.record()); // back out as a JS object
bun run index.ts
{
name: "John Doe",
email: "john@example.com",
age: 42,
height: 187.96,
first: "John",
}