Marx Bot API
The HTTP API a bot's program uses to read and send messages in the Marx messenger.
Address
Every method is under https://api.bot.marx.moscow/api/bot/v1/. HTTPS only. Request and response bodies are JSON in UTF-8 with Content-Type: application/json; a file upload is multipart/form-data. The list of methods: Methods.
Create a bot and get its token
Bots are created by the system bot @marxbot («Создатель ботов», the bot maker) in the Marx app. It answers in Russian.
- In the app's settings tap «Создать бота» (Create bot), or find
@marxbotand open a chat with it. - Send
/newbot. - Send the bot's name: 1 to 64 characters. It is shown in chats and in the profile.
- Send the username: 5 to 32 characters, Latin letters, digits and
_, ending inbot. For example,dacha_weather_bot. - The answer holds a token like
marx_pat_…. Tap it to copy.
Anyone with the token writes as the bot: keep it like a password. /token issues a new one; the old one stops working at once.
| @marxbot command | What it does |
|---|---|
/newbot | Create a bot |
/mybots | List your bots |
/token, /token @bot_username | A new token; without a name and with several bots it asks which one |
/setcommands | Set the bot's menu commands, one command - description per line |
/deletecommands | Remove the bot's commands |
/cancel | Cancel what was started |
One person may own up to 20 bots, create up to 5 bots a day and change a token up to 10 times an hour.
Add a bot to a group or a channel
Direct chat
A bot cannot start a direct chat. The chat appears when a person finds the bot by its username and writes to it; the app shows a «Начать» (Start) button that sends /start.
Group
A bot is added like any member: in the group's profile, «Добавить участников» (Add members) and find it by @username. Anyone allowed to add members may do it; the bot's owner is not needed. Group members may post, and so may the bot. What the bot gets from a group: What a bot sees.
Channel
Only admins post in a channel, so the bot must be an admin: in the channel's profile, «Добавить админа» (Add admin) and find it by @username, or «Сделать админом» (Make admin) if the bot is already in the channel. The channel's owner and admins with the right «Назначать админов» (Appoint admins) can do it. A bot gets no messages from a channel; it only publishes there.
Authorization
Every request carries the bot's token in a header:
Authorization: Bearer marx_pat_...
No header, a token of the wrong form, a revoked or unknown token: 401.
curl https://api.bot.marx.moscow/api/bot/v1/me \
-H "Authorization: Bearer marx_pat_not_a_real_token"
{"detail": "invalid api token"}
Errors
A 2xx status is success. Other statuses carry JSON with a detail field.
| Status | Body | When |
|---|---|---|
| 400, 401, 403, 404, 409 | {"detail": "text"} | The request is understood but cannot be done; the text is the reason, for the program and the log |
| 422 | {"detail": [ … ]} | The body has the wrong form: a missing field, a wrong type, a string too long. One item per field: type, loc (where), msg, input |
| 429 | {"detail", "code", "retry_after"} | A rate limit is exceeded |
| 503 | {"detail", "code", "retry_after"} | The server is busy; retry after retry_after seconds |
| 500, 502 | — | A server error; retry later |
curl https://api.bot.marx.moscow/api/bot/v1/messages \
-H "Authorization: Bearer $MARX_TOKEN" \
-H "Content-Type: application/json" \
-d '{"body": "no chat_id"}'
{"detail": [{"type": "missing", "loc": ["body", "chat_id"], "msg": "Field required", "input": {"body": "no chat_id"}}]}
429 and retry_after
A request over a limit gets 429, with the limit's name from the table below in code and the number of seconds until a retry may succeed in retry_after and in the Retry-After header. A 503 with code db_busy has the same form.
{"detail": "rate limit exceeded", "code": "bot.message.send", "retry_after": 17}
Wait retry_after seconds and send the request again. For POST /messages pass a client_id: a repeat with the same client_id does not create a second message.
Rate limits
Limits count per token, except creating a bot and changing a token, which count per person, the bots' owner. A window is a fixed stretch of time starting at the minute, hour or day by the server's clock, not at the first request: the count resets at its end.
| Action | code | Limit |
|---|---|---|
POST /messages | bot.message.send | 60 a minute |
POST /media, number of files | bot.media.upload | 120 an hour |
POST /media, volume | bot.media.bytes | 800 MB a day |
POST /reactions | bot.reaction.add | 120 a minute |
GET /updates | bot.updates | 120 a minute |
POST /webhook, DELETE /webhook | bot.webhook | 10 an hour |
PUT /commands, DELETE /commands | bot.commands | 30 per 10 minutes |
| Create a bot at @marxbot | bot.create | 5 a day |
| A new token at @marxbot | bot.token | 10 an hour |
Long polling with timeout=30 makes two requests a minute, far below the bot.updates limit.