The VK class is the primary entry point for the VK-IO library. It initializes and provides access to all core services including API methods, file uploads, and update handling.
Constructor
Creates a new VK instance with the specified configuration.
import { VK } from 'vk-io' ;
const vk = new VK ({
token: 'your_access_token' ,
apiVersion: '5.199'
});
Parameters
options
Partial<VKOptions> & { token: string }
required
Configuration options for the VK instance Show VKOptions Properties
VK API access token. You can obtain this from the VK API settings page.
language
'ru' | 'uk' | 'be' | 'en' | 'es' | 'fi' | 'de' | 'it'
The language for API responses. Defaults to English if not specified.
Custom HTTPS agent for network requests. Uses Node.js global agent by default.
apiMode
'sequential' | 'parallel' | 'parallel_selected'
default: "sequential"
Determines how API requests are collected:
sequential - Requests are sent one by one in order
parallel - All requests are batched through execute method
parallel_selected - Only specified methods (in apiExecuteMethods) are batched
apiRequestMode
'sequential' | 'burst'
default: "sequential"
Request sending strategy:
sequential - Requests sent through an interval queue
burst - Parallel requests (may cause network errors)
Time in milliseconds to wait before re-querying after an error.
Maximum number of requests per second. VK has rate limits that vary by token type.
apiBaseUrl
string
default: "https://api.vk.ru/method"
Base URL for API requests. Change this only if you’re using a proxy or custom endpoint.
Number of times to retry a failed request before throwing an error.
Request timeout in milliseconds. Requests exceeding this will be aborted.
Custom headers to include with every API request. Default includes User-Agent.
Maximum number of requests to batch in a single execute call (max 25 per VK limits).
apiExecuteMethods
string[]
default: ["messages.send"]
Methods to batch when apiMode is parallel_selected.
apiExecuteUnsupportedMethods
Methods that cannot be used with execute (mainly upload methods). Has sensible defaults.
Time to wait between polling requests when using Long Poll.
Number of retries for failed polling requests.
Group ID for polling. If not specified, will be auto-detected from token.
Secret key for validating webhook requests from VK.
Confirmation string required when setting up webhooks in VK.
Timeout in milliseconds for file upload operations.
Custom callback service for handling captcha and other interactive prompts.
Properties
Instance of the API class for making VK API method calls. Provides access to all VK API methods. See API Reference for detailed documentation.
Instance of the Upload class for uploading files to VK (photos, videos, documents, etc.). See Upload Reference for detailed documentation.
Instance of the Updates class for receiving and handling updates from VK (messages, wall posts, etc.). See Updates Reference for detailed documentation.
Service for handling callbacks like captcha validation. Can be shared across multiple VK instances.
Examples
Basic Initialization
import { VK } from 'vk-io' ;
const vk = new VK ({
token: process . env . VK_TOKEN
});
// Make API calls
const user = await vk . api . users . get ({ user_ids: [ 1 ] });
console . log ( user );
With Custom Configuration
const vk = new VK ({
token: process . env . VK_TOKEN ,
apiVersion: '5.199' ,
language: 'en' ,
apiLimit: 3 ,
apiMode: 'sequential' ,
apiRetryLimit: 5
});
const vk = new VK ({
token: process . env . VK_TOKEN ,
apiMode: 'parallel' ,
apiExecuteCount: 25
});
// Multiple requests will be automatically batched
await Promise . all ([
vk . api . users . get ({ user_ids: [ 1 ] }),
vk . api . users . get ({ user_ids: [ 2 ] }),
vk . api . users . get ({ user_ids: [ 3 ] })
]);
With Webhook Configuration
const vk = new VK ({
token: process . env . VK_TOKEN ,
webhookSecret: process . env . VK_WEBHOOK_SECRET ,
webhookConfirmation: process . env . VK_WEBHOOK_CONFIRMATION
});
// Start receiving updates via webhook
vk . updates . on ( 'message_new' , async ( context ) => {
await context . send ( 'Hello!' );
});
await vk . updates . startWebhook ({
port: 8080 ,
path: '/webhook'
});
Multi-Instance Setup
import { VK , CallbackService } from 'vk-io' ;
// Shared callback service for multiple instances
const callbackService = new CallbackService ();
const userVk = new VK ({
token: process . env . VK_USER_TOKEN ,
callbackService
});
const groupVk = new VK ({
token: process . env . VK_GROUP_TOKEN ,
callbackService
});
// Both instances share the same captcha handler
callbackService . onCaptcha ( async ({ src , send }) => {
const key = await solveCaptcha ( src );
await send ( key );
});
Best Practices
Token Security : Never hardcode tokens in your source code. Use environment variables or secure configuration management.
Rate Limits : Respect VK’s rate limits. User tokens typically allow 3 requests/second, while service tokens may allow more. Adjust apiLimit accordingly.
API Mode Selection :
Use sequential for simple bots and reliable request ordering
Use parallel for high-performance applications with many requests
Use parallel_selected to optimize specific methods while keeping others sequential