# Functions CLI SDK

Functions CLI is a tool that allows to develop and deploy [Functions](/docs/functions) to Jitsu.

## Installing Jitsu CLI

To download and install Jitsu CLI, run the following command:

<Tabs>
  <TabItem value="npm" label="npm" default>

```bash
npm i -g jitsu-cli
```
  </TabItem>
  <TabItem value="yarn" label="yarn">

```bash
yarn global add jitsu-cli
```
  </TabItem>
  <TabItem value="pnpm" label="pnpm">

```bash
pnpm i -g jitsu-cli
```
  </TabItem>
</Tabs>

## Creating a new project

Run the following command to initialize the project

```bash
jitsu-cli init
```
arguments:
* `--name` – the name of the project. (Optional). By default, interactive prompt is shown to enter the name.
* `--displayname` – human-readable function name that will be used in Jitsu. (Optional). By default, interactive prompt is shown to enter the name.
* `--parent` – the parent directory of project. (Optional). By default, interactive prompt is shown to enter the parent directory.

### Project structure

```shell
myfunc
├── src
│   ├── __tests__
│   │   └── functions
│   │       └── myfunc.test.ts
│   ├── functions
│   │   └── myfunc.ts 
│   └── profiles
│       └── profile.ts
├── tsconfig.json
└── package.json
```

## Development

Use following commands to develop and test your functions locally:

**Install dependencies**
```bash
cd myfunc # go to the project directory
npm install # install dependencies with 'npm' or any other compatible package manager
```

**Build functions**
```bash
jitsu-cli build
```

**Run tests**
```bash
jitsu-cli test
```
Test provided by project template is a simple test that checks that function doesn't crash and returns some event.
You may need to extend test to check that function returns correct result.

[//]: # (### Check function on custom event)

[//]: # ()
[//]: # (You can copy any incoming event from `Data` – `Live Events` section in Jitsu UI and use it to test your function locally.)

[//]: # ()
[//]: # (To check function on provided event, config or persistent storage state run:)

[//]: # (```bash)

[//]: # (jitsu-cli run --event '{"type": "pageview", ...}' --props '{}' --store '{"key1": "value1"}')

[//]: # (```)

[//]: # (or:)

[//]: # (```bash)

[//]: # (jitsu-cli run --event ./event.json --props ./config.json --store ./state.json)

[//]: # (```)

[//]: # (arguments:)

[//]: # (* `--name` – name of function to check &#40;optional&#41;. Required if multiple functions are defined in project)

[//]: # (* `--type` – entity type to run &#40;optional&#41;. Default: `function`)

[//]: # (* `--event` – path to file with event json or event json as a string)

[//]: # (* `--props` – path to file with config json or config json as a string &#40;optional&#41;)

[//]: # (* `--store` – path to file with state json or state json as a string &#40;optional&#41;)

[//]: # ()
[//]: # (Command outputs function result.)

## Adding Functions to Jitsu

**Login to Jitsu (Run once)**
```bash
jitsu-cli login
```
arguments:
* `--host` – Jitsu host (optional). Default: `https://use.jitsu.com`
* `--apikey` – Jitsu user's Api Key (optional). Disables interactive login.

`login` command remembers credentials in `~/.jitsu/jitsu-cli.json` file.


**Deploy functions to Jitsu project**
```bash
jitsu-cli deploy
```
arguments:
* `--workspace` – Id of workspace where to deploy function (Optional). By default, interactive prompt is shown to select workspace
* `--type` – entity type to deploy (optional). Default: `function`

Deploy command creates new functions or updates existing functions in Jitsu project.