# snizu function init

Source: https://snizu.dev/docs/cli/function/init

## Usage [#usage]

```sh title="Usage"
snizu function init <dir> --target discount|delivery|payment|routing --lang rust|js [--title "…"]
```

## Flags [#flags]

<Fields>
<Field name="<dir>" type="path" required>
Where to write it. Made when it doesn't exist.
</Field>

<Field name="--target" type="discount | delivery | payment | routing" required>
What it changes: discounts on the cart, the delivery options shown, the payment methods shown, or the branch an order ships from.
</Field>

<Field name="--lang" type="rust | js" required>
Rust, built with Cargo, or JavaScript, built with Javy.
</Field>

<Field name="--title" type="text">
Its name in the admin. The folder's name by default.
</Field>
</Fields>

## What it writes [#what-it-writes]

<Fields>
<Field name="snizu.function.json">
Its title, target and language, which the other `function` commands read.
</Field>

<Field name="sample.json">
An input its target reads: an Egyptian cart in EGP, with the delivery options, payment methods or branches the target gets.
</Field>

<Field name="src/main.rs, Cargo.toml">
For Rust: `serde_json`, and a release profile that keeps the module small.
</Field>

<Field name="index.js">
For JavaScript: reads standard input with Javy and writes the answer.
</Field>
</Fields>

## How a function runs [#how-a-function-runs]

A function reads JSON on standard input and writes JSON on standard output. It has no network, files, clock or randomness, so everything it needs is in its input. Amounts are in minor units: `subtotal: 30000` is 300.00 EGP.

The discount function it writes takes 10% off orders over 200.00:

```js title="index.js"
function run(input) {
  // 10% off an order over 200.00.
  const discounts = input.cart.subtotal >= 20000
    ? [{ title: "10% off", value: { percentage: 10 }, target: "order" }]
    : [];
  return { discounts };
}
```

The other targets start with `{ "operations": [] }`, which changes nothing: hide, rename or move options, methods or branches by their `id` there.

## Examples [#examples]

```shellsession title="Terminal"
$ snizu function init big-basket --target discount --lang js
✓ Wrote a discount function in /home/salma/big-basket.
Next: snizu function build, then snizu function run --input sample.json
```