Unimatrix Rust SDK
Installation
The recommended way to install the Unimatrix SDK for Rust is to add the uni-sdk crate from crates.io to your project's Cargo.toml. The SDK requires Rust 1.86 or later.
Add the asynchronous SDK and Tokio runtime to Cargo.toml:
[dependencies]
uni-sdk = "0.3"
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
For a synchronous application, enable the blocking feature instead:
[dependencies]
uni-sdk = { version = "0.3", features = ["blocking"] }
Usage
Initialize a client
use uni_sdk::UniClient;
fn main() -> uni_sdk::Result<()> {
let client = UniClient::new("your access key id", "your access key secret")?;
Ok(())
}
Alternatively, configure credentials with environment variables and use UniClient::from_env():
export UNIMTX_ACCESS_KEY_ID=your_access_key_id
export UNIMTX_ACCESS_KEY_SECRET=your_access_key_secret
Send SMS
use uni_sdk::{SendMessageRequest, UniClient};
#[tokio::main]
async fn main() -> uni_sdk::Result<()> {
let client = UniClient::from_env()?;
let response = client.messages().send(&SendMessageRequest::text(
"+1206880xxxx", // in E.164 format
"Your verification code is 2048.",
)).await?;
println!("{:#?}", response.data);
Ok(())
}
or use the blocking client:
use uni_sdk::{blocking::UniClient, SendMessageRequest};
fn main() -> uni_sdk::Result<()> {
let client = UniClient::from_env()?;
let response = client.messages().send(&SendMessageRequest::text(
"+1206880xxxx",
"Your verification code is 2048.",
))?;
println!("{:#?}", response.data);
Ok(())
}
Send OTP
use uni_sdk::{SendOtpRequest, UniClient};
#[tokio::main]
async fn main() -> uni_sdk::Result<()> {
let client = UniClient::from_env()?;
let response = client.otp().send(&SendOtpRequest::new("+1206880xxxx")).await?;
println!("{:#?}", response.data);
Ok(())
}
Verify OTP
use uni_sdk::{UniClient, VerifyOtpRequest};
#[tokio::main]
async fn main() -> uni_sdk::Result<()> {
let client = UniClient::from_env()?;
let response = client.otp().verify(&VerifyOtpRequest::new(
"+1206880xxxx",
"123456", // the code the user provided
)).await?;
println!("{}", response.into_data()?.valid);
Ok(())
}