Maintainers
Pricing
Enterprise
More
More
Documentation
Try Live
Rankings
Install
Blog
Docs7
Ask
New
Add Docs
Kimi API
https://platform.kimi.com/docs
Admin
Kimi API is a language model inference service providing Chat Completions endpoints for
...
Tokens:
20,219
Snippets:
71
Trust Score:
10
Update:
2 weeks ago
Context
Chat
Benchmark
24.71
Latest
Show doc for...
Code
Info
Show Results
Tokens:
Raw
Copy
Link
Tokens:
Raw
Copy
Link
### Go example for Messages API Source: https://platform.kimi.com/docs/api/messages Uses the net/http package to POST a chat message to the Messages API. The payload is a JSON string. Prints the response body. ```go package main import ( "fmt" "strings" "net/http" "io" ) func main() { url := "https://api.moonshot.cn/anthropic/v1/messages" payload := strings.NewReader("{\n \"model\": \"kimi-k3\",\n \"messages\": [\n {\n \"role\": \"user\",\n \"content\": \"你好\"\n }\n ],\n \"max_tokens\": 2\n}") req, _ := http.NewRequest("POST", url, payload) req.Header.Add("Authorization", "Bearer <token>") req.Header.Add("Content-Type", "application/json") res, _ := http.DefaultClient.Do(req) defer res.Body.Close() body, _ := io.ReadAll(res.Body) fmt.Println(string(body)) } ``` -------------------------------- ### Python requests example for chat completion Source: https://platform.kimi.com/docs/api/chat Uses the requests library to POST a chat completion request. The payload mirrors the cURL example, with the model kimi-k3, a user message, and a tools array. Requires a bearer token and prints the response text. ```python import requests url = "https://api.moonshot.cn/v1/chat/completions" payload = { "model": "kimi-k3", "messages": [ { "role": "user", "content": "你好", "name": None, "partial": False } ], "tools": [ { "type": "function", "function": { "name": "<string>", "parameters": {}, "description": "<string>", "strict": True } } ] } headers = { "Authorization": "Bearer <token>", "Content-Type": "application/json" } response = requests.post(url, json=payload, headers=headers) print(response.text) ``` -------------------------------- ### POST /v1/chat/completions Source: https://platform.kimi.com/docs/api/chat Example code for calling the chat completions endpoint in various programming languages. ```APIDOC ## POST /v1/chat/completions ### Description Creates a chat completion using the kimi-k3 model. This endpoint accepts a list of messages and optional tools for function calling. ### Method POST ### Endpoint https://api.moonshot.cn/v1/chat/completions ### Parameters #### Request Body - **model** (string) - Required - The model identifier, e.g., "kimi-k3". - **messages** (array) - Required - A list of message objects. Each message contains: - **role** (string) - Required - The role of the message author (e.g., "user"). - **content** (string) - Required - The content of the message. - **name** (string|null) - Optional - The name of the author. - **partial** (boolean) - Optional - Indicates if the message is partial. - **tools** (array) - Optional - A list of tools for function calling. Each tool contains: - **type** (string) - Required - The tool type, e.g., "function". - **function** (object) - Required - The function definition with: - **name** (string) - Required - The function name. - **parameters** (object) - Required - The function parameters schema. - **description** (string) - Optional - The function description. - **strict** (boolean) - Optional - Whether to enforce strict schema. ### Request Example { "model": "kimi-k3", "messages": [ { "role": "user", "content": "你好", "name": null, "partial": false } ], "tools": [ { "type": "function", "function": { "name": "<string>", "parameters": {}, "description": "<string>", "strict": true } } ] } ### Response #### Success Response (200) The response is a JSON object containing the chat completion result. The exact structure is not specified in the source. #### Response Example (Not provided in the source) ### Authentication Requires an Authorization header with a Bearer token. ### Headers - **Authorization** (string) - Required - Bearer token for authentication. - **Content-Type** (string) - Required - Must be "application/json". ``` -------------------------------- ### Go example for chat completion Source: https://platform.kimi.com/docs/api/chat Uses the net/http package to POST a chat completion request. The payload is a raw JSON string with the model kimi-k3, a user message, and a tools array. Requires a bearer token and prints the response body. ```go package main import ( "fmt" "strings" "net/http" "io" ) func main() { url := "https://api.moonshot.cn/v1/chat/completions" payload := strings.NewReader("{\n \"model\": \"kimi-k3\",\n \"messages\": [\n {\n \"role\": \"user\",\n \"content\": \"你好\",\n \"name\": null,\n \"partial\": false\n }\n ],\n \"tools\": [\n {\n \"type\": \"function\",\n \"function\": {\n \"name\": \"<string>\",\n \"parameters\": {},\n \"description\": \"<string>\",\n \"strict\": true\n }\n }\n ]\n}") req, _ := http.NewRequest("POST", url, payload) req.Header.Add("Authorization", "Bearer <token>") req.Header.Add("Content-Type", "application/json") res, _ := http.DefaultClient.Do(req) defer res.Body.Close() body, _ := io.ReadAll(res.Body) fmt.Println(string(body)) } ``` -------------------------------- ### Create credential success response Source: https://platform.kimi.com/docs/api-reference/凭据库/创建凭据 Example success response (HTTP 201) for creating a credential. Contains the credential ID, vault ID, display name, auth configuration, and timestamps. ```json { "id": "vcrd_01h455vb4pex5vsknk084sn02q", "vault_id": "vlt_01h455vb4pex5vsknk084sn02q", "display_name": "MCP access", "auth": { "type": "static_bearer", "mcp_server_url": "https://example.invalid/mcp" }, "created_at": "2026-01-01T00:00:00Z", "updated_at": "2026-01-01T00:00:00Z" } ``` -------------------------------- ### Batch create credentials - Go, Java, Ruby examples Source: https://platform.kimi.com/docs/api-reference/凭据库/批量创建凭据 Send a POST request to the batch create credentials endpoint. Replace {id} with the vault ID and <token> with your API key. The request body contains a single credential with static_bearer auth; the response is printed to stdout. ```go package main import ( "fmt" "strings" "net/http" "io" ) func main() { url := "https://api.moonshot.cn/v1/vaults/{id}/credentials/batch" payload := strings.NewReader("{\n \"items\": [\n {\n \"display_name\": \"MCP access\",\n \"auth\": {\n \"type\": \"static_bearer\",\n \"mcp_server_url\": \"https://example.invalid/mcp\",\n \"token\": \"sk-example\"\n }\n }\n ]\n}") req, _ := http.NewRequest("POST", url, payload) req.Header.Add("Authorization", "Bearer <token>") req.Header.Add("Content-Type", "application/json") res, _ := http.DefaultClient.Do(req) defer res.Body.Close() body, _ := io.ReadAll(res.Body) fmt.Println(string(body)) } ``` ```java HttpResponse<String> response = Unirest.post("https://api.moonshot.cn/v1/vaults/{id}/credentials/batch") .header("Authorization", "Bearer <token>") .header("Content-Type", "application/json") .body("{\n \"items\": [\n {\n \"display_name\": \"MCP access\",\n \"auth\": {\n \"type\": \"static_bearer\",\n \"mcp_server_url\": \"https://example.invalid/mcp\",\n \"token\": \"sk-example\"\n }\n }\n ]\n}") .asString(); ``` ```ruby require 'uri' require 'net/http' url = URI("https://api.moonshot.cn/v1/vaults/{id}/credentials/batch") http = Net::HTTP.new(url.host, url.port) http.use_ssl = true request = Net::HTTP::Post.new(url) request["Authorization"] = 'Bearer <token>' request["Content-Type"] = 'application/json' request.body = "{\n \"items\": [\n {\n \"display_name\": \"MCP access\",\n \"auth\": {\n \"type\": \"static_bearer\",\n \"mcp_server_url\": \"https://example.invalid/mcp\",\n \"token\": \"sk-example\"\n }\n }\n ]\n}" response = http.request(request) puts response.read_body ``` -------------------------------- ### JavaScript fetch example for chat completion Source: https://platform.kimi.com/docs/api/chat Uses the fetch API to POST a chat completion request. The body is a JSON string with the model kimi-k3, a user message, and a tools array. Requires a bearer token and logs the parsed response or error. ```javascript const options = { method: 'POST', headers: {Authorization: 'Bearer <token>', 'Content-Type': 'application/json'}, body: JSON.stringify({ model: 'kimi-k3', messages: [{role: 'user', content: '你好', name: null, partial: false}], tools: [ { type: 'function', function: {name: '<string>', parameters: {}, description: '<string>', strict: true} } ] }) }; fetch('https://api.moonshot.cn/v1/chat/completions', options) .then(res => res.json()) .then(res => console.log(res)) .catch(err => console.error(err)); ``` -------------------------------- ### Java Unirest example for chat completion Source: https://platform.kimi.com/docs/api/chat Uses the Unirest HTTP library to POST a chat completion request. The body is a raw JSON string with the model kimi-k3, a user message, and a tools array. Requires a bearer token and returns the response as a string. ```java HttpResponse<String> response = Unirest.post("https://api.moonshot.cn/v1/chat/completions") .header("Authorization", "Bearer <token>") .header("Content-Type", "application/json") .body("{\n \"model\": \"kimi-k3\",\n \"messages\": [\n {\n \"role\": \"user\",\n \"content\": \"你好\",\n \"name\": null,\n \"partial\": false\n }\n ],\n \"tools\": [\n {\n \"type\": \"function\",\n \"function\": {\n \"name\": \"<string>\",\n \"parameters\": {},\n \"description\": \"<string>\",\n \"strict\": true\n }\n }\n ]\n}") .asString(); ``` -------------------------------- ### Ruby Net::HTTP example for Messages API Source: https://platform.kimi.com/docs/api/messages Uses Net::HTTP to POST a chat message to the Messages API. Sets SSL, headers, and body. Outputs the response body. ```ruby require 'uri' require 'net/http' url = URI("https://api.moonshot.cn/anthropic/v1/messages") http = Net::HTTP.new(url.host, url.port) http.use_ssl = true request = Net::HTTP::Post.new(url) request["Authorization"] = 'Bearer <token>' request["Content-Type"] = 'application/json' request.body = "{\n \"model\": \"kimi-k3\",\n \"messages\": [\n {\n \"role\": \"user\",\n \"content\": \"你好\"\n }\n ],\n \"max_tokens\": 2\n}" response = http.request(request) puts response.read_body ``` -------------------------------- ### POST /v1/triggers Source: https://platform.kimi.com/docs/api-reference/触发器/创建触发器 This snippet documents the POST /v1/triggers endpoint for creating a trigger, including request body parameters, example requests in multiple languages, and response structures. ```APIDOC ## POST /v1/triggers ### Description Creates a new trigger with a specified policy (e.g., cron schedule) for an agent in a given environment. ### Method POST ### Endpoint https://api.moonshot.cn/v1/triggers ### Parameters #### Request Body - **name** (string) - Required - Name of the trigger. - **agent_id** (string) - Required - Identifier of the agent to associate with the trigger. - **environment_id** (string) - Required - Identifier of the environment where the agent runs. - **policy** (object) - Required - Scheduling policy for the trigger. - **type** (string) - Required - Type of policy (e.g., 'cron'). - **expression** (string) - Required - Cron expression defining the schedule. - **timezone** (string) - Required - Timezone for the schedule. ### Request Example ```json { "name": "Daily summary", "agent_id": "agent_01h455vb4pex5vsknk084sn02q", "environment_id": "env_01h455vb4pex5vsknk084sn02q", "policy": { "type": "cron", "expression": "0 9 * * *", "timezone": "UTC" } } ``` ### Response #### Success Response (201) - **id** (string) - Unique identifier of the created trigger. - **name** (string) - Name of the trigger. - **status** (string) - Status of the trigger (e.g., 'active'). - **agent_id** (string) - Associated agent identifier. - **session_mode** (string) - Session mode for the trigger. - **policy** (object) - The scheduling policy. - **scope** (string) - Scope of the trigger (e.g., 'project'). - **created_by** (string) - User who created the trigger. - **created_at** (string) - Creation timestamp. - **updated_at** (string) - Last update timestamp. #### Response Example ```json { "id": "trig_01h455vb4pex5vsknk084sn02q", "name": "Daily summary", "status": "active", "agent_id": "agent_01h455vb4pex5vsknk084sn02q", "session_mode": "new_session", "policy": { "type": "cron", "expression": "0 9 * * *", "timezone": "UTC" }, "scope": "project", "created_by": "user_example", "created_at": "2026-08-01T09:00:00Z", "updated_at": "2026-08-01T09:00:00Z" } ``` #### Error Responses - **400** - Invalid parameter error. - **401** - Unauthorized. - **404** - Not found. - **409** - Conflict. - **429** - Too many requests. - **default** - Unexpected error. #### Error Response Example ```json { "error": { "type": "invalid_parameter_error", "message": "<string>", "param": "<string>", "request_id": "<string>" } } ``` ``` -------------------------------- ### Python requests example for Messages API Source: https://platform.kimi.com/docs/api/messages Uses the requests library to POST a chat message to the Messages API. The payload includes the model, messages, and max_tokens. Prints the response text. ```python import requests url = "https://api.moonshot.cn/anthropic/v1/messages" payload = { "model": "kimi-k3", "messages": [ { "role": "user", "content": "你好" } ], "max_tokens": 2 } headers = { "Authorization": "Bearer <token>", "Content-Type": "application/json" } response = requests.post(url, json=payload, headers=headers) print(response.text) ``` -------------------------------- ### Java Unirest example for Messages API Source: https://platform.kimi.com/docs/api/messages Uses the Unirest HTTP library to POST a chat message to the Messages API. The body is a JSON string. Returns an HttpResponse<String>. ```java HttpResponse<String> response = Unirest.post("https://api.moonshot.cn/anthropic/v1/messages") .header("Authorization", "Bearer <token>") .header("Content-Type", "application/json") .body("{\n \"model\": \"kimi-k3\",\n \"messages\": [\n {\n \"role\": \"user\",\n \"content\": \"你好\"\n }\n ],\n \"max_tokens\": 2\n}") .asString(); ``` -------------------------------- ### PHP cURL example for chat completion Source: https://platform.kimi.com/docs/api/chat Uses PHP's cURL extension to POST a chat completion request. The payload is JSON-encoded with the model kimi-k3, a user message, and a tools array. Requires a bearer token and prints the response or a cURL error. ```php <?php $curl = curl_init(); curl_setopt_array($curl, [ CURLOPT_URL => "https://api.moonshot.cn/v1/chat/completions", CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => "", CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 30, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => "POST", CURLOPT_POSTFIELDS => json_encode([ 'model' => 'kimi-k3', 'messages' => [ [ 'role' => 'user', 'content' => '你好', 'name' => null, 'partial' => false ] ], 'tools' => [ [ 'type' => 'function', 'function' => [ 'name' => '<string>', 'parameters' => [ ], 'description' => '<string>', 'strict' => true ] ] ] ]), CURLOPT_HTTPHEADER => [ "Authorization: Bearer <token>", "Content-Type: application/json" ], ]); $response = curl_exec($curl); $err = curl_error($curl); curl_close($curl); if ($err) { echo "cURL Error #:" . $err; } else { echo $response; } ``` -------------------------------- ### JavaScript fetch example for Messages API Source: https://platform.kimi.com/docs/api/messages Uses the fetch API to POST a chat message to the Messages API. The request body is a JSON string with model, messages, and max_tokens. Logs the response or error. ```javascript const options = { method: 'POST', headers: {Authorization: 'Bearer <token>', 'Content-Type': 'application/json'}, body: JSON.stringify({model: 'kimi-k3', messages: [{role: 'user', content: '你好'}], max_tokens: 2}) }; fetch('https://api.moonshot.cn/anthropic/v1/messages', options) .then(res => res.json()) .then(res => console.log(res)) .catch(err => console.error(err)); ``` -------------------------------- ### 创建环境 - POST /v1/environments (PHP/Go/Java/Ruby) Source: https://platform.kimi.com/docs/api-reference/环境/创建环境 POST https://api.moonshot.cn/v1/environments 创建环境,请求体包含 name 和 config(type、networking、packages、setup_script)。需要 Authorization: Bearer <token> 和 Content-Type: application/json。 ```php <?php $curl = curl_init(); curl_setopt_array($curl, [ CURLOPT_URL => "https://api.moonshot.cn/v1/environments", CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => "", CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 30, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => "POST", CURLOPT_POSTFIELDS => json_encode([ 'name' => 'Data analysis', 'config' => [ 'type' => 'cloud', 'networking' => [ 'type' => 'limited', 'allowed_hosts' => [ 'example.invalid' ] ], 'packages' => [ 'pip' => [ 'pandas==2.3.2' ] ], 'setup_script' => 'mkdir -p /workspace/output' ] ]), CURLOPT_HTTPHEADER => [ "Authorization: Bearer <token>", "Content-Type: application/json" ], ]); $response = curl_exec($curl); $err = curl_error($curl); curl_close($curl); if ($err) { echo "cURL Error #:" . $err; } else { echo $response; } ``` ```go package main import ( "fmt" "strings" "net/http" "io" ) func main() { url := "https://api.moonshot.cn/v1/environments" payload := strings.NewReader("{\n \"name\": \"Data analysis\",\n \"config\": {\n \"type\": \"cloud\",\n \"networking\": {\n \"type\": \"limited\",\n \"allowed_hosts\": [\n \"example.invalid\"\n ]\n },\n \"packages\": {\n \"pip\": [\n \"pandas==2.3.2\"\n ]\n },\n \"setup_script\": \"mkdir -p /workspace/output\"\n }\n}") req, _ := http.NewRequest("POST", url, payload) req.Header.Add("Authorization", "Bearer <token>") req.Header.Add("Content-Type", "application/json") res, _ := http.DefaultClient.Do(req) defer res.Body.Close() body, _ := io.ReadAll(res.Body) fmt.Println(string(body)) } ``` ```java HttpResponse<String> response = Unirest.post("https://api.moonshot.cn/v1/environments") .header("Authorization", "Bearer <token>") .header("Content-Type", "application/json") .body("{\n \"name\": \"Data analysis\",\n \"config\": {\n \"type\": \"cloud\",\n \"networking\": {\n \"type\": \"limited\",\n \"allowed_hosts\": [\n \"example.invalid\"\n ]\n },\n \"packages\": {\n \"pip\": [\n \"pandas==2.3.2\"\n ]\n },\n \"setup_script\": \"mkdir -p /workspace/output\"\n }\n}") .asString(); ``` ```ruby require 'uri' require 'net/http' url = URI("https://api.moonshot.cn/v1/environments") http = Net::HTTP.new(url.host, url.port) http.use_ssl = true request = Net::HTTP::Post.new(url) request["Authorization"] = 'Bearer <token>' request["Content-Type"] = 'application/json' request.body = "{\n \"name\": \"Data analysis\",\n \"config\": {\n \"type\": \"cloud\",\n \"networking\": {\n \"type\": \"limited\",\n \"allowed_hosts\": [\n \"example.invalid\"\n ]\n },\n \"packages\": {\n \"pip\": [\n \"pandas==2.3.2\"\n ]\n },\n \"setup_script\": \"mkdir -p /workspace/output\"\n }\n}" response = http.request(request) puts response.read_body ``` -------------------------------- ### Create a model response with Go Source: https://platform.kimi.com/docs/api/responses Uses the net/http package to POST a JSON payload to the /v1/responses endpoint. The payload includes the model kimi-k3, input, text, and a function tool. Prints the response body. ```go package main import ( "fmt" "strings" "net/http" "io" ) func main() { url := "https://api.moonshot.cn/v1/responses" payload := strings.NewReader("{\n \"model\": \"kimi-k3\",\n \"input\": \"<string>\",\n \"text\": {},\n \"tools\": [\n {\n \"type\": \"function\",\n \"name\": \"<string>\",\n \"description\": \"<string>\",\n \"parameters\": {},\n \"strict\": true\n }\n ]\n}") req, _ := http.NewRequest("POST", url, payload) req.Header.Add("Authorization", "Bearer <token>") req.Header.Add("Content-Type", "application/json") res, _ := http.DefaultClient.Do(req) defer res.Body.Close() body, _ := io.ReadAll(res.Body) fmt.Println(string(body)) } ``` -------------------------------- ### Error response schema (401) for Messages API Source: https://platform.kimi.com/docs/api/messages Example of an error response for HTTP status code 401. Contains an error object with message, type, and code. ```json { "error": { "message": "<string>", "type": "<string>", "code": "<string>" } } ``` -------------------------------- ### POST /v1/environments Source: https://platform.kimi.com/docs/api-reference/环境/创建环境 创建一个新的环境,用于执行数据分析等任务。需要提供环境名称和配置信息,包括网络、包和设置脚本。 ```APIDOC ## POST /v1/environments ### Description 创建一个新的环境,用于执行数据分析等任务。需要提供环境名称和配置信息,包括网络、包和设置脚本。 ### Method POST ### Endpoint https://api.moonshot.cn/v1/environments ### Parameters #### Request Body - **name** (string) - Required - 环境名称。 - **config** (object) - Required - 环境配置。 - **type** (string) - Required - 环境类型,例如 "cloud"。 - **networking** (object) - Required - 网络配置。 - **type** (string) - Required - 网络类型,例如 "limited"。 - **allowed_hosts** (array of strings) - Optional - 允许访问的主机列表。 - **packages** (object) - Optional - 包管理器配置。 - **pip** (array of strings) - Optional - pip 包列表。 - **setup_script** (string) - Optional - 环境初始化脚本。 ### Request Example { "name": "Data analysis", "config": { "type": "cloud", "networking": { "type": "limited", "allowed_hosts": [ "example.invalid" ] }, "packages": { "pip": [ "pandas==2.3.2" ] }, "setup_script": "mkdir -p /workspace/output" } } ### Response #### Success Response (201) - **id** (string) - 环境 ID。 - **name** (string) - 环境名称。 - **description** (string) - 环境描述。 - **config** (object) - 环境配置。 - **metadata** (object) - 元数据。 - **build_status** (string) - 构建状态。 - **created_at** (string) - 创建时间。 - **updated_at** (string) - 更新时间。 - **scope** (string) - 作用域。 #### Response Example { "id": "env_01h455vb4pex5vsknk084sn02q", "name": "Data analysis", "description": "用于数据分析任务的执行环境", "config": { "type": "cloud", "networking": { "type": "limited", "allowed_hosts": [ "example.invalid" ] }, "packages": { "pip": [ "pandas==2.3.2" ] }, "setup_script": "mkdir -p /workspace/output" }, "metadata": {}, "build_status": "building", "created_at": "2026-08-26T12:00:00Z", "updated_at": "2026-08-26T12:00:00Z", "scope": "project" } #### Error Responses - **400** - 请求参数错误。 - **401** - 认证失败。 - **409** - 冲突。 - **429** - 请求过多。 - **default** - 其他错误。 #### Error Response Example { "error": { "type": "invalid_parameter_error", "message": "<string>", "param": "<string>", "request_id": "<string>" } } ``` -------------------------------- ### POST to Moonshot API with kimi-k3 (Ruby Net::HTTP) Source: https://platform.kimi.com/docs/api/chat Uses Ruby's Net::HTTP to POST the kimi-k3 chat completion request with a function tool, enabling SSL. Prints the response body. ```ruby require 'uri' require 'net/http' url = URI("https://api.moonshot.cn/v1/chat/completions") http = Net::HTTP.new(url.host, url.port) http.use_ssl = true request = Net::HTTP::Post.new(url) request["Authorization"] = 'Bearer <token>' request["Content-Type"] = 'application/json' request.body = "{\n \"model\": \"kimi-k3\",\n \"messages\": [\n {\n \"role\": \"user\",\n \"content\": \"你好\",\n \"name\": null,\n \"partial\": false\n }\n ],\n \"tools\": [\n {\n \"type\": \"function\",\n \"function\": {\n \"name\": \"<string>\",\n \"parameters\": {},\n \"description\": \"<string>\",\n \"strict\": true\n }\n }\n ]\n}" response = http.request(request) puts response.read_body ``` -------------------------------- ### Create a model response with cURL Source: https://platform.kimi.com/docs/api/responses Sends a POST request to the /v1/responses endpoint with a JSON body specifying the model kimi-k3, input, text, and a function tool. Replace <token> with a valid API key and <string> placeholders with actual values. ```bash curl --request POST \ --url https://api.moonshot.cn/v1/responses \ --header 'Authorization: Bearer <token>' \ --header 'Content-Type: application/json' \ --data ' { "model": "kimi-k3", "input": "<string>", "text": {}, "tools": [ { "type": "function", "name": "<string>", "description": "<string>", "parameters": {}, "strict": true } ] } ' ``` -------------------------------- ### Error response schema (400/500) for Messages API Source: https://platform.kimi.com/docs/api/messages Example of an error response for HTTP status codes 400 and 500. Contains a type, error object with type and message, and a request_id. ```json { "type": "error", "error": { "type": "<string>", "message": "<string>" }, "request_id": "<string>" } ``` -------------------------------- ### PHP cURL example for Messages API Source: https://platform.kimi.com/docs/api/messages Uses PHP cURL to POST a chat message to the Messages API. Sets the URL, headers, and JSON-encoded body. Outputs the response or a cURL error. ```php <?php $curl = curl_init(); curl_setopt_array($curl, [ CURLOPT_URL => "https://api.moonshot.cn/anthropic/v1/messages", CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => "", CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 30, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => "POST", CURLOPT_POSTFIELDS => json_encode([ 'model' => 'kimi-k3', 'messages' => [ [ 'role' => 'user', 'content' => '你好' ] ], 'max_tokens' => 2 ]), CURLOPT_HTTPHEADER => [ "Authorization: Bearer <token>", "Content-Type: application/json" ], ]); $response = curl_exec($curl); $err = curl_error($curl); curl_close($curl); if ($err) { echo "cURL Error #:" . $err; } else { echo $response; } ``` -------------------------------- ### Success response schema for Messages API Source: https://platform.kimi.com/docs/api/messages Example of a successful response from the Messages API. Includes fields like id, type, role, model, content (with thinking and signature), stop_reason, stop_sequence, and usage details. ```json { "id": "<string>", "type": "message", "role": "assistant", "model": "<string>", "content": [ { "type": "thinking", "thinking": "<string>", "signature": "<string>" } ], "stop_reason": "end_turn", "stop_sequence": "<string>", "usage": { "input_tokens": 123, "output_tokens": 123, "cache_read_input_tokens": 123, "cache_creation_input_tokens": 123, "cache_creation": { "ephemeral_5m_input_tokens": 123, "ephemeral_1h_input_tokens": 123 }, "output_tokens_details": { "thinking_tokens": 123 } } } ``` -------------------------------- ### Create a model response with Ruby Net::HTTP Source: https://platform.kimi.com/docs/api/responses Uses Net::HTTP to POST a JSON body to the /v1/responses endpoint. The body includes the model kimi-k3, input, text, and a function tool. Prints the response body. ```ruby require 'uri' require 'net/http' url = URI("https://api.moonshot.cn/v1/responses") http = Net::HTTP.new(url.host, url.port) http.use_ssl = true request = Net::HTTP::Post.new(url) request["Authorization"] = 'Bearer <token>' request["Content-Type"] = 'application/json' request.body = "{\n \"model\": \"kimi-k3\",\n \"input\": \"<string>\",\n \"text\": {},\n \"tools\": [\n {\n \"type\": \"function\",\n \"name\": \"<string>\",\n \"description\": \"<string>\",\n \"parameters\": {},\n \"strict\": true\n }\n ]\n}" response = http.request(request) puts response.read_body ``` -------------------------------- ### Create a model response with JavaScript fetch Source: https://platform.kimi.com/docs/api/responses Uses the Fetch API to POST a JSON body to the /v1/responses endpoint. The body includes the model kimi-k3, input, text, and a function tool. Logs the parsed JSON response or any error. ```javascript const options = { method: 'POST', headers: {Authorization: 'Bearer <token>', 'Content-Type': 'application/json'}, body: JSON.stringify({ model: 'kimi-k3', input: '<string>', text: {}, tools: [ { type: 'function', name: '<string>', description: '<string>', parameters: {}, strict: true } ] }) }; fetch('https://api.moonshot.cn/v1/responses', options) .then(res => res.json()) .then(res => console.log(res)) .catch(err => console.error(err)); ``` -------------------------------- ### Create a model response with Python requests Source: https://platform.kimi.com/docs/api/responses Uses the requests library to POST a JSON payload to the /v1/responses endpoint. The payload includes the model kimi-k3, input, text, and a function tool definition. Prints the response text. ```python import requests url = "https://api.moonshot.cn/v1/responses" payload = { "model": "kimi-k3", "input": "<string>", "text": {}, "tools": [ { "type": "function", "name": "<string>", "description": "<string>", "parameters": {}, "strict": True } ] } headers = { "Authorization": "Bearer <token>", "Content-Type": "application/json" } response = requests.post(url, json=payload, headers=headers) print(response.text) ``` -------------------------------- ### Create a model response with Java Unirest Source: https://platform.kimi.com/docs/api/responses Uses the Unirest HTTP client to POST a JSON body to the /v1/responses endpoint. The body includes the model kimi-k3, input, text, and a function tool. Returns the response as a string. ```java HttpResponse<String> response = Unirest.post("https://api.moonshot.cn/v1/responses") .header("Authorization", "Bearer <token>") .header("Content-Type", "application/json") .body("{\n \"model\": \"kimi-k3\",\n \"input\": \"<string>\",\n \"text\": {},\n \"tools\": [\n {\n \"type\": \"function\",\n \"name\": \"<string>\",\n \"description\": \"<string>\",\n \"parameters\": {},\n \"strict\": true\n }\n ]\n}") .asString(); ``` -------------------------------- ### 创建环境 - 201 成功响应 Source: https://platform.kimi.com/docs/api-reference/环境/创建环境 创建环境成功时返回 201,响应体包含环境 ID、配置、build_status 为 building、时间戳和 scope。 ```json { "id": "env_01h455vb4pex5vsknk084sn02q", "name": "Data analysis", "description": "用于数据分析任务的执行环境", "config": { "type": "cloud", "networking": { "type": "limited", "allowed_hosts": [ "example.invalid" ] }, "packages": { "pip": [ "pandas==2.3.2" ] }, "setup_script": "mkdir -p /workspace/output" }, "metadata": {}, "build_status": "building", "created_at": "2026-08-26T12:00:00Z", "updated_at": "2026-08-26T12:00:00Z", "scope": "project" } ``` -------------------------------- ### 创建会话请求示例 (Ruby) Source: https://platform.kimi.com/docs/api-reference/会话/创建会话 使用 Ruby 的 Net::HTTP 发送 POST 请求到 https://api.moonshot.cn/v1/sessions,需要提供 Authorization 和 Content-Type 头,请求体包含 agent_id 和 environment_id。 ```ruby require 'uri' require 'net/http' url = URI("https://api.moonshot.cn/v1/sessions") http = Net::HTTP.new(url.host, url.port) http.use_ssl = true request = Net::HTTP::Post.new(url) request["Authorization"] = 'Bearer <token>' request["Content-Type"] = 'application/json' request.body = "{\n \"agent_id\": \"agent_01h455vb4pex5vsknk084sn02q\",\n \"environment_id\": \"env_01h455vb4pex5vsknk084sn02q\"\n}" response = http.request(request) puts response.read_body ``` -------------------------------- ### POST /v1/vaults/{id}/credentials/batch Source: https://platform.kimi.com/docs/api-reference/凭据库/批量创建凭据 该接口用于批量创建凭据,请求体包含凭据列表,每个凭据包含显示名称和认证信息。 ```APIDOC ## POST /v1/vaults/{id}/credentials/batch ### Description 批量创建凭据。 ### Method POST ### Endpoint `https://api.moonshot.cn/v1/vaults/{id}/credentials/batch` ### Parameters #### Path Parameters - **id** (string) - Required - 凭据库 ID。 #### Request Body - **items** (array) - Required - 要创建的凭据列表。 - **display_name** (string) - Required - 凭据的显示名称。 - **auth** (object) - Required - 认证信息。 - **type** (string) - Required - 认证类型,例如 `static_bearer`。 - **mcp_server_url** (string) - Optional - MCP 服务器 URL。 - **token** (string) - Optional - 访问令牌。 ### Request Example ```json { "items": [ { "display_name": "MCP access", "auth": { "type": "static_bearer", "mcp_server_url": "https://example.invalid/mcp", "token": "sk-example" } } ] } ``` ### Response #### Success Response (201) - **items** (array) - 创建的凭据列表。 - **id** (string) - 凭据 ID。 - **vault_id** (string) - 凭据库 ID。 - **display_name** (string) - 显示名称。 - **auth** (object) - 认证信息。 - **type** (string) - 认证类型。 - **mcp_server_url** (string) - MCP 服务器 URL。 - **created_at** (string) - 创建时间。 - **updated_at** (string) - 更新时间。 #### Response Example ```json { "items": [ { "id": "vcrd_01h455vb4pex5vsknk084sn02q", "vault_id": "vlt_01h455vb4pex5vsknk084sn02q", "display_name": "MCP access", "auth": { "type": "static_bearer", "mcp_server_url": "https://example.invalid/mcp" }, "created_at": "2026-01-01T00:00:00Z", "updated_at": "2026-01-01T00:00:00Z" } ] } ``` #### Error Responses - **400** - 请求参数错误。 - **401** - 未授权。 - **404** - 资源不存在。 - **409** - 冲突。 - **429** - 请求过多。 - **default** - 其他错误。 错误响应示例: ```json { "error": { "type": "invalid_parameter_error", "message": "<string>", "param": "<string>", "request_id": "<string>" } } ``` ``` -------------------------------- ### 创建触发器 - Java (Unirest) Source: https://platform.kimi.com/docs/api-reference/触发器/创建触发器 使用 Unirest 库发送 POST 请求创建触发器,需要设置 Authorization 和 Content-Type 头。 ```java HttpResponse<String> response = Unirest.post("https://api.moonshot.cn/v1/triggers") .header("Authorization", "Bearer <token>") .header("Content-Type", "application/json") .body("{\n \"name\": \"Daily summary\",\n \"agent_id\": \"agent_01h455vb4pex5vsknk084sn02q\",\n \"environment_id\": \"env_01h455vb4pex5vsknk084sn02q\",\n \"policy\": {\n \"type\": \"cron\",\n \"expression\": \"0 9 * * *\",\n \"timezone\": \"UTC\"\n }\n}") .asString(); ``` -------------------------------- ### Create credential with Ruby Net::HTTP Source: https://platform.kimi.com/docs/api-reference/凭据库/创建凭据 Sends a POST request to create a credential in a vault. Requires a valid bearer token and a vault ID. The request body includes the auth type, MCP server URL, and token. ```ruby require 'uri' require 'net/http' url = URI("https://api.moonshot.cn/v1/vaults/{id}/credentials") http = Net::HTTP.new(url.host, url.port) http.use_ssl = true request = Net::HTTP::Post.new(url) request["Authorization"] = 'Bearer <token>' request["Content-Type"] = 'application/json' request.body = "{\n \"auth\": {\n \"type\": \"static_bearer\",\n \"mcp_server_url\": \"https://example.invalid/mcp\",\n \"token\": \"sk-example\"\n }\n}" response = http.request(request) puts response.read_body ```