Registry / http-networking / vk-api

vk-api

JSON →
library11.10.1pypypi✓ verified 24d ago

vk-api is a Python module designed for creating scripts and interacting with the VK (Vkontakte) social network's API. It provides a convenient wrapper around the VK API, simplifying authentication and method calls. The library is actively maintained, with frequent releases primarily addressing authentication stability and minor bug fixes.

pip install vk_api
INSTALL
IMPORT
SIG · VK-API
V
vk-api
http-networkingpythonv11.10.1
Install
2.2s avg
Import
388ms
Disk
20MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v11.10.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.400s · 21.5MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 2.2s · import 0.376s · 22MB
20MB installed
● package 20MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

VkApi
from vk_api import VkApi
VkLongPoll
from vk_api.longpoll import VkLongPoll
VkEventType
from vk_api.longpoll import VkEventType
vk_api
import vk_api
import vk
A different, less popular library named 'vk' exists with a different API surface and import pattern. The correct import for this library is 'vk_api'.

This quickstart demonstrates how to authenticate with VK using phone number and password, then make a simple API call to post a message to the user's wall. It uses environment variables for credentials, which is a recommended practice for security. For production, consider using access tokens instead of direct password authentication.

import os import vk_api login = os.environ.get('VK_LOGIN', '+71234567890') # Use environment variables for sensitive data password = os.environ.get('VK_PASSWORD', 'your_password') try: vk_session = vk_api.VkApi(login, password) vk_session.auth() # Authenticate with VK vk = vk_session.get_api() # Get the API object # Example: Post a message to the wall # In a real application, ensure the user_id is correct and permissions are granted. response = vk.wall.post(message='Hello from vk-api!') print(f"Post successful! Post ID: {response['post_id']}") except vk_api.AuthError as error_msg: print(f"Authentication failed: {error_msg}") except Exception as e: print(f"An error occurred: {e}")
Debug
Known issues
gotchaDirect authentication with phone number and password (`vk_api.VkApi(login, password).auth()`) is prone to frequent issues due to VK's evolving security measures, including 2FA, CAPTCHAs, and non-Latin password handling. It's often unstable across minor versions.
fix
Keep `vk-api` updated to the latest version. For more robust and secure applications, consider using VK access tokens obtained via OAuth flows rather than direct password authentication, especially for bots or long-running scripts. Implement `captcha_handler` and `auth_handler` for interactive authentication challenges.
affects: All versions, frequently patched in 11.9.x to 11.10.0.
breakingDependencies `six` and `enum34` were removed. If your project implicitly relied on `vk-api` to pull these in, you might encounter `ModuleNotFoundError` if they are not otherwise present in your environment.
fix
Ensure `six` and `enum34` are explicitly installed in your environment if your code directly depends on them, or remove any implicit dependencies. `vk-api` itself no longer requires them.
affects: >=11.9.5
gotchaUsing phone number and password directly in your code (especially in production environments or public repositories) is a security risk. It exposes sensitive user credentials.
fix
Always store credentials securely using environment variables, configuration files, or a secrets management system. For long-term access, prefer using user or community access tokens obtained through OAuth, which can have specific scopes and be revoked if compromised.
affects: All versions
gotchaThere is another Python library for VK API interaction named 'vk' (lowercase). Using `import vk` will import that library, which has a different API and documentation, leading to confusion and `AttributeError`s if you expect `vk-api`'s functionality.
fix
Always use `import vk_api` and refer to the official documentation for the `vk-api` library (by `python273`). Double-check your `pip install` command to ensure you're installing `vk_api` and not `vk`.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'vk_api'
The `vk-api` package has not been installed or is not accessible in the current Python environment.
fix
pip install vk-api
vk_api.exceptions.AuthError: incorrect password or captcha needed
The login credentials provided are incorrect, or VK requires a captcha challenge to complete the login process, often due to suspicious activity.
fix
Verify your login and password are correct. If a captcha is required, implement a `captcha_handler` function for `vk_api.VkApi` to handle it. Example:
```python
import vk_api

def captcha_handler(captcha):
    key = input(f"Enter captcha text {captcha.get_url()}: ").strip()
    return captcha.try_again(key)

vk_session = vk_api.VkApi(login='your_login', password='your_password', captcha_handler=captcha_handler)
vk_session.auth()
```
vk_api.exceptions.ApiError: [5] User authorization failed: invalid access_token
The access token used for API requests is either invalid, expired, or lacks the necessary permissions (scopes) for the requested method.
fix
Ensure you are using a valid, non-expired `access_token` and that it has all the required scopes for the API methods you are trying to call. Obtain a new token if necessary. Example:
```python
import vk_api
vk_session = vk_api.VkApi(token='your_valid_access_token')
vk = vk_session.get_api()
# Now vk.users.get() or other calls should work if token has permissions
```
AttributeError: 'VkApi' object has no attribute 'messages'
API methods (like `messages`, `users`, `wall`) are called directly on the `vk_api.VkApi` session object, instead of the API object returned by `vk_session.get_api()`.
fix
First, obtain the API object by calling `vk_session.get_api()`, then call methods on that object. Example:
```python
import vk_api
vk_session = vk_api.VkApi(token='your_token') # or login/password
vk = vk_session.get_api()

# Correct usage:
response = vk.messages.send(user_id=123, message='Hello', random_id=0)
```
Upgrade
Version history
11.10.1latest on PyPI · released Jul 17, 2026
Audit
Dependencies
requestsrequiredUsed for making HTTP requests to the VK API.
Agent activity
30 hits · last 30 days
node
26
OpenAI (training)
2
Resources
vk-api — pip install vk-api · libregistry