Developers » Callback API
Callback API
1. Connecting Callback API
    1.1. Secret key
    1.2. SSL Certificate
    1.3. Configuration via API
2. Data Format
    2.1. Event Types
3. Example Usage
4. SDK Compatibility

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).

Your server should return the "ok" string as a response to each request.


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. You can verify up to 10 servers with different sets of events.

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.


You also can manage your callback servers using groups section methods.

1.1. Secret key
In the «Secret key» field you can specify a random string that will be transmitted in the request to your server in the secret field.

1.2. SSL Certificate
To ensure the data transfer security, we recommend you to upload the SSL Certificate in the Callback API settings of your community.

Detailed information about the certificate is available on this page.

1.3. Configuration via API
You also can manage Callback API settings of your community using API methods:
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"}}


2.1. Event Types
Objects structure in the object field depends on notification type. All event types and corresponding objects supported by Callback API are described on this page.
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.vk.com/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.vk.com/method/messages.send?'. $get_params);

//Your server should return the "ok" string as a response to each request.
echo('ok');

break;

}
?>
4. SDK Compatibility
You can work with Callback API using our SDK: