# Kimi K2 API Integration: A No-Fluff Getting Started

> Source: <https://dev.to/zhangjj1988/kimi-k2-api-integration-a-no-fluff-getting-started-5361>
> Published: 2026-08-18 13:30:00+00:00

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`

array 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.

Setup 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.

`kimi-k2`

`https://api.moonshot.cn/v1`

```
export MOONSHOT_API_KEY="sk-..."

curl https://api.moonshot.cn/v1/chat/completions \
  -H "Authorization: Bearer $MOONSHOT_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "kimi-k2",
    "messages": [{"role": "user", "content": "Summarize the key points of this contract."}],
    "max_tokens": 1024
  }'
python
import os
from openai import OpenAI

client = OpenAI(
    api_key=os.getenv("MOONSHOT_API_KEY"),
    base_url="https://api.moonshot.cn/v1",
)

resp = client.chat.completions.create(
    model="kimi-k2",
    messages=[{"role": "user", "content": "Rewrite this error message in plain language: " + err}],
    max_tokens=512,
)
print(resp.choices[0].message.content)
```

The biggest difference between K2 and text-only models like DeepSeek V4 is that `content`

can be an array of parts:

```
resp = client.chat.completions.create(
    model="kimi-k2",
    messages=[{
        "role": "user",
        "content": [
            {"type": "text", "text": "What is wrong with this dashboard? Be specific."},
            {"type": "image_url", "image_url": {"url": "https://example.com/dashboard.png"}},
        ],
    }],
)
```

Pass 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).

```
stream = client.chat.completions.create(
    model="kimi-k2",
    messages=[{"role": "user", "content": "Give me 5 tips for prompt engineering."}],
    stream=True,
)
for chunk in stream:
    print(chunk.choices[0].delta.content or "", end="")
```

Define tools the usual way, then let the model emit `tool_calls`

:

```
tools = [{
    "type": "function",
    "function": {
        "name": "get_weather",
        "description": "Get the current weather for a city",
        "parameters": {
            "type": "object",
            "properties": {
                "city": {"type": "string"}
            },
            "required": ["city"],
        },
    },
}]

resp = client.chat.completions.create(
    model="kimi-k2",
    messages=[{"role": "user", "content": "What's the weather in Tokyo?"}],
    tools=tools,
)
print(resp.choices[0].message.tool_calls)
```

Execute the tool, append the result as a `tool`

role message, and loop until the model finishes.

The 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.
