vk-api Python Wrapper

11.10.0 · active · verified Sat Apr 11

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.

Warnings

Install

Imports

Quickstart

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}")

view raw JSON →