# Deploy function

> Publish the function. If sourceCode or dependencies are provided in the body, they replace the current draft before deployment. Returns immediately with a deployment ID — poll GET /v1/functions/deployments/{deploymentId} until status is active or failed.

`POST https://api.zavu.dev/v1/functions/{functionId}/deploy`

Publish the function. If `sourceCode` or `dependencies` are provided in the body, they replace the current draft before deployment. Returns immediately with a deployment ID — poll `GET /v1/functions/deployments/{deploymentId}` until status is `active` or `failed`.

## Parameters

- `functionId` (path, string, required): Zavu Function ID.

## Body

- `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): New dependency map (replaces existing dependencies).
  - `{key}` (string)

## Responses

### 202

Deployment queued.

- `deployment` (object, required)
  - `id` (string, required)
  - `functionId` (string, required)
  - `version` (integer, required): Monotonically increasing deployment version, starting at 1.
  - `status` (string, required): Stage of a function deployment. One of: `pending`, `bundling`, `uploading`, `publishing`, `active`, `failed`, `superseded`.
  - `sourceCodeBytes` (integer, nullable): Total size of the deployed source tree in bytes.
  - `bundleBytes` (integer, nullable): Size of the built bundle in bytes. Null until the build finishes.
  - `errorMessage` (string, nullable): Failure reason when status is 'failed'.
  - `buildLogs` (string, nullable): What the build printed: dependency installation, the bundler's output, and the compiler's message when it failed. Returned when fetching a single deployment, omitted from the list. Read this first when a deploy fails — `errorMessage` is often the outer wrapper's summary, and the line that names the broken import or the syntax error is here.
  - `deployedAt` (string<date-time>, nullable)
  - `createdAt` (string<date-time>, required)

### 400

Invalid request or function in a non-deployable state.

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

### 401

Unauthorized.

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

### 404

Function not found.

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

## Example

```bash
curl -X POST "https://api.zavu.dev/v1/functions/function_abc123/deploy" \
  -H "Authorization: Bearer $ZAVUDEV_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "files": {
    "index.ts": "import { formatOrder } from '\''./lib/orders'\'';\n\nexport default async function handler(event) {\n  return { statusCode: 200, body: formatOrder(event) };\n}\n",
    "lib/orders.ts": "export function formatOrder(event) {\n  return JSON.stringify(event);\n}\n"
  }
}'
```

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

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

const result = await zavu.functions.deploy("function_abc123", {
  files: {
    "index.ts": "import { formatOrder } from './lib/orders';\n\nexport default async function handler(event) {\n  return { statusCode: 200, body: formatOrder(event) };\n}\n",
    "lib/orders.ts": "export function formatOrder(event) {\n  return JSON.stringify(event);\n}\n",
  },
});

console.log(result);
```
