Skip to content

6. API Reference

RAprogramm edited this page Nov 4, 2025 · 1 revision

Chapter 6: API Reference

Comprehensive API reference organized by feature.

User Operations

Request Contact

use telegram_webapp_sdk::api::user;

user::request_contact(move |contact_requested| {
    if contact_requested {
        // User shared contact
    }
});

Request Phone Number

user::request_phone_number(move |phone_requested| {
    if phone_requested {
        // User shared phone
    }
});

Storage APIs

Cloud Storage

use telegram_webapp_sdk::api::cloud_storage;

// Set item
cloud_storage::set_item("key", "value", |success| {
    if success {
        // Saved
    }
}).await;

// Get item
cloud_storage::get_item("key", |value| {
    if let Some(val) = value {
        // Retrieved
    }
}).await;

Device Storage (localStorage)

use telegram_webapp_sdk::api::device_storage;

// Save
device_storage::set("key", "value");

// Load
if let Some(value) = device_storage::get("key") {
    // Use value
}

Secure Storage

use telegram_webapp_sdk::api::secure_storage;

// Save securely
secure_storage::set("api_key", "secret", |success| {
    // Saved
}).await;

// Retrieve
secure_storage::get("api_key", |value| {
    // Use secret
}).await;

Biometric Authentication

use telegram_webapp_sdk::api::biometric;

// Initialize biometric manager
biometric::init(|initialized| {
    if initialized {
        // Request access
        biometric::request_access("Unlock app", |granted| {
            if granted {
                // Authenticate
                biometric::authenticate("Verify identity", |authenticated| {
                    if authenticated {
                        // Access granted
                    }
                });
            }
        });
    }
}).await;

Device Sensors

Accelerometer

use telegram_webapp_sdk::api::accelerometer;

// Start
accelerometer::start(100, |started| {
    if started {
        // Read values
        if let Some(accel) = accelerometer::get_acceleration() {
            println!("X: {}, Y: {}, Z: {}", accel.x, accel.y, accel.z);
        }
    }
});

// Stop
accelerometer::stop();

Gyroscope

use telegram_webapp_sdk::api::gyroscope;

gyroscope::start(100, |started| {
    if started {
        if let Some(gyro) = gyroscope::get_angular_velocity() {
            println!("X: {}, Y: {}, Z: {}", gyro.x, gyro.y, gyro.z);
        }
    }
});

Device Orientation

use telegram_webapp_sdk::api::device_orientation;

device_orientation::start(100, |started| {
    if started {
        if let Some(orient) = device_orientation::get_orientation() {
            println!("Alpha: {}, Beta: {}, Gamma: {}", orient.alpha, orient.beta, orient.gamma);
        }
    }
});

Location Services

use telegram_webapp_sdk::api::location_manager;

// Initialize
location_manager::init(|initialized| {
    if initialized {
        // Get location
        location_manager::get_location(|location| {
            if let Some(loc) = location {
                println!("Lat: {}, Lon: {}", loc.latitude, loc.longitude);
            }
        });
    }
}).await;

Haptic Feedback

use telegram_webapp_sdk::api::haptic;

// Impact
haptic::impact_occurred("medium");  // light, medium, heavy, rigid, soft

// Notification
haptic::notification_occurred("success");  // error, success, warning

// Selection changed
haptic::selection_changed();

Theme & Appearance

use telegram_webapp_sdk::api::theme;

// Set colors
theme::set_header_color("#FF0000");
theme::set_background_color("#FFFFFF");
theme::set_bottom_bar_color("#000000");

Viewport

use telegram_webapp_sdk::api::viewport;

// Expand to full height
viewport::expand_viewport();

// Get dimensions
let height = viewport::get_viewport_height();
let stable_height = viewport::get_viewport_stable_height();
let width = viewport::get_viewport_width();

Next: Chapter 7 - Common Patterns

Clone this wiki locally