# vksdk

> SDK for API of vk.com

Latest version **5.3.2** (published 2016-05-22) · BSD-3-Clause license · 0 weekly downloads

## Install

```sh
npm install vksdk
pnpm add vksdk
yarn add vksdk
bun add vksdk
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 5.3.2 |
| Published | 2016-05-22 |
| First published | 2014-06-04 |
| Weekly downloads | 0 |
| License | BSD-3-Clause |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 200 |
| Author | Andrey Korchak |
| Maintainers | 57uff3r |
| Keywords | vk, vkontakte, sdk |

## Links

- npm: https://www.npmjs.com/package/vksdk
- Repository: https://github.com/57uff3r/nodejs-vksdk
- Issues: https://github.com/57uff3r/nodejs-vksdk/issues
- npm.io page: https://npm.io/package/vksdk

## Recent versions

- 5.3.2 (latest) — 2016-05-22
- 5.3.1 — 2016-01-25
- 5.2.1 — 2015-12-13
- 5.2.0 — 2015-11-01
- 5.1.5 — 2015-10-12
- 5.1.0 — 2015-03-24
- 5.0.3 — 2014-12-24
- 5.0.2 — 2014-12-24
- 5.0.1 — 2014-12-24
- 5.0.0 — 2014-12-23
- 4.7.0 — 2014-12-08
- 4.6.0 — 2014-10-31
- 4.5.0 — 2014-09-08
- 4.0.0 — 2014-06-04

## README

nodejs-vksdk
============
Small SDK for vk.com API.

# Installation
    npm install vksdk

# Example
```js
// Setup
var VK = require('vksdk');
var vk = new VK({
   'appId'     : 2807970,
   'appSecret' : 'L14ZKpgQPalJdumI6vFK',
   'language'  : 'ru'
});

/**
 * Request server methods
 */

// Setup server access token for server API methods
vk.on('serverTokenReady', function(_o) {
    // Here will be server access token
    vk.setToken(_o.access_token);
});

// Turn on requests with access tokens
vk.setSecureRequests(true);

// Request server API method
vk.request('secure.getSMSHistory', {}, function(_dd) {
    console.log(_dd);
});

/**
 * Request client methods
 */
// First you have to pass access_token from client side JS code
vk.setToken(access_token);

// Request 'users.get' method
vk.request('users.get', {'user_id' : 1}, function(_o) {
    console.log(_o);
});
```

# Setup
```js
var VK = require('vksdk');

var vk = new VK({
    'appId'     : [Your application ID here],
    'appSecret' : [Your application secret code here],
});
```

Required config options:
* appSecret — application secret code (check your application settings on vk.com)
* appId — vk.com application id

Available config options:

* **[bool] https** — with this options all links in API answers will be with https protocol. Disabled by default.
* **[string] version** — vk.com api verions. Default: 5.27
* **[string] language** — Language code for api answers (for old deprecated API)
* **[bool] secure** — enable api requests with tokens. Default false.


You can  read and change some config options:
* **getVersion()** — get current API version
* **setVersion(_v)** — set current API version
* **getLanguage()** — get current API language
* **setLanguage(_v)** — set current API language
* **getHttps()** — get https links usage for API answers
* **setHttps(_v)** — set https links usage for API answers
* **getSecureRequests()** — get token's usage for API requests
* **setSecureRequests(_v)** — set token's usage for API requests


# API requests
For vk.com API requests you have to use method *request(_method, _requestParams, _response)*.

* **[string] _method** — name of vk.com API method,
* **[mixed] _requestParams** - object with values of params for api method. This param is not required. You also can pass empty object *{}*
* **[mixed] _response** — special response handler (not required), function or event name.

Request method gets data from API and returns result. There are 3 ways to get data from API

## Callback
```js
vk.setSecureRequests(false);
vk.request('users.get', {'user_id' : 1}, function(_o) {
console.log(_o);
});
```

## Event
After success API call SDK emits the event named 'done:' + _method;
So if you call method *users.get*, you have to wait event *done:users.get*

```js
vk.setSecureRequests(false);
vk.request('users.get', {'user_id' : 1});
vk.on('done:users.get', function(_o) {
    console.log(_o);
});
```

## Custom event
Result of request will be returned with your custom event

```js
vk.setSecureRequests(false);
vk.request('users.get', {'user_id' : 1}, 'myCustomEvent');
vk.on('myCustomEvent', function(_o) {
    console.log(_o);
});
```

# Server access token
For some api methods you need server access token

```js
vk.requestServerToken();

// Waiting for special 'serverTokenReady' event
vk.on('serverTokenReady', function(_o) {
    // Here will be server access token
    console.log(_o);
});
```

You also can get tokeb with callback or custom event

# HTTP errors
SDK emits 'http-error' event in case of http errors.

```js
vk.on('http-error', function(_e) {
    console.log(_e);
});
```

# JSON parsing errors
SDK emits 'parse-error' event in case of non-valid API answer.

```js
vk.on('parse-error', function(_e) {
    console.log(_e);
});
```

SDK provides all methods from [events.EventEmitter](http://nodejs.org/api/events.html)

# Bonus
You also can request some api methods without any tokens, with [app signature](https://vk.com/pages?oid=-17680044&p=Application_Interaction_with_API)

```js
vk.oldRequest('places.getCountryById', {'cids' : '1,2'}, function(_o) {
    console.log(_o);
});
```

But this way is deprecated and doesn't work for all api methods.


# For developers
Your commits and pull requests are welcome. Please run the tests before

    npm test

You have to provide tests for the new features.

# Support
* 57uff3r@gmail.com
* skype: andrey.korchak
* http://57uff3r.ru
* http://vk.com/s7uff3r

# Thanks to
* [Alex0007](https://github.com/Alex0007)

See  also vk.com [cities and counties DB](http://citieslist.ru/)

---
_Source: https://npm.io/package/vksdk · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
