> ## Documentation Index
> Fetch the complete documentation index at: https://platform.kimi.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Vaults

> Store third-party service credentials, bind vaults when creating sessions, and manage credential authorization, rotation, and archiving.

A **vault** in Kimi Hosted Agents stores access credentials for third-party services. When you bind a vault at session creation, Hosted Agents supplies credentials during the session according to each credential's type and matching rules.

Once written, credential secret values are never returned in API responses.

This page is organized into two workflows:

* **Initial setup**: Choose a credential type, create a vault, add credentials, and bind the vault when creating a session.
* **Ongoing management**: Inspect credentials, complete OAuth authorization, rotate credential values, and archive or delete resources.

## Initial setup

### Choose a credential type

Select `type` based on how the third-party service authenticates:

| Use case | Credential type |
| - | - |
| MCP server using OAuth 2.0 | `mcp_oauth` |
| MCP server using a static Bearer token | `static_bearer` |
| Programs reading credentials from environment variables | `environment_variable` |

`mcp_oauth` and `static_bearer` match MCP servers by `mcp_server_url`. `environment_variable` matches environment variable names by `secret_name`. Active credentials within the same vault must have unique matching field values. Each vault can contain at most 20 active credentials.

A credential's `type` and identifying fields cannot be modified after creation. To change the authentication type or matching field, archive the existing credential first and create a new one.

For `environment_variable`, the environment variable inside the sandbox holds a placeholder rather than the real secret. This credential type is designed for injecting credentials into outbound requests—not for validating the secret locally or computing request signatures with it.

### Create a vault

The request below creates a vault to hold access credentials for a single user or business workflow:

```bash cURL theme={null}
VAULT_ID=$(curl --silent --show-error --request POST "https://api.moonshot.ai/v1/vaults" \
  --header "Authorization: Bearer $KIMI_API_KEY" \
  --header "kimi-api-version: 2026-09-01-beta" \
  --header "Content-Type: application/json" \
  --data '{
    "display_name": "alice",
    "metadata": {"external_user_id": "usr_abc123"}
  }' | jq -r '.id')
```

```bash CLI theme={null}
VAULT_ID=$(kha vaults create -o json --file - <<'JSON' | jq -r '.id'
{
  "display_name": "alice",
  "metadata": {"external_user_id": "usr_abc123"}
}
JSON
)
```

`id` in a successful response is the vault ID. Save it as `VAULT_ID` for adding credentials and binding the vault to sessions. If `jq` is not installed in your environment, extract `id` from the response JSON and set `VAULT_ID` manually.

### Add a credential

Write an access credential to the vault you just created. The example below adds a static Bearer credential:

```bash cURL theme={null}
CREDENTIAL_ID=$(curl --silent --show-error --request POST "https://api.moonshot.ai/v1/vaults/$VAULT_ID/credentials" \
  --header "Authorization: Bearer $KIMI_API_KEY" \
  --header "kimi-api-version: 2026-09-01-beta" \
  --header "Content-Type: application/json" \
  --data '{
    "display_name": "MCP access",
    "auth": {
      "type": "static_bearer",
      "mcp_server_url": "https://example.invalid/mcp",
      "token": "your-token"
    }
  }' | jq -r '.id')
```

```bash CLI theme={null}
CREDENTIAL_ID=$(kha vaults credentials create "$VAULT_ID" -o json --file - <<'JSON' | jq -r '.id'
{
  "display_name": "MCP access",
  "auth": {
    "type": "static_bearer",
    "mcp_server_url": "https://example.invalid/mcp",
    "token": "your-token"
  }
}
JSON
)
```

Replace `your-token` with the real credential value. Save `id` from the response as `CREDENTIAL_ID` so you can inspect, update, or archive this credential later.

A successful response returns the credential `id` and non-sensitive configuration, without secret values for `token`, `access_token`, `refresh_token`, `client_secret`, or `secret_value`.

#### Add an OAuth credential

When an MCP server uses OAuth 2.0, set `type` to `mcp_oauth` and supply `mcp_server_url` and `access_token`. For automatic token refresh, also provide `refresh` with `token_endpoint`, `client_id`, and `token_endpoint_auth_type` configured together.

#### Add an environment variable credential

To pass credentials via an environment variable, provide `secret_name` and `secret_value`. You can use `networking` to restrict which target hosts receive the substituted credential, and `injection_location` to restrict where substitution takes place. Omitting `networking` defaults to `limited` with an empty allowlist, meaning the credential is not substituted for any host; you must explicitly configure `allowed_hosts` or set `type` to `unrestricted` for substitution to occur.

