Api class provides full access to the Telegram Bot API. All methods are available with convenient parameter handling and optional AbortSignal support for canceling requests.
Overview
The API client is available through:bot.api- On the Bot instancectx.api- On the Context object (preferred inside middleware)
- Type-safe access to all Telegram Bot API methods
- Automatic error handling with
GrammyErrorandHttpError - Request transformation via the transformer system
- Webhook reply optimization
Constructor
string
required
Bot API token obtained from @BotFather
ApiClientOptions
Optional API client configuration
WebhookReplyEnvelope
Optional webhook reply envelope for optimized webhook responses
Properties
RawApi
Provides raw access to all Telegram Bot API methods with 1:1 method signatures as documented on the official API reference.
object
Configuration namespace for advanced API operations.
Common Methods
All methods take an optionalAbortSignal as the last parameter to cancel requests.
getMe
Returns basic information about the bot.sendMessage
Sends a text message.number | string
required
Unique identifier for the target chat or username of the target channel (in the format @channelusername)
string
required
Text of the message to send (1-4096 characters after entities parsing)
object
Optional parameters:
parse_mode: ‘Markdown’, ‘MarkdownV2’, or ‘HTML’entities: List of special entitiesreply_markup: Inline keyboard, custom keyboard, etc.link_preview_options: Link preview settings- And more…
sendPhoto
Sends a photo.InputFile | string
required
Photo to send. Pass:
file_idas string (recommended for existing Telegram files)- HTTP URL as string
InputFilefor uploading new files
sendDocument
Sends a document file.sendAudio
Sends an audio file.sendVideo
Sends a video file.sendAnimation
Sends an animation (GIF or H.264/MPEG-4 AVC video without sound).sendVoice
Sends a voice message (OGG encoded with OPUS).sendVideoNote
Sends a video note (round video message).sendMediaGroup
Sends a group of photos, videos, documents or audios as an album.sendLocation
Sends a location point on the map.sendVenue
Sends information about a venue.sendContact
Sends a phone contact.sendPoll
Sends a native poll.sendDice
Sends an animated emoji with a random value.Message Management
editMessageText
Edits text and game messages.editMessageReplyMarkup
Edits only the reply markup of messages.deleteMessage
Deletes a message.deleteMessages
Deletes multiple messages simultaneously.forwardMessage
Forwards a message.copyMessage
Copies a message (without the forward header).Chat Management
getChat
Gets up-to-date information about the chat.getChatAdministrators
Gets a list of administrators in a chat.getChatMemberCount
Gets the number of members in a chat.getChatMember
Gets information about a member of a chat.banChatMember
Bans a user in a group, supergroup or channel.unbanChatMember
Unbans a previously banned user.restrictChatMember
Restricts a user in a supergroup.promoteChatMember
Promotes or demotes a user in a supergroup or channel.leaveChat
Leaves a group, supergroup or channel.Callback Queries
answerCallbackQuery
Answers a callback query from an inline button.Inline Queries
answerInlineQuery
Answers an inline query.Bot Information
setMyCommands
Sets the list of the bot’s commands.getMyCommands
Gets the current list of the bot’s commands.setMyName
Changes the bot’s name.setMyDescription
Changes the bot’s description.Webhooks
setWebhook
Sets a webhook URL to receive updates.deleteWebhook
Removes webhook integration.getWebhookInfo
Gets current webhook status.Updates
getUpdates
Receives incoming updates using long polling.bot.start(). It’s used internally by grammY.
Transformers
Transformers allow you to modify API calls before they are sent to Telegram.- Rate limiting
- Logging
- Default parameters
- Request retrying
- Caching
Error Handling
The API client throws two types of errors:GrammyError
Thrown when the Telegram API returns an error response.HttpError
Thrown when the HTTP request fails (network error, timeout, etc.).Complete Example
See Also
- Bot - The main Bot class
- Context - Context shortcuts for API calls
- Telegram Bot API - Official API reference
- Transformers - Advanced transformer usage