API Reference
Public Runtime API
This reference contains only endpoints that an external bot server can call with a kcb_live_… bot token. Base URL: https://chat.wokey.pro/_keychat.
Bot creation and editing, token issuance and rotation, webhook configuration, user installation, callback and inline proxies, and administrative operations are performed by the Key Chat client itself. They are intentionally excluded from the public bot-developer API.
One bot token cannot create or modify other bots, read arbitrary chats, or manage users. Never place a bot token in PWA, browser, or mobile code.
Send or update a message
/v1/bot/messagesAuthentication: current bot token in Authorization: Bearer kcb_live_…
Posts as the authenticated bot only into a direct or group chat assigned to it. Obtain chat_id from a platform webhook event.
content supports plain text, Markdown, attachments, message buttons, a dynamic menu, and a commands autocomplete list. reply_to_event_id creates a reply, while replace_event_id updates an earlier message from the same bot.
Provide a stable idempotency_key for safe retries. Reusing the key returns the original result without creating a duplicate message.
Request body
{
"chat_id": "<chat_uuid>",
"content": {
"text": "Готово",
"format": "markdown",
"attachments": [],
"commands": [
{
"command": "/start",
"description": "Start again",
"icon": "▶️"
},
{
"command": "/invite",
"arguments": "<email>",
"description": "Invite a user"
}
],
"keyboard": {
"rows": [
[
{
"id": "details",
"text": "Подробнее",
"action": {
"type": "callback",
"data": "job-42"
}
}
]
]
}
},
"idempotency_key": "job-42-result",
"reply_to_event_id": null,
"replace_event_id": null
}Successful response
{
"event_id": "$event_id",
"chat_id": "<chat_uuid>",
"duplicate": false
}Possible errors
- 401 — invalid or revoked bot token
- 404 — chat is not available to this bot
- 409 — idempotency_key is already being processed
- 422 — invalid message content
- 503 — messaging transport is temporarily unavailable
Usage examples
import { KeyChatBot, botCommand, callbackButton } from '@keychat/bots';
const bot = new KeyChatBot(process.env.KEYCHAT_BOT_TOKEN!);
const result = await bot.sendMessage({
chat_id: chatId,
content: {
text: '**Ready**',
format: 'markdown',
keyboard: { rows: [[callbackButton('details', 'Details', 'job-42')]] },
commands: [botCommand('/invite', { arguments: '<email>', description: 'Invite a user' })]
},
idempotency_key: crypto.randomUUID()
});Read a message by ID
/v1/bot/messages/{event_id}?chat_id={chat_uuid}Authentication: current bot token in Authorization: Bearer kcb_live_…
Returns one message, for example the source event referenced by m.in_reply_to. URL-encode event_id; chat_id is required and comes from the webhook. This endpoint never lists messages and is not a history API.
In a direct bot chat, any event in that chat may be read. In a group, only the bot’s own messages and messages previously delivered to that bot as a mention or reply are available. Every other case returns 404 without revealing why.
message uses the same decrypted and normalized shape as message.created. Edited messages return their latest version and deleted messages return 410. Attachment download_url keeps its original seven-day grant; reading a message does not renew or recreate access to old media.
Successful response
{
"chat_id": "<chat_uuid>",
"from_bot": false,
"message": {
"id": "$event_id",
"type": "m.room.message",
"text": "Photo",
"caption": "Photo",
"content": {
"msgtype": "m.image",
"body": "photo.jpg"
},
"attachments": [
{
"server": "wokey.pro",
"media_id": "media_id",
"name": "photo.jpg",
"mime_type": "image/jpeg",
"download_url": "https://chat.wokey.pro/_keychat/v1/bot/media/download/wokey.pro/media_id"
}
]
}
}Possible errors
- 401 — invalid or revoked token
- 404 — message is missing, chat does not belong to the bot, or the group event is out of scope
- 410 — message was deleted
- 422 — chat_id is missing or invalid
- 502 — Matrix is temporarily unavailable
Usage examples
const replied = await bot.getMessage(replyEventId, chatId);
console.log(replied.message.text);
if (replied.message.attachments[0]) {
const attachment = replied.message.attachments[0];
const file = await bot.downloadMedia(attachment.server, attachment.media_id);
}Upload media
/v1/bot/mediaAuthentication: current bot token in Authorization: Bearer kcb_live_…
Uploads an image, video, or file before sending a message. Submit multipart/form-data with one file field; one upload is limited to 200 MB.
The response is a media descriptor. Copy it unchanged into content.attachments and add type: image, video, or file. The URL belongs to Key Chat media storage and does not expose the bot token.
Do not substitute arbitrary remote URLs for an upload: the platform must control attachment availability, safety, and lifetime.
Successful response
{
"url": "mxc://wokey.pro/media_id",
"name": "photo.jpg",
"mime_type": "image/jpeg",
"size": 482013
}Possible errors
- 400 — file field is missing
- 401 — invalid or revoked bot token
- 413 — file exceeds 200 MB
- 502 — media storage is temporarily unavailable
Usage examples
import { readFile } from 'node:fs/promises';
const bytes = await readFile('photo.jpg');
const media = await bot.uploadMedia(new Blob([bytes], { type: 'image/jpeg' }), 'photo.jpg');
await bot.sendMessage({
chat_id: chatId,
content: { attachments: [{ type: 'image', ...media }] }
});Download an incoming attachment
/v1/bot/media/download/{server}/{mediaId}Authentication: current bot token in Authorization: Bearer kcb_live_…
Downloads an image, video, or file from a message delivered to this bot. The response contains the original file bytes; for encrypted chats Key Chat verifies the checksum and decrypts the content server-side.
Every message.created webhook always includes a normalized message.attachments array, even when the original Matrix message used content.file or content.url. Each item contains server, media_id, download_url, mime_type, name, and the original MXC URL. An image caption is exposed separately as message.caption. Pass server and media_id to the SDK method, or GET download_url with the same Bearer token.
Access is scoped to this bot and only to attachments actually delivered to it. The grant lasts 7 days, stops working after the bot is removed from the chat, and cannot read arbitrary Matrix media. The maximum response size is 200 MB.
Successful response
200 — binary file content with the correct Content-TypePossible errors
- 401 — invalid or revoked token
- 404 — media was not delivered to this bot, chat was removed, or file is missing
- 410 — the 7-day grant expired
- 413 — file exceeds 200 MB
- 502 — Matrix media is temporarily unavailable
Usage examples
const attachment = event.data.message.attachments[0];
const file = await bot.downloadMedia(attachment.server, attachment.media_id);
await processImage(file.data, file.contentType);