```bash cURL theme={null}
curl --request POST "https://api.moonshot.ai/v1/vaults/$VAULT_ID/credentials" \
  --header "Authorization: Bearer $KIMI_API_KEY" \
  --header "kimi-api-version: 2026-09-01-beta" \
  --header "Content-Type: application/json" \
  --data '{
    "display_name": "Example API key",
    "auth": {
      "type": "environment_variable",
      "secret_name": "EXAMPLE_API_KEY",
      "secret_value": "your-token",
      "networking": {"type": "limited", "allowed_hosts": ["api.example.com"]},
      "injection_location": {"header": true}
    }
  }'
```

```bash CLI theme={null}
kha vaults credentials create "$VAULT_ID" --file - <<'JSON'
{
  "display_name": "Example API key",
  "auth": {
    "type": "environment_variable",
    "secret_name": "EXAMPLE_API_KEY",
    "secret_value": "your-token",
    "networking": {"type": "limited", "allowed_hosts": ["api.example.com"]},
    "injection_location": {"header": true}
  }
}
JSON
```

Omitting `injection_location` altogether at creation time enables substitution in both request headers and request bodies. If you provide the object and omit one of its fields, the omitted field defaults to `false`—for example, passing `{"header": true}` does not substitute in the request body. At least one location must be enabled.

`networking.allowed_hosts` only controls which target hosts can receive the substituted credential; it does not replace the execution environment's network policy. Credential substitution is available only when both the target host configuration and the execution environment permit the request.

#### Add credentials in batch

To write multiple credentials in a single request, call the batch creation endpoint and pass 1 to 20 credentials in `items`, each using the same fields as single-credential creation:

```bash cURL theme={null}
curl --request POST "https://api.moonshot.ai/v1/vaults/$VAULT_ID/credentials/batch" \
  --header "Authorization: Bearer $KIMI_API_KEY" \
  --header "kimi-api-version: 2026-09-01-beta" \
  --header "Content-Type: application/json" \
  --data '{
    "items": [
      {
        "display_name": "MCP access",
        "auth": {
          "type": "static_bearer",
          "mcp_server_url": "https://example.invalid/mcp",
          "token": "your-token"
        }
      },
      {
        "display_name": "MCP access 2",
        "auth": {
          "type": "static_bearer",
          "mcp_server_url": "https://example.invalid/mcp-2",
          "token": "your-token-2"
        }
      }
    ]
  }'
```

```bash CLI theme={null}
kha vaults credentials batch "$VAULT_ID" --file - <<'JSON'
{
  "items": [
    {
      "display_name": "MCP access",
      "auth": {
        "type": "static_bearer",
        "mcp_server_url": "https://example.invalid/mcp",
        "token": "your-token"
      }
    },
    {
      "display_name": "MCP access 2",
      "auth": {
        "type": "static_bearer",
        "mcp_server_url": "https://example.invalid/mcp-2",
        "token": "your-token-2"
      }
    }
  ]
}
JSON
```

Batch creation is atomic: if validation fails for any item, none of the credentials are created. Matching field values within `items` (such as `mcp_server_url`) must be unique; duplicate values cause the endpoint to return HTTP 400. the batch also counts toward the vault's limit of 20 active credentials. In a successful response, `items` returns the created credentials in request order, without credential secret values.

### Complete OAuth authorization

You can also add an `mcp_oauth` credential via browser authorization without manually filling in `access_token`; completing authorization creates the credential automatically. Starting authorization when the vault already has an active credential for the same MCP server returns HTTP 409—archive the old credential first. After creating an authorization session, direct the user to open the returned `authorize_url`.

```bash cURL theme={null}
OAUTH_SESSION_ID=$(curl --silent --show-error --request POST "https://api.moonshot.ai/v1/vaults/$VAULT_ID/oauth-sessions" \
  --header "Authorization: Bearer $KIMI_API_KEY" \
  --header "kimi-api-version: 2026-09-01-beta" \
  --header "Content-Type: application/json" \
  --data '{
    "mcp_server_url": "https://example.invalid/mcp",
    "display_name": "MCP access"
  }' | jq -r '.oauth_session_id')
```

```bash CLI theme={null}
OAUTH_SESSION_ID=$(kha vaults oauth create "$VAULT_ID" --mcp-url "https://example.invalid/mcp" --display-name "MCP access" -o json | jq -r '.oauth_session_id')
```

From the response, extract `authorize_url` for the user to open in a browser and save `oauth_session_id` as `OAUTH_SESSION_ID`. If `client` is omitted, the platform registers an OAuth client automatically; to use an existing client, provide `client_id` along with any other `client` fields required by the authorization server.

Once the user completes authorization in the browser, poll the authorization session status:

