Skip to content

WebApp Methods

The window.Safew.WebApp object provides the following methods to control Mini App behavior and interactions.

Lifecycle

ready

javascript
Safew.WebApp.ready()

Notifies the client that the Mini App is ready to be displayed. Should be called as early as possible after the page has loaded.

close

javascript
Safew.WebApp.close()

Closes the Mini App.

isVersionAtLeast

javascript
Safew.WebApp.isVersionAtLeast(version)

Checks if the current client version meets the requirement.

ParameterTypeRequiredDescription
versionStringYesMinimum required version, e.g., "7.10"

Returns: Boolean

UI Control

setHeaderColor

javascript
Safew.WebApp.setHeaderColor(color)

Sets the Mini App header bar color.

ParameterTypeRequiredDescription
colorStringYesColor keyword (bg_color, secondary_bg_color) or hex color value

setBackgroundColor

javascript
Safew.WebApp.setBackgroundColor(color)

Sets the Mini App background color.

ParameterTypeRequiredDescription
colorStringYesColor keyword or hex color value

setBottomBarColor

javascript
Safew.WebApp.setBottomBarColor(color)

Sets the bottom bar color. Bot API 7.10+

ParameterTypeRequiredDescription
colorStringYesColor keyword or hex color value

enableClosingConfirmation

javascript
Safew.WebApp.enableClosingConfirmation()

Enables closing confirmation. When enabled, a confirmation dialog will appear when the user tries to close the Mini App.

disableClosingConfirmation

javascript
Safew.WebApp.disableClosingConfirmation()

Disables closing confirmation.

enableVerticalSwipes

javascript
Safew.WebApp.enableVerticalSwipes()

Enables vertical swipe gestures, allowing the user to close or minimize the Mini App by swiping down. Bot API 7.7+

disableVerticalSwipes

javascript
Safew.WebApp.disableVerticalSwipes()

Disables vertical swipe gestures. Bot API 7.7+

requestFullscreen

javascript
Safew.WebApp.requestFullscreen()

Requests fullscreen mode. Bot API 8.0+

exitFullscreen

javascript
Safew.WebApp.exitFullscreen()

Exits fullscreen mode. Bot API 8.0+

lockOrientation

javascript
Safew.WebApp.lockOrientation()

Locks the current screen orientation. Bot API 8.0+

unlockOrientation

javascript
Safew.WebApp.unlockOrientation()

Unlocks screen orientation. Bot API 8.0+

Expand & Scroll

expand

javascript
Safew.WebApp.expand()

Expands the Mini App to its maximum available height.

requestViewport

javascript
Safew.WebApp.requestViewport()

Requests a viewport information refresh.

Data & Communication

sendData

javascript
Safew.WebApp.sendData(data)

Sends data to the Bot. After calling, the Mini App will close and the data will be sent to the Bot via a web_app_data service message.

ParameterTypeRequiredDescription
dataStringYesData to send, 1-4096 bytes

Note

This method is only available when the Mini App is launched via a Keyboard Button.

switchInlineQuery

javascript
Safew.WebApp.switchInlineQuery(query, chat_types)

Switches to inline mode and inserts the inline query in the specified chat types. Bot API 6.7+

ParameterTypeRequiredDescription
queryStringYesInline query text to insert
chat_typesArray of StringNoAllowed chat types: "users", "bots", "groups", "channels"
javascript
Safew.WebApp.openLink(url, options)

Opens a link in an external browser.

ParameterTypeRequiredDescription
urlStringYesURL to open
optionsObjectNoOptions
options.try_instant_viewBooleanNoWhether to try opening with Instant View
javascript
Safew.WebApp.openSafewLink(url)

Opens a SafeW link within the app.

ParameterTypeRequiredDescription
urlStringYesSafeW link (https://t.me/...)

Sharing & Social

shareToStory

javascript
Safew.WebApp.shareToStory(media_url, params)

Shares content to a story. Bot API 7.8+

ParameterTypeRequiredDescription
media_urlStringYesMedia file URL (image or video)
paramsObjectNoShare parameters
params.textStringNoAdditional text description
params.widget_linkObjectNoWidget link
params.widget_link.urlStringNoLink URL
params.widget_link.nameStringNoLink display name

shareMessage

javascript
Safew.WebApp.shareMessage(msg_id, callback)

Shares a message prepared by the Bot to a chat. Bot API 8.0+

ParameterTypeRequiredDescription
msg_idStringYesMessage ID prepared by the Bot
callbackFunctionNoCallback function with sent (Boolean) parameter indicating success

setEmojiStatus

javascript
Safew.WebApp.setEmojiStatus(custom_emoji_id, params, callback)

Sets a custom emoji status for the user. Bot API 8.0+

ParameterTypeRequiredDescription
custom_emoji_idStringYesCustom emoji ID
paramsObjectNoParameters
params.durationIntegerNoStatus duration in seconds
callbackFunctionNoCallback function with set (Boolean) parameter indicating success

downloadFile

javascript
Safew.WebApp.downloadFile(params, callback)

Requests a file download. Bot API 8.0+

ParameterTypeRequiredDescription
paramsObjectYesDownload parameters
params.urlStringYesFile download URL
params.file_nameStringYesSuggested file name
callbackFunctionNoCallback function with accepted (Boolean) parameter indicating user consent

Events

onEvent

javascript
Safew.WebApp.onEvent(eventType, eventHandler)

Subscribes to an event. See Events for details.

ParameterTypeRequiredDescription
eventTypeStringYesEvent name
eventHandlerFunctionYesEvent handler function

offEvent

javascript
Safew.WebApp.offEvent(eventType, eventHandler)

Unsubscribes from an event.

ParameterTypeRequiredDescription
eventTypeStringYesEvent name
eventHandlerFunctionYesPreviously registered event handler function

Usage Example

javascript
const sw = window.Safew.WebApp;

// Initialize
sw.ready();
sw.expand();

// Set UI
sw.setHeaderColor('#1a1a2e');
sw.setBackgroundColor('#16213e');
sw.enableClosingConfirmation();

// Listen for theme changes
sw.onEvent('themeChanged', () => {
  console.log('Theme updated:', sw.colorScheme);
});

// Send data to Bot
sw.MainButton.setText('Submit');
sw.MainButton.onClick(() => {
  sw.sendData(JSON.stringify({ action: 'submit' }));
});
sw.MainButton.show();