Skip to main content

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.

PyPI Version Python Versions License Telegram


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

  1. Open kimi.moonshot.cn in your browser
  2. Open DevToolsNetwork → any request
  3. 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

Telegram Channel

Join for Python tutorials, projects, and updates → t.me/pythontodayz


License

MIT — made by addy

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

p2d_kimi-0.1.1.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

p2d_kimi-0.1.1-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

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

Hashes for p2d_kimi-0.1.1.tar.gz
Algorithm Hash digest
SHA256 6023cc9424e001d89c9965e649b9a44e9e5fc811285a7946d51445e60cda94cb
MD5 47ea48f316b080804e0534175de73559
BLAKE2b-256 6879a5de5285ee3647af448bf9330640a548cf92310e2b5f0248cbf614ea4c4b

See more details on using hashes here.

Provenance

The following attestation bundles were made for p2d_kimi-0.1.1.tar.gz:

Publisher: publish.yml on pooraddyy/p2d-kimi

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for p2d_kimi-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 835b1b777fe9fb977ec66db2fb07d929eb243bddadd75405ed977a963a22ade0
MD5 f65f499d8f70e287a86f3c75db452079
BLAKE2b-256 27d8502555ed872020445e3b27fc2f205756f6bdf1edcd123c1a729dfaf817f2

See more details on using hashes here.

Provenance

The following attestation bundles were made for p2d_kimi-0.1.1-py3-none-any.whl:

Publisher: publish.yml on pooraddyy/p2d-kimi

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page