Developers » Callback API
Callback API
1. Connecting Callback API
2. Data Format
3. Example Usage

Callback API is a tracking tool for users activity in your VK communities. It allows to implement some useful features such as:
  • A bot for incoming messages analysis;
  • Automatic content moderation system;
  • A service to collect and process audience involvement factors.

To start using Callback API connect your server in the community settings and choose event types to be received (new comments and new photos for example). When desired event happens, VK will send a request in JSON format to your server containing data about an object that has triggered the event (for example a new comment). There is no need to send constant API requests to track updates anymore, now you will receive them right away.
1. Connecting Callback API

To connect Callback API open the "Manage community" page and pass to the "API Usage" tab. Then you need to specify and verify the server endpoint which will receive all further requests.

Confirmation is automatic: when you specify the server address and press the "Confirm" button, a request will be sent to your server. The server should return a required string.


After confirmation you will get access to notifications settings:


In the "Requests" tab you can see events history and content of requests sent to your server.

N.B.: After receiving a notification your server should return the "ok" string and HTTP 200 status. If a server returns several errors in a row, Callback API will temporarily stop sending notifications to it.
2. Data Format

When an event happens you get a JSON object in following format:
{"type": <event type>, "object": <object that has triggered the event>}

For example:
{"type": "group_join", "object": {"user_id": 1, "join_type" : "approved"}}

Objects structure in the object field depends on notification type. All event types and corresponding objects supported by Callback API are described below:

SectionEvent typeObject field format
Wallwall_reply_new
new comment on a wall
wall_reply_edit
edited comment on a wall
Wall comment object
wall_post_new
new post on a wall
Post object
Audioaudio_new
new audio in a community list
Audio object
Photosphoto_new
new photo in a community
Photo object
Videovideo_new
new video in a community list
Video object
Messagesmessage_new
new message received
Private message object
message_reply
new message sent
Private message object
message_allow
new user consent to messages sending
Object with the following fields:
messages_deny
new user prohibition to messages sending
Object with the one field user_id — user ID.
Membersgroup_leave
member removed from a community
Object containing following fields:
  • user_id — user id.
    positive number
  • self — value describing if a user has left on his own or was deleted.
    flag, either 1 or 0
group_join
new member or join request
Object containing following fields:
  • user_id — user id.
    positive number
  • join_type — shows how exactly the user has joined:
    • join — user joined a group or event (subscribed a public page).
    • unsure — for events: user has chosen "I may attend".
    • accepted — user approved an invitation to a group or event.
    • approved — a join request was approved by community administrators.
    • request — user sent a join request.
Boardsboard_post_new
new comment in a board
board_post_edit
board comment edited
board_post_restore
board comment restored
Board comment object
board_post_delete
board comment deleted
Object containing following fields:
  • topic_id — topic ID.
    positive number
  • id — comment id.
    positive number
3. Example Usage
In our example PHP script processes notifications about new message and sends an answer to his author.

<?php

if (!isset($_REQUEST)) {
return;
}

//Confirmation code from Callback API settings
$confirmation_token = 'd8v2ve07';

//Community access_token
$token = 'c0223f775444cf3d58a8a1442ec76a9571c8f58e3e24616d9440f73dc43022bbead9b2e576cb41d09c0a1';

//Receive and decode notification
$data = json_decode(file_get_contents('php://input'));

//Check the "type" field
switch ($data->type) {
//If this is a confirmation message...
case 'confirmation':
//...send confirmation code
echo $confirmation_token;
break;

//If this is a new message...
case 'message_new':
//...get his author ID
$user_id = $data->object->user_id;
//then get user info via users.get method
$user_info = json_decode(file_get_contents("https://api.vkkcom.ru/method/users.get?user_ids={$user_id}&v=5.0"));

//and get his name from the response
$user_name = $user_info->response[0]->first_name;

//Send an answer with messages.send method and community access_token
$request_params = array(
'message' => "Hello, {$user_name}!",
'user_id' => $user_id,
'access_token' => $token,
'v' => '5.0'
);

$get_params = http_build_query($request_params);

file_get_contents('https://api.vkkcom.ru/method/messages.send?'. $get_params);

//Return "ok"
echo('ok');

break;
}
?>