# Functions

Deploy a handler once and call it by name. Every call runs in a fresh microVM, so one call can never see another's data. Functions are on the Pro plan and up.

**curl**

```
API=https://api.kosa8.com/v1
AUTH="Authorization: Bearer $KOSA8_API_KEY"
JSON='Content-Type: application/json'

# Deploy: code plus a runtime. Each call runs in a fresh microVM.
curl -s -X PUT $API/functions/hello-fn -H "$AUTH" -H "$JSON" -d '{
  "runtime": "python3.12",
  "code": "def handler(event):\n    return {\"greeting\": \"hello \" + event[\"name\"]}\n"
}' | jq '{name, version}'

curl -s $API/functions/hello-fn/invoke -H "$AUTH" -H "$JSON" \
  -d '{"event":{"name":"kosa8"}}' | jq '{ok, result}'

curl -s -X DELETE $API/functions/hello-fn -H "$AUTH"
```

**Python**

```
from kosa8 import Kosa8Cloud

cloud = Kosa8Cloud()

cloud.functions.deploy("hello-fn", {
    "runtime": "python3.12",
    "code": "def handler(event):\n    return {'greeting': 'hello ' + event['name']}\n",
})
out = cloud.functions.invoke("hello-fn", {"name": "kosa8"})   # runs in a fresh microVM
print(out["ok"], out["result"])                                # True {'greeting': 'hello kosa8'}
cloud.functions.delete("hello-fn")
```

**JavaScript**

```
import { Kosa8Cloud } from '@kosa8/sdk';

const cloud = new Kosa8Cloud();

await cloud.functions.deploy('hello-fn', {
  runtime: 'node22',
  code: "exports.handler = async (event) => ({ greeting: 'hello ' + event.name });\n",
});
const out = await cloud.functions.invoke('hello-fn', { name: 'kosa8' }); // a fresh microVM per call
console.log(out.ok, out.result);                                         // true { greeting: 'hello kosa8' }
await cloud.functions.delete('hello-fn');
```

## Deploy

`PUT /v1/functions/{name}` with:

| Field | What it does |
|---|---|
| `runtime` | `python3.12` or `node22` |
| `code` | The source. Python defines `def handler(event)`; Node exports `exports.handler`. Up to 6 MiB. |
| `env` | Environment for every call: up to 64 names (none starting `K8_`), 16 KiB in all |
| `timeout_s` | How long one call may run: default 30 s, up to 90 s |
| `web` | `off` (default), `public` or `key`: also answer plain HTTP requests at a URL, open or behind a key |
| `schedule` | A five-field cron expression (UTC) to call it on a timetable, or `null` |

Deploying again with the same name replaces it and bumps its `version`. Deploying needs a key with `functions:deploy`; calling needs `functions:invoke`.

## Call

`POST /v1/functions/{name}/invoke` with `{"event": …}` answers with `ok`, `result`, logs and timings. `POST /v1/functions/{name}/map` with `{"events": [...]}` calls it on up to 100 events, up to 16 at a time. `GET …/invocations` lists recent calls. Every endpoint is in the [Functions API](https://docs.kosa8.com/api/functions/).

## Functions, one-call runs, or sandboxes?

| You have | Use |
|---|---|
| The same handler, called again and again | A function |
| Some code or a command to run once, maybe with files | [One-call run](https://docs.kosa8.com/guides/cloud-run/), `POST /v1/run` |
| State that lasts between calls, or a long task | A [sandbox](https://docs.kosa8.com/concepts/sandboxes/) |

---
Source: https://docs.kosa8.com/serverless/functions/
