Kimi2 with openrouter

AI
Author

Tony D

Published

July 18, 2025

OpenRouter is a platform that provides a unified API for accessing a wide variety of large language models (LLMs) from different providers like Anthropic, Google, and Meta. It simplifies the process of integrating and using these models for developers, offering a single point of access instead of managing multiple APIs and accounts

get api key from https://openrouter.ai/

load package

Code
from openai import OpenAI
import keyring

define model

Code
client = OpenAI(
  base_url="https://openrouter.ai/api/v1",
  api_key=keyring.get_password("system", "openrouter"),
)

call model

Code
completion = client.chat.completions.create(
  extra_headers={
    "HTTP-Referer": "<YOUR_SITE_URL>", # Optional. Site URL for rankings on openrouter.ai.
    "X-Title": "<YOUR_SITE_NAME>", # Optional. Site title for rankings on openrouter.ai.
  },
  extra_body={},
  model="moonshotai/kimi-k2:free",
  messages=[
    {
      "role": "user",
      "content": "What is the meaning of life?"
    }
  ]
)

get result

Code
print(completion.choices[0].message.content)