Hosted chat or API for your own bot
To ask a model a question yourself, open CLODEX Chat after signing in. To build an interface for customers, use an API: your server receives a message, checks the user and sends a request to the selected model. Return the response to your site, mobile app or Telegram conversation.
A chatbot API does not replace application logic. Conversation storage, knowledge-base access, data-processing consent and tool permissions remain your responsibility. Never publish the primary API key in widget code delivered to a browser.
A minimal integration flow
A Telegram integration follows the same pattern: the bot handler calls your backend rather than sharing the model key with subscribers. The Telegram bot token and the model API key are separate credentials. Store them separately and never send them as conversation text.
- Create a separate CLODEX key for the bot and configure its spending quota.
- Choose a model from the catalog available to the key and verify the required protocol.
- Authenticate the user on your backend and validate message length and request frequency.
- Send the request, handle the response or error and return the result to the client.
Example request to a chat API
Set CLODEX_API_KEY and CLODEX_MODEL in your server’s protected environment. The example below requires a model with Chat Completions support. Running it submits a generation request and uses your balance; reading this page sends nothing.
Some models require Responses API or Messages API. Do not silently change protocols after an error. Verify the exact model ID and supported route in the documentation, then make a short test request.
import json, os, urllib.request
# Choose a model that supports Chat Completions.
payload = {
"model": os.environ["CLODEX_MODEL"],
"messages": [{"role": "user", "content": "Hello"}],
"stream": False,
}
request = urllib.request.Request(
"https://clodex.xyz/v1/chat/completions",
data=json.dumps(payload).encode(),
headers={
"Authorization": "Bearer " + os.environ["CLODEX_API_KEY"],
"Content-Type": "application/json",
},
)
with urllib.request.urlopen(request, timeout=30) as response:
print(json.load(response))
Conversation history and user context
Build the messages list for the current conversation. Do not mix conversations from different users or assume every request is automatically connected to its predecessor. Context storage and transmission must be configured explicitly for the selected API.
Bound the history size, remove unnecessary personal information and account for the cost of repeated context. Keep application instructions separate from user messages. Uploaded files and model output must not grant administrative permissions.
Streaming and incomplete responses
Streaming displays content incrementally, but the interface must distinguish partial text from a completed response. User cancellation, a network disconnect or a timeout does not establish successful protocol completion. Keep a safe request identifier for troubleshooting.
Retry only after classifying the failure. If the bot has already sent a message or called an external tool, a retry can duplicate the action. Validate and confirm payment, administrative and other irreversible operations on your server.
Chatbot API costs and limits
Usage costs depend on the selected model, input context, generated output and billing rules. A lengthy support history can cost more than a short text-generation request. Compare billing units and cache terms rather than relying on a cheap-API label.
In addition to key quotas, apply per-user limits, attachment-size checks and concurrency controls. Measure typical support requests before a broad rollout. This guide does not promise a fixed price per conversation or unlimited free access.
Frequently asked questions
Can I use the API for a Telegram bot?
Yes. Your bot handler calls a supported API through a backend. Keep the model key and Telegram bot token on the server.
Should I send the entire chat history?
Send only the necessary context for the current conversation within the model’s limits and your data policy. Never combine messages from different users.
Does one API route work for all models?
No. Check whether the selected model requires Chat Completions, Responses or Messages API. Interface compatibility does not imply identical parameters.