---
title: "AdsGram SDK API Reference"
description: "Full reference for the AdsGram JavaScript SDK: init, AdController methods (show, destroy, addEventListener), parameters, and event types."
outline: [2,3]
---
# API Reference

## Adsgram methods

### init
Create `AdController` which is used to show, load and destroy ad.\
It's enough to initialize only once for one `blockId`.\
If you call `init` function with same `blockId` more than once, you will get the same `AdController` for this `blockId`.
#### Syntax
```js
const AdController = window.Adsgram.init({
  blockId: "your-block-id",
  debug: true,
  debugConsole: true,
  debugBannerType: "FullscreenMedia"
});
```

#### Parameters
`blockId`
> String with digits, for example `"123"`.\
> The unique identifier of the ad block that you receive in the [publisher's account](https://partner.adsgram.ai/).\
> Detailed instruction on how to get blockId you can find in [Get blockId section](./get-block-id)

`debug` <Badge type="info" text="Optional" />
> Boolean value if set to `true` you will get response from server with test ad and detailed logs with `ADSGRAM DEBUG LOG` prefix will be output to the browser console for integration debugging.
> ::: danger
> Don't forget to remove or set to `false`, when release to production.\
> We do not add **test ad** impressions to the statistics, so you will not see the statistics of its impressions, shows, clicks, etc. on the platform and block page.\
> Request to `Reward Url` won't be sent.
> :::

`debugConsole` <Badge type="info" text="Optional" />
> Boolean value. Default is `true`. Works only if debug parameter is true.\
> If `true`, detailed logs with `ADSGRAM DEBUG LOG` prefix will be output to the browser console.
> If `false`, logs are not output to the console, even in debug mode.

`debugBannerType` <Badge type="info" text="Optional" />
> Possible values are `"FullscreenMedia"` and `"RewardedVideo"`.\
> Use this value when you want to get exact test banner type. Works only if debug parameter equals `true`.

#### Return value
`AdController`

## AdController methods
### show
Loads and shows ad.
#### Syntax
::: code-group
```js [javascript]
AdController.show();
```
```ts [typescript]
const showPromise: Promise<ShowPromiseResult> = AdController.show();
```
:::
#### Return value
> `Promise` which becomes resolved if the user watches the ad till the end or closes it in the Interstitial format, otherwise rejected.\
> Result of the promise has the following types:
> ```ts
> interface ShowPromiseResult {
>   done: boolean; // true if user watch till the end or close it in interstitial format, otherwise false
>   description: string; // event description
>   state: 'load' | 'render' | 'playing' | 'destroy'; // banner state
>   error: boolean; // true if event was emitted due to error, otherwise false
> }
> ```

#### Examples
Using [promises API](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then):
::: code-group
```js [javascript]
AdController.show().then((result) => {
  // user watch ad till the end or close it in interstitial format
  // your code to reward user
}).catch((result) => {
  // user get error during playing ad or skip ad
  // do nothing or whatever you want
})
```
```ts [typescript]
AdController.show().then((result: ShowPromiseResult) => {
  // user watch ad till the end or close it in interstitial format
  // your code to reward user
}).catch((result: ShowPromiseResult) => {
  // user get error during playing ad or skip ad
  // do nothing or whatever you want
})
```
:::
You can also achieve the same behavior using [await](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await)
::: code-group
```js [javascript]
try {
  const showPromiseResult = await AdController.show();
  // user watch ad till the end or close it in interstitial format
  // your code to reward user
} catch (showPromiseResult) {
  // user get error during playing ad or skip ad
  // do nothing or whatever you want
}
```
```ts [typescript]
try {
  const showPromiseResult: ShowPromiseResult = await AdController.show();
  // user watch ad till the end or close it in interstitial format
  // your code to reward user
} catch (error) {
  const showPromiseResult = error as ShowPromiseResult;
  // user get error during playing ad or skip ad
  // do nothing or whatever you want
}
```
:::
::: tip
Chain promise with `finally` if you need to add any extra action after the ad ends playing, is skipped or gets an error.
:::
### destroy
To stop showing ad and remove loaded banner data.
#### Syntax
```js
AdController.destroy();
```
#### Return value
> None(`undefined`)
::: info
Usually you don't need to call it manually, in case of an error, skip or ad fully watched the ad will be automatically destroyed. 
:::
### addEventListener
Subscribes to `event` and invokes `callback` function if `event` was fired.\
It is used if you need more control over the display of ads.
#### Syntax
```js
AdController.addEventListener('onReward', () => {
    // your code to reward user
});
```
#### Parameters {#add-event-listener-parameters}
`event`
> ##### Types of events:
> | Event              | Occurs                                                             |
> |--------------------|--------------------------------------------------------------------|
> | `onStart`          | when the first banner frame is shown                               |
> | `onSkip`           | when user closed the ad                                            |
> | `onReward`         | when user watched rewarded banner till the end                     |
> | `onComplete`       | when user watched interstitial banner till the end or close it     |
> | `onError`          | when user gets an error during render or playing the banner        |
> | `onBannerNotFound` | when there is no banner to display                                 |
> | `onNonStopShow`    | when user is trying to watch several ads in a row                  |
> | `onTooLongSession` | when session is too long, user needs to restart the app to get ads |
>
> ::: tip
> If you want to show your custom alert, you can subscribe to `onBannerNotFound`, `onNonStopShow` or `onTooLongSession`.\
> Otherwise we'll show the default alert.\
> The alert is displayed in the user's language or in English \
> and looks like this 👇
> <img src="./images/default_alert_banner.webp" alt="alert" width="300" />
> :::

`callback`
> Function that will be invoked when an event of the specified type occurs.

#### Return value
> None(`undefined`)
### removeEventListener
Unsubscribes to `event` with determined `callback`
#### Syntax
```ts
AdController.removeEventListener('onReward', rewardFunction);
```
#### Parameters
The same as in [addEventListener](#add-event-listener-parameters)

#### Return value
> None(`undefined`)
