# Create function

> Create a new Zavu Function. The function starts in draft status. A dedicated API key is auto-provisioned and injected as the ZAVU_API_KEY secret so the function can call back into the Zavu API without manual setup.

`POST https://api.zavu.dev/v1/functions`

Create a new Zavu Function. The function starts in `draft` status. A dedicated API key is auto-provisioned and injected as the `ZAVU_API_KEY` secret so the function can call back into the Zavu API without manual setup.

Provide `sourceCode` to seed the draft. Call `POST /v1/functions/{functionId}/deploy` afterwards to publish.

## Body

- `slug` (string, required): URL-safe identifier (lowercase, digits, hyphens). Must be unique per project.
- `name` (string, required)
- `description` (string)
- `runtime` (string): Runtime the function is deployed on. One of: `nodejs24`.
- `timeoutSec` (integer): Per-invocation timeout in seconds. Event and cron invocations are asynchronous, so a long timeout only bounds cost; a tool called during a live conversation holds up the reply, and a function exposed over HTTP is additionally bounded by the platform's HTTP response limit.
- `memoryMb` (integer) One of: `128`, `256`, `512`, `1024`.
- `httpEnabled` (boolean): Whether to expose a public HTTPS URL for this function.
- `files` (object): The project's source files, keyed by path relative to the project root (e.g. `index.ts`, `lib/orders.ts`). Imports between them are resolved when the function is built, so a function can be split across as many files as it needs. Paths must be relative and use forward slashes; `..`, `node_modules/` and `package.json` are rejected. npm packages are not uploaded here — declare them under `dependencies` and Zavu installs them. Limits: 200 files and 900,000 bytes for the whole tree.
  - `{key}` (string)
- `entrypoint` (string): Which file in `files` is the entry point. Defaults to `index.ts`.
- `sourceCode` (string): Shortcut for a single-file function: exactly equivalent to sending `files` with one entry named after `entrypoint` (`index.ts` by default). Fully supported — use whichever fits. If both are sent, `files` wins.
- `dependencies` (object): npm dependencies. Keys are package names, values are semver ranges.
  - `{key}` (string)

## Responses

### 201

Function created.

- `function` (object, required): A Zavu Function — user-supplied TypeScript that runs in Zavu Cloud and reacts to messaging events or HTTP requests.
  - `id` (string, required)
  - `slug` (string, required): URL-safe identifier, unique per project.
  - `name` (string, required)
  - `description` (string, nullable)
  - `runtime` (string, required): Runtime the function is deployed on. One of: `nodejs24`.
  - `status` (string, required): Lifecycle status of a Zavu Function. One of: `draft`, `bundling`, `deploying`, `active`, `failed`, `disabled`.
  - `timeoutSec` (integer, required): Per-invocation timeout in seconds.
  - `memoryMb` (integer, required): Memory allocation in MB.
  - `httpEnabled` (boolean, required): Whether the function can be invoked over HTTPS via its public URL.
  - `publicUrl` (string<uri>, nullable): HTTPS endpoint, present only while httpEnabled is true. Null otherwise, including for a function that was previously exposed — the stored URL stops serving the moment HTTP is turned off, so it is never returned.
  - `dependencies` (object, required): npm dependencies installed in the function bundle. Keys are package names, values are semver ranges.
    - `{key}` (string)
  - `activeDeploymentId` (string, nullable): ID of the deployment currently serving traffic.
  - `createdAt` (string<date-time>, required)
  - `updatedAt` (string<date-time>, required)

### 400

Invalid request (e.g. duplicate slug, invalid dependencies).

- `code` (string, required)
- `message` (string, required)
- `details` (object)

### 401

Unauthorized.

- `code` (string, required)
- `message` (string, required)
- `details` (object)

### 409

A function with this `slug` already exists in the project. Slugs are unique per project.

- `code` (string, required)
- `message` (string, required)
- `details` (object)

## Example

```bash
curl -X POST "https://api.zavu.dev/v1/functions" \
  -H "Authorization: Bearer $ZAVUDEV_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "slug": "order-bot",
  "name": "Order Bot",
  "description": "Replies to order status questions on WhatsApp.",
  "sourceCode": "import { defineFunction } from '\''@zavudev/functions'\'';\n\nexport default defineFunction(async (event, ctx) => {\n  ctx.log('\''received'\'', event.type);\n});\n",
  "dependencies": {
    "openai": "^4.20.0"
  }
}'
```

```typescript
import Zavudev from "@zavudev/sdk";

const zavu = new Zavudev({ apiKey: process.env.ZAVUDEV_API_KEY });

const result = await zavu.functions.create({
  slug: "order-bot",
  name: "Order Bot",
  description: "Replies to order status questions on WhatsApp.",
  sourceCode: "import { defineFunction } from '@zavudev/functions';\n\nexport default defineFunction(async (event, ctx) => {\n  ctx.log('received', event.type);\n});\n",
  dependencies: {
    openai: "^4.20.0",
  },
});

console.log(result);
```
