Moonshot API Integration: Connect Your App to Our API
If your code already talks to an OpenAI-style API, integrating this one is a configuration change. This page covers the setup, the features that work, and the places where behavior differs from what you may assume.

What integration means here
A moonshot api integration, in practical terms, is an app that sends chat completion requests in the OpenAI format to a hosted model. This service speaks that format. It is not Moonshot's own API and not a router across vendors: it is a hosted endpoint for one open-weight model, run by an independent provider, tuned to answer without content refusals for lawful adult use.
That has a useful consequence. If you have already built against Moonshot's API, or against any OpenAI-style API, you do not need a new client library. You change the base URL, the key and the model name, and the rest of your request code keeps working. The details of a vendor's special extras are their own, so check their documentation for anything not listed below.
Start by getting a key from the API key page. Sign-up is with Google or with email and password, and new users can claim $0.50 of trial credit with one click, with no card needed.
The minimal integration
Here is the smallest working example in Python. The values to change are marked by position: URL, key, model.
from openai import OpenAI
client = OpenAI(
base_url="https://api.moonshotapi.top/v1",
api_key="YOUR_KEY",
)
reply = client.chat.completions.create(
model="uncensored",
messages=[{"role": "user", "content": "Explain idempotency in two sentences."}],
)
print(reply.choices[0].message.content)
And the same call in plain HTTP, which is what you would use from a language without an SDK:
curl https://api.moonshotapi.top/v1/chat/completions \
-H "Authorization: Bearer YOUR_KEY" \
-H "Content-Type: application/json" \
-d '{"model":"uncensored","messages":[{"role":"user","content":"Hi"}]}'
Two methods exist: POST /v1/chat/completions and GET /v1/models. Anything else you might call on other APIs, such as embeddings or image endpoints, is not offered. The quickstart shows the first request step by step.
Streaming responses
Set stream: true and the API sends server-sent events in the usual chunk format. Usage is sent at the end of the stream, so you can log token counts without a second call.
import OpenAI from "openai";
const client = new OpenAI({
baseURL: "https://api.moonshotapi.top/v1",
apiKey: process.env.API_KEY,
});
const stream = await client.chat.completions.create({
model: "uncensored",
messages: [{ role: "user", content: "List five uses of a hash map." }],
stream: true,
});
for await (const part of stream) {
const piece = part.choices[0]?.delta?.content;
if (piece) process.stdout.write(piece);
}
One integration detail matters here. A streamed request holds one of your concurrency slots until the stream finishes. If your backend proxies streams to browsers, a slow client keeps a slot busy, so set timeouts and cancel when the browser disconnects.
Tool calling and JSON mode
Tool calling follows the OpenAI shape: you pass tools, optionally tool_choice, read tool_calls from the reply (they stream too), run the function yourself, and send the result back as a message with role: "tool".
import json
from openai import OpenAI
client = OpenAI(base_url="https://api.moonshotapi.top/v1", api_key="YOUR_KEY")
tools = [{
"type": "function",
"function": {
"name": "get_weather",
"description": "Current weather for a city",
"parameters": {
"type": "object",
"properties": {"city": {"type": "string"}},
"required": ["city"],
},
},
}]
messages = [{"role": "user", "content": "Is it raining in Lisbon?"}]
first = client.chat.completions.create(
model="uncensored", messages=messages, tools=tools
)
call = first.choices[0].message.tool_calls[0]
args = json.loads(call.function.arguments)
result = {"city": args["city"], "conditions": "light rain"}
messages.append(first.choices[0].message)
messages.append({"role": "tool", "tool_call_id": call.id, "content": json.dumps(result)})
final = client.chat.completions.create(
model="uncensored", messages=messages, tools=tools
)
print(final.choices[0].message.content)
In real code, check that tool_calls exists before indexing it, since the model may answer directly. Validate the parsed arguments as you would any user input.
For structured output, set response_format={"type": "json_object"}. Also tell the model in the prompt to return JSON, and parse defensively: JSON mode asks for valid JSON, but your own schema is your responsibility to check.
Wiring it into frameworks and tools
Frameworks that wrap the OpenAI client generally expose the same three settings. In LangChain for Python, the langchain-openai package takes them on ChatOpenAI:
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(
base_url="https://api.moonshotapi.top/v1",
api_key="YOUR_KEY",
model="uncensored",
)
print(llm.invoke("Name three sorting algorithms.").content)
In the Vercel AI SDK, createOpenAI from @ai-sdk/openai accepts a baseURL and apiKey, and you then pick the model by ID. Chat front ends such as SillyTavern let you add an OpenAI-compatible endpoint by pasting the same URL, key and model name into a custom connection form.
A rule of thumb for any tool: if it has a field called base URL, API URL or endpoint, it can point here. If it only offers a fixed list of providers with no custom option, it cannot, and you would need a proxy or a different client.
| Setting | Value |
|---|---|
| Base URL | https://api.moonshotapi.top/v1 |
| Auth header | Authorization: Bearer YOUR_KEY |
| Model | uncensored |
Testing the integration before launch
Most integration bugs show up in a few predictable places. Run through this list against a funded key or the trial key before real users arrive.
- Happy path. One plain request returns text and a usage object.
- Streaming path. A streamed request delivers chunks in order and ends cleanly. Cancel one halfway and confirm your server stops reading, so the concurrency slot is freed.
- Tool path. A prompt that should trigger your function does so, and your code survives a reply with no tool call.
- JSON path. Feed the output to your parser on twenty varied inputs. Count the failures and decide what your fallback does.
- Limit path. Deliberately send ten parallel requests on a trial key. You should see 429 responses with error code
concurrency, and your retry code should recover without losing messages. - Long-input path. Send a prompt near 90,000 tokens once. Confirm your code handles the slower response and that the prompt plus the expected reply fits in the 100,000-token window.
Error handling reference
| Symptom | Likely cause | Action |
|---|---|---|
| 429, code rate_limited | Too many requests per minute | Slow the start rate, retry with backoff |
| 429, code concurrency | More than 8 in flight (2 on trial) | Cap workers, close idle streams |
| 429 with Retry-After: 5 | Temporary overload | Wait 5 seconds, retry |
| Not found | Missing /v1 or doubled path | Fix the base URL |
| Model not found | Wrong model name | Use uncensored |
Keep the request ID of every failure in your logs. It costs nothing and turns a vague bug report into something support can trace.
Production concerns
Limits
A full key allows 8 concurrent requests and 300 requests per minute. A trial key allows 2 and 60. Past either limit the API returns HTTP 429, with error code rate_limited for requests per minute or concurrency for too many at once. On temporary overload it also returns 429 with a Retry-After: 5 header. Wait that long and retry with jitter. The Together AI troubleshooting guide on this site walks through a similar retry setup for another provider, and the idea transfers directly.
Context and body size
The window is 100,000 tokens for prompt plus completion, and a request body can be up to 8 MB. You may leave max_tokens unset, though a cap guards against very long replies.
Observability
Log the X-Request-Id response header with every call. It is the handle support needs to find a request. The X-Balance-USD header gives your remaining credit, which is handy for an alert when it drops below a threshold.
Sampling parameters
The parameters temperature, top_p, stop, seed and the frequency and presence penalties are passed through, so existing tuning carries over. A fixed seed is a useful way to make test runs comparable.
Data
Prompts are not used for training. Failed or refused requests are not charged.
Budgeting the integration
Pricing is $0.15 per 1M input tokens and $0.50 per 1M output tokens, from prepaid credit that never expires. A support-style assistant that sends 3,000 input tokens and receives 600 output tokens per call costs 0.003 x $0.15 = $0.00045 for input plus 0.0006 x $0.50 = $0.0003 for output, so $0.00075 per call. At 20,000 calls a month that is $15.00. Top-ups start at $20 and run to $500 in whole amounts, with +5% from $50, +10% from $100 and +15% from $200. Payment is by crypto (USDT on TRC20, USDC on Base). The pricing page has the details.
If your integration also needs embeddings, image generation or a specific vendor's model, keep that part on the provider that offers it and send only the text generation here. Mixing providers behind one interface in your own code is a normal and tidy way to do it.
One more habit pays off early: keep the system prompt in one file and version it. When replies change, you want to know whether the prompt, the sampling settings or your code moved. With a fixed seed and the same prompt you can rerun a request and compare, which makes regressions much easier to find than reading logs of free-form output.
More guides
Questions and answers
Do I need a special SDK for this API?
No. Any OpenAI SDK or compatible client works after you change the base URL and key and set the model to uncensored.
Does it support function calling and JSON output?
Yes. Tool calling with tools and tool_choice works, including streamed tool_calls, and JSON mode works with response_format set to json_object.
Is this the same as Moonshot's API?
No. It is an independent endpoint for one open-weight model. It uses the same request format as OpenAI-style APIs, which is why existing client code works.
What should I do on a 429 response?
Check the error code. Wait and retry with backoff for rate_limited, reduce parallel calls for concurrency, and wait for the Retry-After value if it is present.
Your key is one form away
Create an account, copy the key, change the base URL. That is the whole setup.