TencentCloud SMS SDK for Rust
A Rust implementation of the TencentCloud SMS SDK, providing a comprehensive interface for sending SMS messages through TencentCloud's SMS service.
Features
- Complete SMS API Coverage: Send SMS messages (verification codes, notifications, marketing)
- Async/Await Support: Built on
tokio
for high-performance async operations - Type Safety: Strongly typed request/response models with validation
- Error Handling: Comprehensive error types with detailed error information
- Authentication: TC3-HMAC-SHA256 signature algorithm support
- Multiple Regions: Support for all TencentCloud regions
- Domestic & International: Support for both domestic and international SMS
- Configurable: Customizable HTTP profiles, timeouts, and proxy settings
Installation
Add this to your Cargo.toml
:
[]
= "0.1.2"
= { = "1.0", = ["full"] }
Quick Start
Basic Usage
use ;
async
Using Environment Variables
use ;
async
Configuration
Custom HTTP Profile
use ;
let credential = from_env?;
// Create custom HTTP profile
let mut http_profile = new;
http_profile
.set_req_timeout
.set_connect_timeout
.set_keep_alive
.set_endpoint;
// Create client profile
let client_profile = with_http_profile;
// Create client with custom profile
let client = with_profile;
Proxy Configuration
let mut http_profile = new;
http_profile
.set_proxy_host
.set_proxy_port;
let client_profile = with_http_profile;
let client = with_profile;
Examples
Domestic SMS
let request = new;
let response = client.send_sms.await?;
International SMS
let request = new_international;
let response = client.send_sms.await?;
Batch SMS
let request = new;
let response = client.send_sms.await?;
// Check results
for status in &response.send_status_set
Advanced Request Configuration
let mut request = new;
// Set additional options
request
.set_session_context
.set_extend_code
.set_sender_id;
let response = client.send_sms.await?;
Error Handling
The SDK provides comprehensive error handling with detailed error information:
use error_codes;
match client.send_sms.await
Response Handling
The SendSmsResponse
provides various methods to check the results:
let response = client.send_sms.await?;
// Check overall success
if response.is_all_success else
// Get statistics
println!;
println!;
println!;
println!;
// Check specific phone numbers
if response.check_phone_success
// Get failed numbers with reasons
let failed_numbers = response.get_failed_numbers;
for in failed_numbers
Regions
The SDK supports all TencentCloud regions:
// China regions
let client = new; // Beijing
let client = new; // Shanghai
let client = new; // Guangzhou
let client = new; // Chengdu
// International regions
let client = new; // Singapore
let client = new; // Seoul
let client = new; // Tokyo
let client = new; // US East
let client = new; // Europe
Prerequisites
Before using the SDK, you need to:
- Create a TencentCloud account and obtain your
SecretId
andSecretKey
- Enable SMS service in the TencentCloud console
- Create SMS signatures and get them approved
- Create SMS templates and get them approved
- Get your SMS SDK App ID from the SMS console
Environment Variables
The SDK supports the following environment variables:
TENCENTCLOUD_SECRET_ID
orTC_SECRET_ID
: Your TencentCloud Secret IDTENCENTCLOUD_SECRET_KEY
orTC_SECRET_KEY
: Your TencentCloud Secret KeyTENCENTCLOUD_TOKEN
orTC_TOKEN
: Session token (for temporary credentials)
Security Best Practices
- Never hardcode credentials in your source code
- Use environment variables or configuration files for credentials
- Implement proper error handling to avoid credential leakage
- Use temporary credentials when possible
- Regularly rotate your access keys
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Support
- Documentation: https://docs.rs/tencentcloud-sms-sdk
- Issues: GitHub Issues
- TencentCloud SMS Documentation: https://cloud.tencent.com/document/product/382
Acknowledgments
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.