Unofficial Python client for Kimi AI — reverse-engineered, no API key required
Project description
p2d-kimi
Unofficial Python client for Kimi AI (kimi.moonshot.cn). No API key needed — just your bearer token.
Features
- No API key required — uses your browser session token
- Persistent sessions — messages stay in the same conversation automatically
- Streaming support — yield reply chunks in real time
- Web search — toggle Kimi's built-in search on any message
- Multi-turn conversations — full context preserved across calls
- Lightweight — only depends on
requests
Install
pip install p2d-kimi
Quick Start
from p2dkimi import KimiClient
client = KimiClient(token="your-kimi-bearer-token")
reply = client.chat("What is the capital of France?")
print(reply)
Multi-Turn Conversation
Messages are automatically grouped into the same session. The client creates one session on the first call and reuses it for all subsequent calls.
client = KimiClient(token="your-token")
reply1 = client.chat("My name is Alex.")
reply2 = client.chat("What is my name?")
print(reply2)
Starting a New Session
Call new_chat() to explicitly start a fresh conversation.
client.new_chat(name="fresh-start")
reply = client.chat("Let's start over.")
Streaming
from p2dkimi import KimiClient
client = KimiClient(token="your-token")
for chunk in client.stream("Explain quantum computing in simple terms"):
print(chunk, end="", flush=True)
With Web Search
reply = client.chat("Latest news on AI today", use_search=True)
Manual Chat ID
You can still pass a chat_id directly to target a specific conversation.
chat_id = client.new_chat(name="my-session")
reply1 = client.chat("My name is Alex.", chat_id=chat_id)
reply2 = client.chat("What is my name?", chat_id=chat_id)
print(reply2)
API Reference
KimiClient(token, model="kimi", timeout=60)
| Parameter | Type | Description |
|---|---|---|
token |
str |
Bearer JWT from kimi.moonshot.cn |
model |
str |
"kimi" (default), "k1", "ok-computer", etc. |
timeout |
int |
HTTP timeout in seconds |
Methods
| Method | Returns | Description |
|---|---|---|
chat(message, chat_id, history, use_search) |
str |
Send a message, get full reply |
stream(message, chat_id, history, use_search) |
Generator[str] |
Send a message, yield reply chunks |
new_chat(name) |
str |
Create a new conversation and set it as active |
Getting Your Token
- Open kimi.moonshot.cn in your browser
- Open DevTools → Network → any request
- Copy the value of
Authorization: Bearer <token>
Or from iOS/Android using a MITM proxy (e.g. mitmproxy, Charles).
The token is a JWT that lasts ~30 days.
How It Works
KimiClient.chat("hello")
→ new_chat() POST /api/chat → chat_id (stored in self.chat_id)
→ stream_completion() POST /api/chat/{id}/completion/stream → SSE chunks
→ collect chunks event=cmpl text="Hello!"
→ return full string
KimiClient.chat("follow-up")
→ reuses self.chat_id (no new session created)
→ stream_completion() POST /api/chat/{id}/completion/stream → SSE chunks
REST API Server
The included Flask server exposes HTTP endpoints:
| Endpoint | Method | Description |
|---|---|---|
POST /chat/new |
POST | Create a new session → returns chat_id |
POST /chat |
POST | Send a message → returns reply + chat_id |
POST /chat/stream |
POST | Stream a reply as plain text |
GET /health |
GET | Health check |
Pass your token via Authorization: Bearer <token> header. Include chat_id in the body to continue an existing session.
Demo
See DEMO.md for a full working Telegram bot built with this library.
Community
Join for Python tutorials, projects, and updates → t.me/pythontodayz
License
MIT — made by addy
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file p2d_kimi-0.1.1.tar.gz.
File metadata
- Download URL: p2d_kimi-0.1.1.tar.gz
- Upload date:
- Size: 5.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6023cc9424e001d89c9965e649b9a44e9e5fc811285a7946d51445e60cda94cb
|
|
| MD5 |
47ea48f316b080804e0534175de73559
|
|
| BLAKE2b-256 |
6879a5de5285ee3647af448bf9330640a548cf92310e2b5f0248cbf614ea4c4b
|
Provenance
The following attestation bundles were made for p2d_kimi-0.1.1.tar.gz:
Publisher:
publish.yml on pooraddyy/p2d-kimi
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
p2d_kimi-0.1.1.tar.gz -
Subject digest:
6023cc9424e001d89c9965e649b9a44e9e5fc811285a7946d51445e60cda94cb - Sigstore transparency entry: 1615672677
- Sigstore integration time:
-
Permalink:
pooraddyy/p2d-kimi@a8eb11065504c35fea4c17c236f15ae187256bbb -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/pooraddyy
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@a8eb11065504c35fea4c17c236f15ae187256bbb -
Trigger Event:
release
-
Statement type:
File details
Details for the file p2d_kimi-0.1.1-py3-none-any.whl.
File metadata
- Download URL: p2d_kimi-0.1.1-py3-none-any.whl
- Upload date:
- Size: 6.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
835b1b777fe9fb977ec66db2fb07d929eb243bddadd75405ed977a963a22ade0
|
|
| MD5 |
f65f499d8f70e287a86f3c75db452079
|
|
| BLAKE2b-256 |
27d8502555ed872020445e3b27fc2f205756f6bdf1edcd123c1a729dfaf817f2
|
Provenance
The following attestation bundles were made for p2d_kimi-0.1.1-py3-none-any.whl:
Publisher:
publish.yml on pooraddyy/p2d-kimi
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
p2d_kimi-0.1.1-py3-none-any.whl -
Subject digest:
835b1b777fe9fb977ec66db2fb07d929eb243bddadd75405ed977a963a22ade0 - Sigstore transparency entry: 1615672693
- Sigstore integration time:
-
Permalink:
pooraddyy/p2d-kimi@a8eb11065504c35fea4c17c236f15ae187256bbb -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/pooraddyy
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@a8eb11065504c35fea4c17c236f15ae187256bbb -
Trigger Event:
release
-
Statement type: