---
metadata:
  - name: generator
    content: Diplodoc Platform v5.52.0
alternate:
  - https://yandex.com/dev/games/doc/en/sdk/sdk-example.md
  - https://yandex.com/dev/games/doc/hi/sdk/sdk-example.md
  - https://yandex.com/dev/games/doc/ko/sdk/sdk-example.md
  - https://yandex.com/dev/games/doc/ru/sdk/sdk-example.md
  - https://yandex.com/dev/games/doc/tr/sdk/sdk-example.md
  - https://yandex.com/dev/games/doc/vi/sdk/sdk-example.md
  - https://yandex.com/dev/games/doc/zh/sdk/sdk-example.md
  - href: en/sdk/sdk-example.md
    type: text/markdown
    title: Markdown version
  - href: ../llms.txt
    type: text/markdown
    title: llms.txt
---
> **Documentation Index:** Fetch the complete configuration index at https://yandex.com/dev/games/doc/en/llms.txt

# Example

<!-- source: en/_includes/script-common.md -->
<!-- source: en/_includes/script/index-js.md -->

<!-- endsource: en/_includes/script/index-js.md -->

<!-- source: en/_includes/script/requirements-js.md -->

<!-- endsource: en/_includes/script/requirements-js.md -->

<!-- source: en/_includes/script/image-modal-js.md -->

<!-- endsource: en/_includes/script/image-modal-js.md -->
<!-- endsource: en/_includes/script-common.md -->

Below you will find code samples for setting up the Yandex Games SDK synchronously and asynchronously.

{% list tabs %}

* Synchronous connection

    Example specifics:

    - [Callback functions](*key_callback) are not set for the first ad call.
    - Every callback function possible is specified for the second call and each subsequent call.
    - A `'click'` event handler is assigned to the **Show ads** button (an ad is called on each button click).

    ```html showLineNumbers
    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
        <meta name="mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <title>Page example with SDK configured synchronously</title>
        
        
    </head>
    <body>
        <button id="button">Show ads</button>
    </body>
    </html>
    ```

* Asynchronous connection

    Example specifics:

    - An [onClose](*key_onClose) callback function is set for the first ad call.
    - Every [callback function](*key_callback) possible is specified for the second call and each subsequent call.
    - The `onClose` callback function includes code that will be run after the ad unit is closed.
    - All errors that occur during the SDK operation or during the execution of callback functions are passed to the [onError](*key_onError) function.
    - A `'click'` event handler is assigned to the **Show ads** button (an ad is called on each button click).

    ```html showLineNumbers
    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
        <meta name="mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <title>Example of page with SDK configured asynchronously</title>
        
    </head>
    <body>
    <!-- Yandex Games SDK -->
    
    <button id="button">Show ads</button>
    </body>
    </html>
    ```

{% endlist %}

[*key_callback]: - `onClose`: Called when the ad is closed, on error, or if the ad failed to open due to too frequent calls. It's used with the `wasShown` argument (`boolean` type), the value of which indicates whether the ad was shown or not.
- `onOpen`: Called when the ad is opened successfully.
- `onError`: Called when an error occurs. The error object is passed to the callback function.

[*key_onClose]: `onClose`: Called when the ad is closed, on error, or if the ad failed to open due to too frequent calls. It's used with the `wasShown` argument (`boolean` type), which indicates whether the ad was shown or not.

[*key_onError]: `onError`: Called when an error occurs. The error object is passed to the callback function.

[*key_onOpen]: `onOpen`: Called when the ad is opened successfully.