telegram-miniapp-sdk
v2.2.0
Published
SDK for Telegram Mini Apps with authentication and ad integration
Maintainers
Readme
Telegram Mini App SDK
A comprehensive SDK for building Telegram Mini Apps and Games with integrated advertising and offer wall capabilities.
Features
- 🔐 Secure Authentication
- 📱 Advertisement Integration
- 💰 Offer Wall Implementation
- 🎮 Game Integration Support
- 🔄 Real-time Updates
Installation
npm install telegram-miniapp-sdk
# or
yarn add telegram-miniapp-sdkQuick Start
Initialize the SDK
The SDK uses a singleton pattern, which means you only need to initialize it once in your application:
import TelegramMiniAppSDK from 'telegram-miniapp-sdk';
// Initialize the SDK
try {
const sdk = await TelegramMiniAppSDK.initialize({
apiKey: 'YOUR_API_KEY',
});
console.log('SDK initialized successfully');
} catch (error) {
console.error('SDK initialization failed:', error);
}Using the SDK
After initialization, you can access the SDK instance from anywhere in your application:
// Get the SDK instance
const sdk = TelegramMiniAppSDK.getInstance();
// Get Offer Wall
try {
const offers = await sdk.getOfferWall('YOUR_PUBLISHER_ID', {
user_unique_id: 'OPTIONAL_USER_ID',
limit: 10 // Optional and Default value is 3
});
console.log('Offers:', offers);
} catch (error) {
console.error('Failed to fetch offers:', error);
}
// Get Display Ads
try {
const ads = await sdk.getAd('YOUR_PUBLISHER_ID', {
user_unique_id: 'OPTIONAL_USER_ID'
});
console.log('Ads:', ads);
} catch (error) {
console.error('Failed to fetch ads:', error);
}React Integration Example
import React, { useEffect, useState } from 'react';
import TelegramMiniAppSDK from 'telegram-miniapp-sdk';
// Initialize SDK at app startup
const initializeSDK = async () => {
try {
await TelegramMiniAppSDK.initialize({
apiKey: process.env.REACT_APP_SDK_API_KEY
});
return true;
} catch (error) {
console.error('SDK initialization failed:', error);
return false;
}
};
// Use in components
const MyComponent = () => {
const [offers, setOffers] = useState([]);
useEffect(() => {
const fetchOffers = async () => {
try {
const sdk = TelegramMiniAppSDK.getInstance();
const offerWall = await sdk.getOfferWall("YOUR_PUBLISHER_ID");
setOffers(offerWall);
} catch (error) {
console.error('Failed to fetch offers:', error);
}
};
fetchOffers();
}, []);
return (
// Your component JSX
);
};Error Handling
The SDK throws errors in the following cases:
- When trying to use
getInstance()before initialization - When initialization fails
- When API requests fail
- When invalid parameters are provided
Always wrap SDK calls in try-catch blocks for proper error handling.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Support
For help, contact our support team.
