Java SDK

Installation

The recommended way to install the Unimatrix SDK for Java is with a dependency management tool such as Maven or Gradle, which is available on Maven Central.

Maven users

Add this dependency to your project's pom.xml file:

<dependency>
    <groupId>com.unimtx</groupId>
    <artifactId>uni-sdk</artifactId>
    <version>0.3.0</version>
</dependency>

Gradle users

Add this dependency to your project's build.gradle file:

implementation "com.unimtx:uni-sdk:0.3.0"

Usage

The following example shows how to use the Unimatrix Java SDK to interact with Unimatrix services.

Initialization

import com.unimtx.Uni;

public class Example {
    private static String ACCESS_KEY_ID = "your access key id";
    private static String ACCESS_KEY_SECRET = "your access key secret";

    public static void main(String[] args) {
        Uni.init(ACCESS_KEY_ID, ACCESS_KEY_SECRET); // if using simple auth mode, just pass in the first parameter
    }
}

or you can configure your credentials by environment variables:

export UNIMTX_ACCESS_KEY_ID=your_access_key_id
export UNIMTX_ACCESS_KEY_SECRET=your_access_key_secret

Send SMS

Send a text message to a single recipient.

import com.unimtx.Uni;
import com.unimtx.UniException;
import com.unimtx.UniResponse;
import com.unimtx.model.UniMessage;

class Example {
    public static void main(String[] args) {
        Uni.init();

        UniMessage message = UniMessage.build()
            .setTo("+1206880xxxx") // in E.164 format
            .setText("Your verification code is 2048.");

        try {
            UniResponse res = message.send();
            System.out.println(res.data);
        } catch (UniException e) {
            System.out.println("Error: " + e);
            System.out.println("RequestId: " + e.requestId);
        }
    }
}

Send a message using a template with variables.

Map<String, String> templateData = new HashMap<String, String>();
templateData.put("code", "2048");

UniMessage.build()
    .setTo("+1206880xxxx")
    .setSignature("Unimatrix")
    .setTemplateId("pub_verif_en_basic2")
    .setTemplateData(templateData)
    .send();

Send OTP

Send a one-time passcode (OTP) to a recipient. The following example will send a automatically generated verification code to the user.

import com.unimtx.Uni;
import com.unimtx.UniResponse;
import com.unimtx.model.UniOtp;

class Example {
    public static void main(String[] args) {
        Uni.init();

        UniResponse res = UniOtp.build()
            .setTo("+1206880xxxx")
            .send();

        System.out.println(res.data);
    }
}

Verify OTP

Verify the one-time passcode (OTP) that a user provided. The following example will check whether the user-provided verification code is correct.

import com.unimtx.Uni;
import com.unimtx.UniResponse;
import com.unimtx.model.UniOtp;

class Example {
    public static void main(String[] args) {
        Uni.init();

        UniResponse res = UniOtp.build()
            .setTo("+1206880xxxx")
            .setCode("123456") // the code user provided
            .verify();

        System.out.println(res.valid);
    }
}