uni-sdk 0.3.0

The official Unimatrix SDK for Rust
Documentation
# Unimatrix Rust SDK

[![Crates.io](https://img.shields.io/crates/v/uni-sdk.svg)](https://crates.io/crates/uni-sdk) [![Documentation](https://docs.rs/uni-sdk/badge.svg)](https://docs.rs/uni-sdk) [![Release](https://img.shields.io/github/release/unimtx/uni-rust-sdk.svg)](https://github.com/unimtx/uni-rust-sdk/releases/latest) [![GitHub license](https://img.shields.io/badge/license-MIT-brightgreen.svg)](https://github.com/unimtx/uni-rust-sdk/blob/main/LICENSE)

The Unimatrix Rust SDK provides convenient access to integrate communication capabilities into your Rust applications using the Unimatrix HTTP API. The SDK provides support for sending SMS and 2FA verification.

## Getting started

Before you begin, you need an [Unimatrix](https://www.unimtx.com/) account. If you don't have one yet, you can [sign up](https://www.unimtx.com/signup?s=rust.sdk.gh) for an Unimatrix account and get free credits to get you started.

## Documentation

Check out the documentation at [unimtx.com/docs](https://www.unimtx.com/docs) for a quick overview.

## Installation

The SDK uses an asynchronous client by default. Add `uni-sdk` and an async runtime to your `Cargo.toml`:

```toml
[dependencies]
uni-sdk = "0.3"
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
```

To use the synchronous client, enable the `blocking` feature. An async runtime is not required by the application:

```toml
[dependencies]
uni-sdk = { version = "0.3", features = ["blocking"] }
```

The SDK requires Rust 1.86 or later.

## Usage

The following examples show how to use the Unimatrix Rust SDK to interact with Unimatrix services.

### Initialize a client

```rust
use uni_sdk::UniClient;

fn main() -> uni_sdk::Result<()> {
    let client = UniClient::new(
        "your access key id",
        "your access key secret",
    )?;

    Ok(())
}
```

Or configure your credentials using environment variables:

```sh
export UNIMTX_ACCESS_KEY_ID=your_access_key_id
export UNIMTX_ACCESS_KEY_SECRET=your_access_key_secret
```

```rust
use uni_sdk::UniClient;

fn main() -> uni_sdk::Result<()> {
    let client = UniClient::from_env()?;

    Ok(())
}
```

### Send SMS

Send a text message to a single recipient.

```rust
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(())
}
```

### Send verification code

Send a one-time passcode (OTP) to a recipient. The following example sends an automatically generated verification code.

```rust
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(())
}
```

### Check verification code

Verify the one-time passcode that a user provided.

```rust
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(())
}
```

### Handle errors

API errors include the error code, HTTP status, and request ID.

```rust
use uni_sdk::{SendOtpRequest, UniClient};

#[tokio::main]
async fn main() -> uni_sdk::Result<()> {
    let client = UniClient::from_env()?;

    if let Err(error) = client.otp().send(&SendOtpRequest::new("invalid")).await {
        eprintln!("{}", error);
        eprintln!("request ID: {:?}", error.request_id());
    }

    Ok(())
}
```

### Synchronous (non-async) client

Use `uni_sdk::blocking::UniClient` when your application does not use an async runtime. Its API mirrors the async client but does not require `.await`. Avoid calling the blocking client directly from an async task, because it blocks that task's executor thread.

```rust
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(())
}
```

## Reference

### Other Unimatrix SDKs

To find Unimatrix SDKs in other programming languages, check out the list below:

- [Java]https://github.com/unimtx/uni-java-sdk
- [Go]https://github.com/unimtx/uni-go-sdk
- [Node.js]https://github.com/unimtx/uni-node-sdk
- [Python]https://github.com/unimtx/uni-python-sdk
- [PHP]https://github.com/unimtx/uni-php-sdk
- [Ruby]https://github.com/unimtx/uni-ruby-sdk
- [.NET]https://github.com/unimtx/uni-dotnet-sdk

## License

This library is released under the [MIT License](https://github.com/unimtx/uni-rust-sdk/blob/main/LICENSE).