Last Updated: June 2026

Kimi API Code Integration — Quickstart Examples 2026

Quick Answer

Integrate Kimi API using the OpenAI SDK with a single base URL change. Install pip install openai (Python) or npm install openai (JavaScript), set the base URL to https://api.moonshot.cn/v1, and use your kimi code api key. All chat completions parameters work identically to OpenAI.

Python Integration

Using the official OpenAI Python SDK with kimi code api:

from openai import OpenAI
import os

# Initialize client with Kimi API
client = OpenAI(
    api_key=os.environ.get("KIMI_API_KEY"),
    base_url="https://api.moonshot.cn/v1"
)

# Make a chat completion request
response = client.chat.completions.create(
    model="kimi-k2-5",  # or "kimi-k2" for lower cost
    messages=[
        {
            "role": "system",
            "content": "You are a helpful coding assistant."
        },
        {
            "role": "user",
            "content": "Write a Python function to merge two sorted arrays."
        }
    ],
    temperature=0.3,
    max_tokens=2048
)

print(response.choices[0].message.content)
print(f"Tokens used: {response.usage.total_tokens}")

JavaScript / TypeScript Integration

import OpenAI from 'openai';

const client = new OpenAI({
  apiKey: process.env.KIMI_API_KEY,
  baseURL: 'https://api.moonshot.cn/v1'
});

async function chat(userMessage) {
  const response = await client.chat.completions.create({
    model: 'kimi-k2-5',
    messages: [
      { role: 'system', content: 'You are a helpful assistant.' },
      { role: 'user', content: userMessage }
    ],
    temperature: 0.7,
    max_tokens: 2048
  });

  return response.choices[0].message.content;
}

// Usage
const answer = await chat('Explain async/await in JavaScript');
console.log(answer);

cURL Integration

curl https://api.moonshot.cn/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $KIMI_API_KEY" \
  -d '{
    "model": "kimi-k2-5",
    "messages": [
      {"role": "user", "content": "Hello, Kimi!"}
    ],
    "temperature": 0.7
  }'

SDK Support Matrix

LanguageSDKInstall CommandStatus
PythonOpenAI Python SDKpip install openaiSupported
JavaScriptOpenAI Node SDKnpm install openaiSupported
cURLNative HTTPPre-installedSupported
GoAny HTTP clientStandard libraryCompatible
Rustreqwest / async-openaicargo add reqwestCompatible
JavaOkHttp / OpenAI JavaMaven/GradleCompatible

Streaming Responses

For real-time applications, use streaming with kimi chat api:

stream = client.chat.completions.create(
    model="kimi-k2-5",
    messages=[{"role": "user", "content": "Tell me a story"}],
    stream=True
)

for chunk in stream:
    if chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="", flush=True)

Frequently Asked Questions About Kimi API

What programming languages does Kimi API support?

Kimi API supports any language that can make HTTP requests. Official OpenAI-compatible SDKs are available for Python and JavaScript/TypeScript. You can also use cURL, Go, Rust, Java, C#, Ruby, or any language with an HTTP client library.

Do I need a special SDK for Kimi API?

No. Since Kimi API is OpenAI-compatible, you can use the official OpenAI Python SDK (pip install openai) or JavaScript SDK (npm install openai). Just change the base_url to api.moonshot.cn/v1. No proprietary SDK required.

How do I use Kimi API for coding tasks?

Use the kimi-k2-5 model with a system prompt like 'You are an expert programmer.' Send your coding question or code snippet as the user message. K2.5 excels at code generation, debugging, refactoring, and code review tasks.

Does Kimi API support streaming?

Yes. Set stream: true in your API request to receive server-sent events (SSE). Streaming delivers tokens as they are generated, improving perceived latency for chat applications and real-time interfaces.

Can I use Kimi chat API for building chatbots?

Yes. Kimi API's chat completions endpoint is ideal for building chatbots. Use the messages array to maintain conversation history, set a system prompt for personality, and use K2 for cost-effective chatbot deployments.

Summary

Kimi code api integration is as simple as changing a base URL in your existing OpenAI setup. With Python, JavaScript, and cURL examples above, you can start building with kimi api immediately. Need an API key? See our API Key guide. For model comparisons, visit Models Overview.