```bash cURL theme={null}
curl --request GET "https://api.moonshot.ai/v1/vaults/$VAULT_ID/oauth-sessions/$OAUTH_SESSION_ID" \
  --header "Authorization: Bearer $KIMI_API_KEY" \
  --header "kimi-api-version: 2026-09-01-beta"
```

```bash CLI theme={null}
kha vaults oauth get "$VAULT_ID" "$OAUTH_SESSION_ID"
```

Continue polling while the status is `pending`. When the status becomes `authorized`, the response includes `credential_id` for the new credential—save it as `CREDENTIAL_ID`. When the status is `failed`, the response includes `error`; address the cause and start a new authorization session. There is no synchronous endpoint to finalize authorization; you must poll to obtain the result.

### Check credential coverage

The credential precheck endpoint checks active credential coverage across vaults for a set of MCP server URLs. For example, running a precheck before starting OAuth authorization lets you detect whether an active credential already exists for the same server and avoid a 409 response:

```bash cURL theme={null}
curl --request POST "https://api.moonshot.ai/v1/vaults/credentials/check" \
  --header "Authorization: Bearer $KIMI_API_KEY" \
  --header "kimi-api-version: 2026-09-01-beta" \
  --header "Content-Type: application/json" \
  --data '{
    "mcp_server_urls": ["https://example.invalid/mcp"],
    "vault_ids": ["vlt_your_vault_id"]
  }'
```

```bash CLI theme={null}
kha vaults credentials check --file - <<'JSON'
{
  "mcp_server_urls": ["https://example.invalid/mcp"],
  "vault_ids": ["vlt_your_vault_id"]
}
JSON
```

Pass 1 to 50 unique MCP server URLs in `mcp_server_urls`; `vault_ids` is optional and accepts up to 16 vault IDs; when omitted, the endpoint searches all vaults visible to the caller. In the response, `coverage` reports coverage for each URL in request order: `mcp_server_url` is the original URL from the request, and `matching_vault_ids` lists the IDs of vaults containing an active credential for that URL (an empty array means no coverage). Each URL is normalized before matching, and URLs that cannot be normalized match no credentials. Only `mcp_oauth` and `static_bearer` credentials count toward coverage; `environment_variable` credentials do not appear in the results.

### Bind a vault when creating a session

Vaults can only be bound when creating a session and cannot be added afterward via the session resources endpoints. Replace `agent_id` and `environment_id` with the ID of an agent you have access to and the ID of an execution environment whose build is ready.

```bash cURL theme={null}
curl --request POST "https://api.moonshot.ai/v1/sessions" \
  --header "Authorization: Bearer $KIMI_API_KEY" \
  --header "kimi-api-version: 2026-09-01-beta" \
  --header "Content-Type: application/json" \
  --data '{
    "agent_id": "agent_your_agent_id",
    "environment_id": "env_your_environment_id",
    "resources": [{"type": "vault", "vault_id": "vlt_your_vault_id"}],
    "title": "Alice daily summary"
  }'
```

```bash CLI theme={null}
kha sessions create --file - <<'JSON'
{
  "agent_id": "agent_your_agent_id",
  "environment_id": "env_your_environment_id",
  "resources": [{"type": "vault", "vault_id": "vlt_your_vault_id"}],
  "title": "Alice daily summary"
}
JSON
```

After creation succeeds, `resources` on the session contains a binding with `type` set to `vault`. You can bind at most 16 vaults to a session. If multiple bound vaults hold an active credential for the same MCP server, the platform uses the first match according to the vault order in `resources`.

## Ongoing management

### Inspect vaults and credentials

Inspect a vault and its credentials to verify current names, matching fields, and archive status. Credential lists exclude archived credentials by default; pass `include_archived=true` to include archived credentials as well. List endpoints paginate with `page_size` and an opaque `page_token`: pass `next_page_token` from the response as `page_token` on the next request; when `next_page_token` is omitted, there are no more results.

```bash cURL theme={null}
curl --request GET "https://api.moonshot.ai/v1/vaults/$VAULT_ID" \
  --header "Authorization: Bearer $KIMI_API_KEY" \
  --header "kimi-api-version: 2026-09-01-beta"
```

```bash CLI theme={null}
kha vaults get "$VAULT_ID"
```

```bash cURL theme={null}
curl --request GET "https://api.moonshot.ai/v1/vaults/$VAULT_ID/credentials" \
  --header "Authorization: Bearer $KIMI_API_KEY" \
  --header "kimi-api-version: 2026-09-01-beta"
```

```bash CLI theme={null}
kha vaults credentials list "$VAULT_ID"
```

Retrieve a single credential:

