Stored procedures (game settings)
Stored procedures are functions written in VKScript whose code is stored in the game settings and executed on the VK servers.
- •
Each procedure can execute one or more API requests. In this case, the results of individual requests aren't transmitted to the user's device, which speeds up the game and saves traffic. The latter is especially important for games that run on mobile devices.
- •
Stored procedures don't require a game server. This means you can move part of the business logic out of the server code, thereby simplifying it.
In the Stored procedures settings section, you can create, test, edit and delete stored procedures for your game.
To open the section:
- 1.Go to the apps list.
- 2.Open the settings of the game you need.
- 3.Select Development → Stored procedures from the left-side menu.
Access
Stored procedures can be edited by:
- •Game creator.
- •Administrator with full access.
- •Administrator with partial access and the Editing permission.
Learn more about permissions in the Administrators section.
Working with procedures
Create a procedure
- 1.
Click Add procedure.
- 2.
Enter the procedure name using Latin letters. Length: 1 to 100 characters.
The procedure name used in code always starts with the
execute.prefix. This prefix can't be changed. - 3.
In the API version field, specify the VK API version that will be used to call API requests from the procedure.
The parameters and return values of API requests may differ depending on the version. Select the expected version to avoid potential issues.
- 4.
Enter the procedure code. Some key syntax features are described in the VKScript section.
- 5.
To check how the code works, click Run.
- •The result will be displayed below the code input field.
- •The data is displayed in the form in which it will be passed to the procedure call code.
- •Before running, the platform checks the syntax of the procedure code. Code with syntax errors can't be executed.
The field will display information about the first syntax error found: text on a red background with the error description and the line number in the code.
- •
- 6.
When everything is ready, click Save.
Edit a procedure
- 1.
To find a procedure in the list, start typing its name in the search field.
- 2.
Click the row with the required procedure.
- 3.
Make changes and click Save.
To exit without saving, click Back in the top right corner of the screen. In this case, all changes will be discarded.
Create a procedure version
To keep the game working even when the stored procedure code changes, you can create multiple versions of a procedure.
The procedure can be initiated from server-side code and from code running on the client side. In this case, a situation may arise where the client-side code isn't compatible with the new procedure code. In such a case, you can create a new version of the procedure and use it in the updated client code, while the old code will work with the old version.
To create a stored procedure version:
- 1.
Open the procedure for editing and, in the Procedure version drop-down list, select the version you want to use as the base.
- 2.
In the same list, click Add version. A new procedure version will be created based on the selected one.
The Add version button will appear after you save the procedure for the first time.
- 3.
Enter the code for the new version and save the changes.
Delete a procedure
To delete a stored procedure and all its versions:
- 1.
Find the procedure in the list. To do this, start typing its name in the search field.
- 2.
Hover over the required row and click Delete procedure.
Delete a procedure version
To delete a specific version of a stored procedure:
- 1.
Open the procedure for editing.
- 2.
Click Procedure version and select a version from the list.
Important! If the procedure has multiple versions, deleting the first version will delete all versions.
- 3.
In the bottom right corner, click Delete version.
Executing a procedure
Perform a test run
To test a stored procedure, open it for editing and click Run.
- •The result will be displayed below the code input field.
- •The data is displayed in the form in which it will be passed to the procedure call code.
- •Before running, the platform checks the syntax of the procedure code. Code with syntax errors can't be executed.
The field will display information about the first syntax error found: text on a red background with the error description and the line number in the code.
Calling a procedure in code
To execute a procedure, call the execute API method and pass the procedure name and parameters. Stored procedure parameters are specified when the procedure is called — they don't need to be declared in the procedure editor. To access a parameter, use the syntax Args.param-name.
curl -X POST 'https://api.vk.ru/method/execute.<PROCEDURE-NAME>' \
-H 'Authorization: Bearer <ACCESS-TOKEN>' \
-F 'v=<API-VERSION>' \
-F 'func_v=<PROCEDURE-VERSION>' \
-F '<PARAMETER-1>=<VALUE-1>' \
-F '<PARAMETER-2>=<VALUE-2>'The stored procedure can also be called using the VKWebAppCallAPIMethod event.
bridge.send("VKWebAppCallAPIMethod", {
method: "execute.<PROCEDURE-NAME>",
params: {
<PARAMETER-1>: "<VALUE-1>",
<PARAMETER-2>: "<VALUE-2>",
v: "<API-VERSION>",
func_v: "<PROCEDURE-VERSION>",
access_token: "<ACCESS-TOKEN>"
}
});VKScript
Some key features of VKScript are listed in the table below.
Feature | |
|---|---|
Access to stored procedure parameters | Args.param_name |
Calling API requests | var r = API.users.get( {"user_ids": 123} );
Here, users.get is the group name and API method name as specified in the documentation. |
Access to request results | r@.field_name |
Return a value and exit the procedure | return { "result": 12345}; |
Creating functions within a procedure | Not supported. |
Related materials
- •
- •