Keyboard
The Keyboard API allows you to create interactive keyboards with various button types for VK messages. Use Keyboard for static button definitions and KeyboardBuilder for dynamic keyboard construction.
Keyboard Class
Static methods for creating keyboards and buttons.
Static Methods
builder()
Returns a new KeyboardBuilder instance for constructing keyboards.
const keyboard = Keyboard . builder ()
. textButton ({ label: 'Start' })
. row ()
. textButton ({ label: 'Help' });
Returns: KeyboardBuilder
keyboard(rows)
Assembles a keyboard from an array of button rows.
rows
(IKeyboardProxyButton | IKeyboardProxyButton[])[]
required
Array of button rows. Each row can be a single button or an array of buttons.
const keyboard = Keyboard . keyboard ([
Keyboard . textButton ({ label: 'Option 1' }),
[ Keyboard . textButton ({ label: 'A' }), Keyboard . textButton ({ label: 'B' })],
]);
Returns: KeyboardBuilder
textButton(options)
Creates a text button that can be colored.
options
IKeyboardTextButtonOptions
required
Configuration for the text button
Show IKeyboardTextButtonOptions
Button label (max 40 characters)
color
ButtonColor | ButtonColorUnion
Button color (default: SECONDARY_COLOR)
Custom data payload (max 255 characters when stringified)
const button = Keyboard . textButton ({
label: 'Buy Coffee' ,
payload: { command: 'buy' , item: 'coffee' },
color: Keyboard . POSITIVE_COLOR ,
});
Creates a URL button that opens a link.
options
IKeyboardURLButtonOptions
required
Configuration for the URL button
Show IKeyboardURLButtonOptions
Button label (max 40 characters)
The URL to open when clicked
Custom data payload (max 255 characters when stringified)
const button = Keyboard . urlButton ({
label: 'Visit Site' ,
url: 'https://example.com' ,
});
Creates a button that requests user location. Occupies the entire keyboard width.
options
IKeyboardLocationRequestButtonOptions
required
Configuration for the location button
Show IKeyboardLocationRequestButtonOptions
Custom data payload (max 255 characters when stringified)
const button = Keyboard . locationRequestButton ({
payload: { command: 'order_delivery' },
});
Creates a VK Pay button. Occupies the entire keyboard width.
options
IKeyboardVKPayButtonOptions
required
Configuration for the VK Pay button
Show IKeyboardVKPayButtonOptions
hash
KeyboardVKPayHash | string
required
Payment parameters or hash string
const button = Keyboard . payButton ({
payload: {},
hash: {
action: 'transfer-to-group' ,
group_id: 1 ,
aid: 10 ,
},
});
Creates a VK Apps button. Occupies the entire keyboard width.
options
IKeyboardApplicationButtonOptions
required
Configuration for the VK Apps button
Show IKeyboardApplicationButtonOptions
Button label (max 40 characters)
VK Apps application identifier
Community ID where the app is installed
Navigation hash for the application
const button = Keyboard . applicationButton ({
label: 'Open App' ,
appId: 6232540 ,
ownerId: - 157525928 ,
});
Creates a callback button that triggers events without sending a message.
options
IKeyboardCallbackButtonOptions
required
Configuration for the callback button
Show IKeyboardCallbackButtonOptions
Button label (max 40 characters)
color
ButtonColor | ButtonColorUnion
Button color (default: SECONDARY_COLOR)
Custom data payload (max 255 characters when stringified)
const button = Keyboard . callbackButton ({
label: 'Like' ,
payload: { action: 'like' , id: 123 },
color: Keyboard . PRIMARY_COLOR ,
});
Button color constants for text and callback buttons.
White button (#FFFFFF) - indicates secondary action
Blue button (#5181B8) - indicates main action
Red button (#E64646) - indicates dangerous/negative action
Green button (#4BB34B) - indicates agreement/confirmation
KeyboardBuilder Class
Builder class for constructing keyboards with a fluent API.
Properties
Whether the keyboard closes after pressing a button (default: false)
Whether the keyboard is attached inline to the message (default: false)
Methods
textButton(options)
Adds a text button to the current row.
options
IKeyboardTextButtonOptions
required
Button configuration (same as Keyboard.textButton)
builder . textButton ({
label: 'Buy Coffee' ,
payload: { command: 'buy' , item: 'coffee' },
color: Keyboard . POSITIVE_COLOR ,
});
Returns: this (for chaining)
Adds a URL button. May create a new row if current row has 2+ buttons.
options
IKeyboardURLButtonOptions
required
Button configuration
Returns: this (for chaining)
Adds a location request button. May create a new row if current row has 2+ buttons.
options
IKeyboardLocationRequestButtonOptions
required
Button configuration
Returns: this (for chaining)
Adds a VK Pay button. May create a new row if current row has 2+ buttons.
options
IKeyboardVKPayButtonOptions
required
Button configuration
Returns: this (for chaining)
Adds a VK Apps button. May create a new row if current row has 2+ buttons.
options
IKeyboardApplicationButtonOptions
required
Button configuration
Returns: this (for chaining)
Adds a callback button to the current row.
options
IKeyboardCallbackButtonOptions
required
Button configuration
Returns: this (for chaining)
row()
Saves the current row and starts a new row.
builder
. textButton ({ label: 'Button 1' })
. textButton ({ label: 'Button 2' })
. row ()
. textButton ({ label: 'Button 3' });
Returns: this (for chaining)
Note: Maximum 5 buttons per row.
oneTime(enabled)
Sets whether the keyboard closes after a button press.
builder . oneTime (); // Enable
builder . oneTime ( false ); // Disable
Returns: this (for chaining)
inline(enabled)
Sets whether the keyboard is inline.
builder . inline (); // Enable
builder . inline ( false ); // Disable
Returns: this (for chaining)
Note: Inline keyboards have a maximum of 6 rows, regular keyboards have 10.
clone()
Clones the builder with all settings and buttons.
const clone = builder . clone ();
Returns: KeyboardBuilder
toString()
Returns the JSON string representation for the VK API.
const keyboardJson = builder . toString ();
Returns: string
Usage Examples
Simple Keyboard
const keyboard = Keyboard . builder ()
. textButton ({ label: 'Start' , color: Keyboard . POSITIVE_COLOR })
. row ()
. textButton ({ label: 'Help' })
. toString ();
await vk . api . messages . send ({
peer_id: userId ,
message: 'Welcome!' ,
keyboard ,
});
Multi-Row Keyboard with Callbacks
const keyboard = Keyboard . builder ()
. callbackButton ({
label: 'Like' ,
payload: { action: 'like' },
color: Keyboard . PRIMARY_COLOR ,
})
. callbackButton ({
label: 'Dislike' ,
payload: { action: 'dislike' },
color: Keyboard . NEGATIVE_COLOR ,
})
. row ()
. urlButton ({ label: 'Website' , url: 'https://example.com' })
. inline ()
. toString ();
One-Time Keyboard
const keyboard = Keyboard . builder ()
. textButton ({ label: 'Yes' , color: Keyboard . POSITIVE_COLOR })
. textButton ({ label: 'No' , color: Keyboard . NEGATIVE_COLOR })
. oneTime ()
. toString ();
Using Static keyboard() Method
const keyboard = Keyboard . keyboard ([
Keyboard . textButton ({ label: 'Option 1' }),
[
Keyboard . textButton ({ label: 'A' }),
Keyboard . textButton ({ label: 'B' }),
Keyboard . textButton ({ label: 'C' }),
],
Keyboard . urlButton ({ label: 'More Info' , url: 'https://example.com' }),
]). toString ();
Type Definitions
enum ButtonColor {
SECONDARY = 'secondary' ,
PRIMARY = 'primary' ,
NEGATIVE = 'negative' ,
POSITIVE = 'positive' ,
}
type ButtonPayload = object | string ;
Payload must not exceed 255 characters when JSON stringified.
VK Pay Hash Types
Group ID to receive payment
Payment amount in rubles (minimum 1)
Show IVKPayTransferToGroup
action
'transfer-to-group'
required
Group ID to receive transfer
Constraints
Maximum label length: 40 characters
Maximum payload size: 255 characters (JSON stringified)
Maximum buttons per row: 5
Maximum rows for inline keyboards: 6
Maximum rows for regular keyboards: 10