```bash cURL theme={null}
curl --request GET "https://api.moonshot.ai/v1/vaults/$VAULT_ID/credentials/$CREDENTIAL_ID" \
  --header "Authorization: Bearer $KIMI_API_KEY" \
  --header "kimi-api-version: 2026-09-01-beta"
```

```bash CLI theme={null}
kha vaults credentials get "$VAULT_ID" "$CREDENTIAL_ID"
```

A successful response returns the resource record and non-sensitive configuration, without credential secret values.

### Update a vault

Updating a vault allows modifying only `display_name` or `metadata`:

```bash cURL theme={null}
curl --request PATCH "https://api.moonshot.ai/v1/vaults/$VAULT_ID" \
  --header "Authorization: Bearer $KIMI_API_KEY" \
  --header "kimi-api-version: 2026-09-01-beta" \
  --header "Content-Type: application/json" \
  --data '{"display_name": "Alice credentials"}'
```

```bash CLI theme={null}
kha vaults update "$VAULT_ID" --display-name "Alice credentials"
```

### Rotate credential values

When rotating a credential value, submit only the fields that need updating. The credential type and identifying fields—such as `type`, `mcp_server_url`, `secret_name`, `token_endpoint`, and `client_id`—cannot be modified. `type` in the request must match the existing credential.

The request below rotates the token for a static Bearer credential. `mcp_server_url` is the credential's existing value and cannot be changed to another URL:

```bash cURL theme={null}
curl --request PATCH "https://api.moonshot.ai/v1/vaults/$VAULT_ID/credentials/$CREDENTIAL_ID" \
  --header "Authorization: Bearer $KIMI_API_KEY" \
  --header "kimi-api-version: 2026-09-01-beta" \
  --header "Content-Type: application/json" \
  --data '{
    "auth": {
      "type": "static_bearer",
      "mcp_server_url": "https://example.invalid/mcp",
      "token": "your-new-token"
    }
  }'
```

```bash CLI theme={null}
kha vaults credentials update "$VAULT_ID" "$CREDENTIAL_ID" --file - <<'JSON'
{
  "auth": {
    "type": "static_bearer",
    "mcp_server_url": "https://example.invalid/mcp",
    "token": "your-new-token"
  }
}
JSON
```

A successful response returns the updated non-sensitive configuration, without the new credential value.

### Archive or delete

Archiving and deleting are two distinct operations:

* **Archive**: Retains the resource record while wiping its secret values. Archived credentials or vaults cannot be used in new sessions.
* **Delete**: Permanently removes the resource record.

Choose the endpoint corresponding to the resource you want to manage. Deletion applies only to archived vaults or credentials; to delete permanently, archive first and then delete.

| Resource | Archive endpoint | Delete endpoint |
| - | - | - |
| Credential | `POST /v1/vaults/{vault_id}/credentials/{credential_id}/archive` | `DELETE /v1/vaults/{vault_id}/credentials/{credential_id}` |
| Vault | `POST /v1/vaults/{vault_id}/archive` | `DELETE /v1/vaults/{vault_id}` |

Archiving a vault also archives its active credentials and wipes their secret values. Deleting a vault deletes its credential records at the same time—you do not need to delete each credential separately.

Archive a single credential while keeping its record:

```bash cURL theme={null}
curl --request POST "https://api.moonshot.ai/v1/vaults/$VAULT_ID/credentials/$CREDENTIAL_ID/archive" \
  --header "Authorization: Bearer $KIMI_API_KEY" \
  --header "kimi-api-version: 2026-09-01-beta"
```

```bash CLI theme={null}
kha vaults credentials archive "$VAULT_ID" "$CREDENTIAL_ID"
```

Permanently delete a vault and its credential records. Archive the vault first:

```bash cURL theme={null}
curl --request POST "https://api.moonshot.ai/v1/vaults/$VAULT_ID/archive" \
  --header "Authorization: Bearer $KIMI_API_KEY" \
  --header "kimi-api-version: 2026-09-01-beta"
```

```bash CLI theme={null}
kha vaults archive "$VAULT_ID"
```

Delete the vault after archiving completes:

```bash cURL theme={null}
curl --request DELETE "https://api.moonshot.ai/v1/vaults/$VAULT_ID" \
  --header "Authorization: Bearer $KIMI_API_KEY" \
  --header "kimi-api-version: 2026-09-01-beta"
```

```bash CLI theme={null}
kha vaults delete "$VAULT_ID"
```

## Security boundaries

Credential values are write-only and never appear in create, retrieve, list, or update responses. Never include credential values in client logs, prompts, or any other content that enters the model context.

MCP credentials match by `mcp_server_url`, and environment variable credentials match by `secret_name`. For `environment_variable`, the platform substitutes the placeholder only when both the target host configuration and the execution environment's network policy allow the request.
