Runs your JavaScript-like script on the server to make multiple API calls and post-process their results.

You can use this, for example, to retrieve different data in one go to display a complex screen, like a user’s profile, or to make several API calls that depend on each other’s results. You can make up to 25 API calls in one execute.

The server will compile your code into an intermediate representation and cache that. Thus, it is more efficient to pass extra parameters and retrieve them using the Args.param_name syntax rather than dynamically generate the code itself.

The scripting language has the following features:

  • Arithmetic operations: + - * / %
  • + also works for string concatenation
  • Variables: var a
  • Conditions: if, else
  • Loops: while, for, break, continue
  • Object and array literals: {key: "value", 'another_key': 123}, ['a', 123, {}, someVariable]
  • API calls: API.method({parameters}), e.g. API.users.get({user_id: 1, fields: "photo_400"})
  • Object array field selection operator @.. For example, the script return API.friends.get({count: 100, fields: "first_name"}).items@.first_name; will return an array of the current user’s friends’ first names.
  • Functions: parseInt and parseDouble
  • Methods on arrays: push, pop, slice, splice, shift, unshift, indexOf
  • Methods on strings: substr, split, indexOf
  • The .length property on strings and arrays
  • The special Args object that contains any extra parameters you passed to this method
  • The delete operator to remove fields from objects, e.g. delete groups[i].description;
  • // and /* ... */ comments

Code examples

Retrieve a wall post and the first names of the last 10 people who reposted it:

var post = API.wall.getById({posts: Args.post_id})[0];
var reposterIDs = API.wall.getReposts({post_id: Args.post_id, count: 10}).items@.from_id;
return {post: post, reposter_names: API.users.get({user_ids: reposterIDs})@.first_name};

Retrieve the current user’s friends, but only those that are from other servers:

var friends = API.friends.get({fields: "photo_200,domain", count: 1000}).items;
var result = [];
for (var i = 0; i < friends.length; i = i + 1) {
	var friend = friends[i];
	if (!friend.domain) continue;
	result.push(friend);
}
return result;
This method requires an access token, but no permissions.

Parameters

code
string

Your script.

Result

Whatever you return using the return statement. If there is no return statement, returns null.

If any of the API calls failed, there will be an extra execute_errors field in the top-level JSON object (next to response) with error objects for each failed call with extra method parameter specifying which method it was.

Errors

12
Failed to compile the script
13
A runtime error occurred during script execution
Global errors may also occur when calling this method.