Call the Claude API from Python and Node.js with a RocketKey key
Use a RocketKey API key in your code: the Anthropic or OpenAI SDK for Python and Node.js, the right base URL, model choice and errors 401, 402, 429.
Updated 25 Oct 2026
A RocketKey key is not only for Claude Code or Cursor: you can call the API from your own code with the Anthropic SDK (Messages API) or the OpenAI SDK (Chat Completions API). Change two things: the base URL points to RocketKey, and the API key is your RocketKey key. Every request counts against the same daily USD allowance of your plan.
No key yet? Read Get started first.
1. Put the key in an environment variable
Never write the key into your code. The examples below read it from ROCKETKEY_API_KEY:
export ROCKETKEY_API_KEY='sk-rk-YOUR_KEY_HERE'$env:ROCKETKEY_API_KEY = 'sk-rk-YOUR_KEY_HERE'set ROCKETKEY_API_KEY=sk-rk-YOUR_KEY_HEREThe variable lasts for that terminal window only. In a real project you can keep it in a .env file (add .env to .gitignore).
2. Anthropic SDK (recommended for Claude models)
Install: pip install anthropic for Python, npm install @anthropic-ai/sdk for Node.js. The Anthropic SDK base URL has no /v1 at the end: the SDK adds /v1/messages itself.
import os
from anthropic import Anthropic
client = Anthropic(
api_key=os.environ["ROCKETKEY_API_KEY"],
base_url="https://rocketkeyapi.indexes.social",
)
message = client.messages.create(
model="claude-sonnet-5-5",
max_tokens=1024,
messages=[{"role": "user", "content": "Hello"}],
)
for block in message.content:
if block.type == "text":
print(block.text)import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic({
apiKey: process.env.ROCKETKEY_API_KEY,
baseURL: "https://rocketkeyapi.indexes.social",
});
const message = await client.messages.create({
model: "claude-sonnet-5-5",
max_tokens: 1024,
messages: [{ role: "user", content: "Hello" }],
});
for (const block of message.content) {
if (block.type === "text") console.log(block.text);
}Some Claude models always reason before answering, so message.content can start with a thinking block. That is why the code prints text blocks only.
Use the Anthropic SDK when you need the full Claude feature set: tool use, images, streaming, prompt caching. The SDK reference is on platform.claude.com.
3. OpenAI SDK (for code that already uses OpenAI)
RocketKey also accepts the OpenAI Chat Completions format, so code built on the openai library only needs a new base URL and model. Install: pip install openai or npm install openai. With the OpenAI SDK, the base URL includes /v1.
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["ROCKETKEY_API_KEY"],
base_url="https://rocketkeyapi.indexes.social/v1",
)
response = client.chat.completions.create(
model="claude-sonnet-5-5",
messages=[{"role": "user", "content": "Hello"}],
)
print(response.choices[0].message.content)import OpenAI from "openai";
const client = new OpenAI({
apiKey: process.env.ROCKETKEY_API_KEY,
baseURL: "https://rocketkeyapi.indexes.social/v1",
});
const response = await client.chat.completions.create({
model: "claude-sonnet-5-5",
messages: [{ role: "user", content: "Hello" }],
});
console.log(response.choices[0].message.content);4. Pick a model
Replace claude-sonnet-5-5 with the model ID you want, from the Models page. Sonnet is the balanced choice for most work; Haiku is far cheaper for simple, high-volume tasks. See Choose a Claude model for coding for a cost comparison. A wrong model ID gives error 404.
List the models your key can use (free, no allowance used):
Lists the models your key can use. A JSON list means the key works.
curl "https://rocketkeyapi.indexes.social/v1/models" \
-H "Authorization: Bearer sk-rk-YOUR_KEY_HERE"curl.exe "https://rocketkeyapi.indexes.social/v1/models" -H "Authorization: Bearer sk-rk-YOUR_KEY_HERE"5. Handle errors
| Code | Meaning | What to do |
|---|---|---|
| 401 | Wrong, deleted or blocked key. | Check ROCKETKEY_API_KEY. See error 401. |
| 402 | The day's USD allowance is used up. | Wait for 00:00 (GMT+7) or upgrade. See error 402. |
| 429 | Over the plan's requests per minute (60, 120 or 240). | Send fewer requests in parallel, retry after a few seconds. See error 429. |
| 5xx, 529 | The model side is overloaded or failing for a moment. | Retry later. |
Per Anthropic's documentation, the Python SDK retries connection errors, 429 and 5xx twice by default with exponential backoff. Change it with max_retries when you create the client. For 402, retrying does nothing until the allowance resets.
Tips for batch jobs
- Cost is input and output tokens at the prices on the Models page. Track it per key under Dashboard → Usage, By key.
- Give each app or environment (dev, CI) its own key, so you can block exactly that one. See Manage API keys.
- Stream long answers (
stream=Truein Python) so the connection is not dropped midway. - All keys of an account share the per-minute request limit. When running many processes, cap concurrent requests to avoid 429.