| Crates.io | tencentcloud-sms-sdk |
| lib.rs | tencentcloud-sms-sdk |
| version | 0.1.2 |
| created_at | 2025-07-15 13:50:29.076866+00 |
| updated_at | 2025-09-14 08:23:10.993565+00 |
| description | TencentCloud SMS SDK for Rust |
| homepage | https://github.com/y-zion/tencentcloud-sms-sdk-rust |
| repository | https://github.com/y-zion/tencentcloud-sms-sdk-rust |
| max_upload_size | |
| id | 1753384 |
| size | 148,416 |
A Rust implementation of the TencentCloud SMS SDK, providing a comprehensive interface for sending SMS messages through TencentCloud's SMS service.
tokio for high-performance async operationsAdd this to your Cargo.toml:
[dependencies]
tencentcloud-sms-sdk = "0.1.2"
tokio = { version = "1.0", features = ["full"] }
use tencentcloud_sms_sdk::{Client, Credential, SendSmsRequest};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create credentials
let credential = Credential::new("your_secret_id", "your_secret_key", None);
// Create client
let client = Client::new(credential, "ap-guangzhou");
// Create SMS request
let request = SendSmsRequest::new(
vec!["+8613800000000".to_string()], // Phone numbers
"1400000000", // SMS SDK App ID
"123456", // Template ID
"YourSignature", // SMS signature
vec!["123456".to_string()], // Template parameters
);
// Send SMS
let response = client.send_sms(request).await?;
println!("SMS sent! Request ID: {}", response.request_id);
println!("Success count: {}", response.success_count());
println!("Failed count: {}", response.failed_count());
Ok(())
}
use tencentcloud_sms_sdk::{Client, Credential, SendSmsRequest};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Set environment variables:
// export TENCENTCLOUD_SECRET_ID=your_secret_id
// export TENCENTCLOUD_SECRET_KEY=your_secret_key
let credential = Credential::from_env()?;
let client = Client::new(credential, "ap-guangzhou");
// ... rest of the code
Ok(())
}
use tencentcloud_sms_sdk::{Client, ClientProfile, Credential, HttpProfile};
let credential = Credential::from_env()?;
// Create custom HTTP profile
let mut http_profile = HttpProfile::new();
http_profile
.set_req_timeout(30)
.set_connect_timeout(30)
.set_keep_alive(true)
.set_endpoint("sms.ap-guangzhou.tencentcloudapi.com");
// Create client profile
let client_profile = ClientProfile::with_http_profile(http_profile);
// Create client with custom profile
let client = Client::with_profile(credential, "ap-guangzhou", client_profile);
let mut http_profile = HttpProfile::new();
http_profile
.set_proxy_host(Some("proxy.example.com"))
.set_proxy_port(Some(8080));
let client_profile = ClientProfile::with_http_profile(http_profile);
let client = Client::with_profile(credential, "ap-guangzhou", client_profile);
let request = SendSmsRequest::new(
vec!["+8613800000000".to_string()],
"1400000000",
"123456",
"YourSignature",
vec!["123456".to_string()],
);
let response = client.send_sms(request).await?;
let request = SendSmsRequest::new_international(
vec!["+1234567890".to_string()],
"1400000000",
"123456",
vec!["123456".to_string()],
);
let response = client.send_sms(request).await?;
let request = SendSmsRequest::new(
vec![
"+8613800000000".to_string(),
"+8613800000001".to_string(),
"+8613800000002".to_string(),
],
"1400000000",
"123456",
"YourSignature",
vec!["123456".to_string()],
);
let response = client.send_sms(request).await?;
// Check results
for status in &response.send_status_set {
if status.is_success() {
println!("✓ {} sent successfully", status.phone_number);
} else {
println!("✗ {} failed: {}", status.phone_number, status.message);
}
}
let mut request = SendSmsRequest::new(
vec!["+8613800000000".to_string()],
"1400000000",
"123456",
"YourSignature",
vec!["123456".to_string()],
);
// Set additional options
request
.set_session_context("user_session_123")
.set_extend_code("01")
.set_sender_id("YourSenderID");
let response = client.send_sms(request).await?;
The SDK provides comprehensive error handling with detailed error information:
use tencentcloud_sms_sdk::error::error_codes;
match client.send_sms(request).await {
Ok(response) => {
println!("Success: {}", response.request_id);
}
Err(e) => {
println!("Error: {}", e.print_all());
// Check specific error types
if e.is_network_error() {
println!("Network error occurred");
} else if let Some(code) = e.code() {
match code {
error_codes::SIGNATURE_INCORRECT_OR_UNAPPROVED => {
println!("Please check your SMS signature");
}
error_codes::TEMPLATE_INCORRECT_OR_UNAPPROVED => {
println!("Please check your SMS template");
}
error_codes::SMS_SDK_APP_ID_VERIFY_FAIL => {
println!("Please check your SMS SDK App ID");
}
_ => {
println!("API error: {}", code);
}
}
}
}
}
The SendSmsResponse provides various methods to check the results:
let response = client.send_sms(request).await?;
// Check overall success
if response.is_all_success() {
println!("All messages sent successfully!");
} else {
println!("Some messages failed to send");
}
// Get statistics
println!("Total: {}", response.send_status_set.len());
println!("Success: {}", response.success_count());
println!("Failed: {}", response.failed_count());
println!("Total fee: {}", response.get_total_fee());
// Check specific phone numbers
if response.check_phone_success("+8613800000000") {
println!("Message to +8613800000000 was sent successfully");
}
// Get failed numbers with reasons
let failed_numbers = response.get_failed_numbers();
for (phone, reason) in failed_numbers {
println!("Failed: {} - {}", phone, reason);
}
The SDK supports all TencentCloud regions:
// China regions
let client = Client::new(credential, "ap-beijing"); // Beijing
let client = Client::new(credential, "ap-shanghai"); // Shanghai
let client = Client::new(credential, "ap-guangzhou"); // Guangzhou
let client = Client::new(credential, "ap-chengdu"); // Chengdu
// International regions
let client = Client::new(credential, "ap-singapore"); // Singapore
let client = Client::new(credential, "ap-seoul"); // Seoul
let client = Client::new(credential, "ap-tokyo"); // Tokyo
let client = Client::new(credential, "us-east-1"); // US East
let client = Client::new(credential, "eu-frankfurt"); // Europe
Before using the SDK, you need to:
SecretId and SecretKeyThe SDK supports the following environment variables:
TENCENTCLOUD_SECRET_ID or TC_SECRET_ID: Your TencentCloud Secret IDTENCENTCLOUD_SECRET_KEY or TC_SECRET_KEY: Your TencentCloud Secret KeyTENCENTCLOUD_TOKEN or TC_TOKEN: Session token (for temporary credentials)Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.
This SDK is based on the TencentCloud C++ SDK implementation by Cursor and follows the same API patterns and structure for consistency across different language SDKs.