{"slug": "kimi-k2-api-integration-a-no-fluff-getting-started", "title": "Kimi K2 API Integration: A No-Fluff Getting Started", "summary": "Moonshot AI's Kimi K2, a Mixture-of-Experts model, supports native image understanding through the standard chat-completions API, enabling multimodal tasks like document QA and screenshot analysis. The integration guide demonstrates setup via cURL and Python, covering streaming, function calling, and token management for production use.", "body_md": "Kimi K2 is Moonshot AI's flagship Mixture-of-Experts model, and the first thing developers notice is what it can see. Unlike text-only models, K2 takes images natively through the same chat-completions interface you already know — an `image_url`\n\narray inside the message content is all it takes. If your workload involves long-document QA, screenshot analysis, or an agent swarm that needs to read what's on screen, K2 is worth a serious look.\n\nSetup is deliberately boring. You hit the standard v1 endpoint, send the same request shape used everywhere, and turn on multimodal only when you need it. This guide walks the fastest path from zero to a working request: cURL first, then Python, then the parts — function calling, error handling, token math — that tend to trip people up in production.\n\n`kimi-k2`\n\n`https://api.moonshot.cn/v1`\n\n```\nexport MOONSHOT_API_KEY=\"sk-...\"\n\ncurl https://api.moonshot.cn/v1/chat/completions \\\n  -H \"Authorization: Bearer $MOONSHOT_API_KEY\" \\\n  -H \"Content-Type: application/json\" \\\n  -d '{\n    \"model\": \"kimi-k2\",\n    \"messages\": [{\"role\": \"user\", \"content\": \"Summarize the key points of this contract.\"}],\n    \"max_tokens\": 1024\n  }'\npython\nimport os\nfrom openai import OpenAI\n\nclient = OpenAI(\n    api_key=os.getenv(\"MOONSHOT_API_KEY\"),\n    base_url=\"https://api.moonshot.cn/v1\",\n)\n\nresp = client.chat.completions.create(\n    model=\"kimi-k2\",\n    messages=[{\"role\": \"user\", \"content\": \"Rewrite this error message in plain language: \" + err}],\n    max_tokens=512,\n)\nprint(resp.choices[0].message.content)\n```\n\nThe biggest difference between K2 and text-only models like DeepSeek V4 is that `content`\n\ncan be an array of parts:\n\n```\nresp = client.chat.completions.create(\n    model=\"kimi-k2\",\n    messages=[{\n        \"role\": \"user\",\n        \"content\": [\n            {\"type\": \"text\", \"text\": \"What is wrong with this dashboard? Be specific.\"},\n            {\"type\": \"image_url\", \"image_url\": {\"url\": \"https://example.com/dashboard.png\"}},\n        ],\n    }],\n)\n```\n\nPass remote URLs or base64 data URLs. Each image consumes tokens against the 256K window, so keep images reasonably sized and crop where you can. This capability alone is why K2 often wins for document and screen-understanding tasks — for a text-only comparison, see our [DeepSeek V4 guide](https://taotok.io/deepseek-v4-api-guide).\n\n```\nstream = client.chat.completions.create(\n    model=\"kimi-k2\",\n    messages=[{\"role\": \"user\", \"content\": \"Give me 5 tips for prompt engineering.\"}],\n    stream=True,\n)\nfor chunk in stream:\n    print(chunk.choices[0].delta.content or \"\", end=\"\")\n```\n\nDefine tools the usual way, then let the model emit `tool_calls`\n\n:\n\n```\ntools = [{\n    \"type\": \"function\",\n    \"function\": {\n        \"name\": \"get_weather\",\n        \"description\": \"Get the current weather for a city\",\n        \"parameters\": {\n            \"type\": \"object\",\n            \"properties\": {\n                \"city\": {\"type\": \"string\"}\n            },\n            \"required\": [\"city\"],\n        },\n    },\n}]\n\nresp = client.chat.completions.create(\n    model=\"kimi-k2\",\n    messages=[{\"role\": \"user\", \"content\": \"What's the weather in Tokyo?\"}],\n    tools=tools,\n)\nprint(resp.choices[0].message.tool_calls)\n```\n\nExecute the tool, append the result as a `tool`\n\nrole message, and loop until the model finishes.\n\nThe extended Kimi K2 guide with more examples is on the taotok.io blog at [https://taotok.io/kimi-k2-api-integration](https://taotok.io/kimi-k2-api-integration), and if you're deciding between K2 and DeepSeek V4, the [side-by-side comparison](https://taotok.io/deepseek-v4-vs-kimi-k2) will save you an afternoon.", "url": "https://wpnews.pro/news/kimi-k2-api-integration-a-no-fluff-getting-started", "canonical_source": "https://dev.to/zhangjj1988/kimi-k2-api-integration-a-no-fluff-getting-started-5361", "published_at": "2026-08-18 13:30:00+00:00", "updated_at": "2026-08-18 13:44:32.874163+00:00", "lang": "en", "topics": ["large-language-models", "developer-tools"], "entities": ["Moonshot AI", "Kimi K2", "DeepSeek V4", "OpenAI", "taotok.io"], "alternates": {"html": "https://wpnews.pro/news/kimi-k2-api-integration-a-no-fluff-getting-started", "markdown": "https://wpnews.pro/news/kimi-k2-api-integration-a-no-fluff-getting-started.md", "text": "https://wpnews.pro/news/kimi-k2-api-integration-a-no-fluff-getting-started.txt", "jsonld": "https://wpnews.pro/news/kimi-k2-api-integration-a-no-fluff-getting-started.jsonld"}}