# Create function

URL: https://docs.hanzo.ai/docs/openapi/function/post-function

> Publishes a serverless function under the caller's org and answers 201 with it.

`POST /v1/function`

> [Function →](https://docs.hanzo.ai/docs/openapi/function) · [All API references →](https://docs.hanzo.ai/docs/openapi) · [Get started →](https://docs.hanzo.ai/docs/guides)

|               |                                        |
| ------------- | -------------------------------------- |
| **Address**   | `https://api.hanzo.ai/v1/function`     |
| **Method**    | `POST`                                 |
| **Operation** | `post_function`                        |
| **Auth**      | `Authorization: Bearer $HANZO_API_KEY` |

Publishes a serverless function under the caller's org and answers 201
with it.

The name is the key and is claimed once; the names that would shadow a
collection route are reserved. runtime and environment are the same field —
either spelling is accepted — and default to node.

Bounds are clamped rather than refused where a clamp is honest: a timeout above
the 900-second ceiling becomes the ceiling instead of silently reverting to the
30-second default, and an omitted memory limit becomes 256Mi. target=fleet runs
on the org's own GPU fleet and supports runtime=python only.

Requires a validated principal; the function is owned by that principal's org.

## Request

11 fields, body `application/json` (required).

| Field         | In   | Type            | Required | Description                                                                                                                                 |
| ------------- | ---- | --------------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------- |
| `code`        | body | string          | —        | Code is the source to run, capped so one function cannot amplify the store.                                                                 |
| `envNames`    | body | string\[]       | —        | EnvNames are the secret NAMES to mount.                                                                                                     |
| `environment` | body | string          | —        | Environment is a second spelling of runtime, accepted so a console that says "environment" needs no translation.                            |
| `handler`     | body | string          | —        | Handler is the entry point within the code.                                                                                                 |
| `image`       | body | string          | —        | Image names a prebuilt image to run instead of source.                                                                                      |
| `memoryLimit` | body | string          | —        | MemoryLimit is the memory the function runs with, defaulting to 256Mi.                                                                      |
| `name`        | body | string          | yes      | Name is the function's org-unique handle and the segment that addresses it, matching ^\[A-Za-z0-9]\[A-Za-z0-9.\_-]{0,63}$.                  |
| `namespace`   | body | string          | —        | Namespace groups functions for display.                                                                                                     |
| `runtime`     | body | string          | —        | Runtime is the language the code runs under: node, python or deno.                                                                          |
| `target`      | body | string          | —        | Target is where the function runs: sandbox (the default) or fleet, the org's own GPU fleet.                                                 |
| `timeoutSec`  | body | integer (int64) | —        | TimeoutSec is the per-invocation deadline, defaulting to 30 and clamped at 900 — a larger value is capped rather than reset to the default. |

## Response

| Status    | Body                  | Meaning |
| --------- | --------------------- | ------- |
| `201`     | function.functionView | created |
| `default` | problem-details       | refused |

`201` body — 16 fields.

| Field            | In   | Type            | Always | Description                                                           |
| ---------------- | ---- | --------------- | ------ | --------------------------------------------------------------------- |
| `avgDurationMs`  | body | number (double) | —      | mean wall-clock of those runs                                         |
| `createdAt`      | body | string          | —      | when it was first published                                           |
| `endpoint`       | body | string          | —      | the path that invokes it                                              |
| `envCount`       | body | integer (int64) | —      | how many secret NAMES it mounts; values are never carried             |
| `environment`    | body | string          | —      | the language it runs under                                            |
| `errors7d`       | body | integer (int64) | —      | how many of those runs failed                                         |
| `image`          | body | string          | —      | the prebuilt image it runs, when it runs one instead of source        |
| `invocations7d`  | body | integer (int64) | —      | runs in the last 7 days; ABSENT, never 0, when it has not run         |
| `lastDeployedAt` | body | string          | —      | when its code last changed                                            |
| `memoryLimit`    | body | string          | —      | the memory it runs with, and the multiplier on its compute charge     |
| `name`           | body | string          | —      | the function's org-unique handle                                      |
| `namespace`      | body | string          | —      | the display group it belongs to; the org is the isolation key         |
| `status`         | body | string          | —      | whether it is ready to serve                                          |
| `successRate`    | body | number (double) | —      | share of those runs that succeeded, 0..1                              |
| `target`         | body | string          | —      | where it runs: empty for the sandbox, "fleet" for the org's GPU fleet |
| `timeoutSec`     | body | integer (int64) | —      | its per-invocation deadline                                           |

Failure carries the platform error shape — see [Errors](https://docs.hanzo.ai/docs/errors).

## Examples

**CLI**

```bash
hanzo function create --name <name>
```

**SDK**

**.ts**

```ts
import { Configuration, FunctionApi } from 'hanzoai';

const api = new FunctionApi(new Configuration({ accessToken: process.env.HANZO_API_KEY }));
const { data } = await api.postFunction({ name: "<name>" });
```

**.py**

```python
from hanzoai.cloud import ApiClient, Configuration
from hanzoai.cloud.api import FunctionApi

client = ApiClient(Configuration(access_token=os.environ["HANZO_API_KEY"]))
result = FunctionApi(client).post_function(name="<name>")
```

**.go**

```go
cfg := hanzoai.NewConfiguration()
cfg.AddDefaultHeader("Authorization", "Bearer "+os.Getenv("HANZO_API_KEY"))
client := hanzoai.NewAPIClient(cfg)

resp, _, err := client.FunctionAPI.PostFunction(context.Background()).Execute()
if err != nil {
	return err
}
```

**.rs**

```rust
use hanzo_client::apis::{configuration::Configuration, function_api};

let mut cfg = Configuration::new();
cfg.bearer_access_token = std::env::var("HANZO_API_KEY").ok();

let result = function_api::post_function(&cfg, Default::default()).await?;
```

**.java**

```java
import ai.hanzo.cloud.ApiClient;
import ai.hanzo.cloud.api.FunctionApi;

ApiClient client = new ApiClient();
client.setBearerToken(System.getenv("HANZO_API_KEY"));

var result = new FunctionApi(client).postFunction();
```

**HTTP**

```bash
curl -X POST https://api.hanzo.ai/v1/function \
  -H "Authorization: Bearer $HANZO_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
       "name": "<name>"
     }'
```

**MCP**

MCP reaches **function** through the `function` tool, which names its 10 operations with its own verbs — this one among them, under a name only MCP declares. `describe` explains any of them:

```bash
curl -X POST https://api.hanzo.ai/v1/mcp \
  -H "Content-Type: application/json" \
  -d '{
       "jsonrpc": "2.0",
       "id": 1,
       "method": "tools/call",
       "params": {
         "name": "describe",
         "arguments": {
           "op": "list_functions"
         }
       }
     }'
```

---

[Function API](https://docs.hanzo.ai/docs/openapi/function) · [All Hanzo APIs](https://docs.hanzo.ai/docs/openapi) · [Interactive reference](https://docs.hanzo.ai/docs/openapi/reference)
