Skip to main content
Plugins are a powerful way to extend your grammY bot’s functionality. They allow you to add features without cluttering your main bot logic, making your code more modular and maintainable.

What are Plugins?

In grammY, a plugin is simply middleware that provides a specific feature or functionality. Since grammY’s middleware system is extremely flexible, plugins can:
  • Add properties to the context object
  • Process updates before they reach your handlers
  • Provide helper methods and utilities
  • Manage state and session data
  • Integrate with external services

The Middleware Foundation

Every plugin in grammY is built on top of the middleware system. Understanding middleware is key to understanding plugins:

Plugin Types

grammY plugins come in two main categories:

Built-in Plugins

These are utilities that ship with grammY core:
  • Session - Persistent data storage per chat/user
  • Keyboard Builders - Simplified keyboard creation (Keyboard, InlineKeyboard)
  • Webhook Callback - Easy webhook integration
  • Inline Query Builder - Helper for inline query results
See the Built-in Plugins page for detailed documentation.

Official Plugins

These are maintained by the grammY team but distributed separately:
  • Router - Route updates to different handlers
  • Menu - Interactive menus with inline keyboards
  • Conversations - Stateful, step-by-step user interactions
  • I18n - Internationalization and localization
  • Hydrate - Easy message and callback query editing
  • Files - Simplified file handling
  • Transformer - Modify API requests before sending
  • Ratelimiter - Prevent spam and abuse
  • Stateless Question - Ask questions without sessions
  • Autoquote - Automatically quote replied messages
  • Parse Mode - Set default parse mode
  • Emoji - Type-safe emoji support

Using Plugins

Most plugins are installed using the bot.use() method:

Plugin Installation Pattern

Typical plugin usage follows this pattern:

Context Flavors

Many plugins extend the context object with additional properties and methods using context flavors:

Plugin Composition

Plugins can be composed together using the Composer class:

Plugin Execution Order

Plugins execute in the order they’re installed:

Conditional Plugin Installation

You can install plugins conditionally:

Error Handling in Plugins

Plugins can throw errors that bubble up to your error handler:

Plugin Best Practices

  1. Install plugins early - Install plugins like session before your handlers
  2. Use type-safe flavors - Leverage TypeScript’s context flavors for type safety
  3. Keep plugins focused - Each plugin should do one thing well
  4. Document dependencies - Note which plugins depend on others
  5. Handle errors gracefully - Don’t let plugin errors crash your bot
  6. Test plugin interactions - Ensure plugins work well together

Finding Plugins

Official plugins are available in the @grammyjs namespace on npm:
You can also find community plugins by searching for “grammy” or “grammy-plugin” on npm.

Next Steps