How to share file in Telegram Mini App


telegram web TMA

Since a Telegram Mini App is simply HTML, CSS, and JavaScript running inside a native client, you can use the navigator.share() API.

An example of sharing sound file in Typescript app:

import WebApp from '@twa-dev/sdk'

try {
  navigator.share({
    url: "soundsite.com/static" + sound.file,
    text: sound.name,
    title: sound.name,
  });
} catch (error) {
  WebApp.showAlert(
    "Unfortunatelly sharing unavailable on desktop, please try on mobile phone"
  );
}

However, sharing will only work on native iOS, Android, and Desktop Telegram clients. The Telegram Mini App in the Telegram Web client is treated as a third-party iframe, and in this case, the browser will restrict access to the navigator.share() API. You will encounter the following error:

Unhandled Promise Rejection: NotAllowedError: Third-party iframes are not allowed to call share() unless explicitly allowed via Feature-Policy (web-share)
comments powered by Disqus