<span aria-hidden="true" id="java-sdk-"></span>

# Java SDK

[transloadit/java-sdk⁠](https://github.com/transloadit/java-sdk)

<span aria-hidden="true" id="user-content-java-sdk-install"></span>

<span aria-hidden="true" id="install"></span>

## Install

The JARs can be downloaded manually from [Maven Central Search⁠](https://search.maven.org/artifact/com.transloadit.sdk/transloadit), or can be installed from the Maven repository.

Existing users should take note of the [JCenter shutdown⁠](https://jfrog.com/blog/into-the-sunset-bintray-jcenter-gocenter-and-chartcenter/) and update their build files to receive the most recent versions.

The examples below target Java SDK `2.2.4`, including its external signature provider API.

**Gradle:**

```groovy
implementation 'com.transloadit.sdk:transloadit:2.2.4'

```

**Maven:**

```xml
<dependency>
  <groupId>com.transloadit.sdk</groupId>
  <artifactId>transloadit</artifactId>
  <version>2.2.4</version>
</dependency>

```

<span aria-hidden="true" id="user-content-java-sdk-usage"></span>

<span aria-hidden="true" id="usage"></span>

## Usage

All interactions with the SDK begin with the `com.transloadit.sdk.Transloadit` class.

The complete `Main` examples below run on a trusted server using server-held credentials. Replace the credential, file, Assembly, and Template placeholders with your own server configuration. Keep account and Template administration on your backend. For uploads from mobile or desktop apps distributed to users, use the external signature provider described below.

<span aria-hidden="true" id="authentication"></span>

### Authentication

The SDK supports two authentication methods:

<span aria-hidden="true" id="1-api-key--secret"></span>

#### 1. API key + secret

On a trusted server, the SDK can sign requests using your Auth Key and Auth Secret. Load real credentials from your server’s secret store; never include the Auth Secret in a distributed app or commit it to source control. Server-side initialization:

```java
Transloadit transloadit = new Transloadit("YOUR_TRANSLOADIT_KEY", "YOUR_TRANSLOADIT_SECRET");

```

<span aria-hidden="true" id="2-external-signature-provider"></span>

#### 2. External signature provider

For client uploads, keep the Auth Secret on your backend and require [Signature Authentication](/docs/api/authentication.md) for the Workspace or Template used by the app. See the [Android SDK guide](/docs/sdks/android-sdk.md) for the application/backend integration.

`SignatureProvider.generateSignature(String paramsJson)` runs synchronously. Your implementation must obtain a signature for the exact UTF-8 bytes of `paramsJson` from your authenticated HTTPS backend, with a finite network timeout. Throw if authorization, transport, or response validation fails, and run SDK requests off the UI thread. The backend must authenticate the user and authorize the upload’s Steps or Template, destinations, limits, nonce, and expiry before signing. It must not sign arbitrary client-supplied params. For a server-owned Template, disable `allow_steps_override`.

The constructor below requests an expiry five minutes in the future; the backend must validate it against its own clock. Return only the approved HMAC-SHA384 signature (`sha384:` followed by 96 hexadecimal digits), never the Auth Secret. Do not reserialize the params before signing or reuse a signature for different params.

Save this helper as `SignedClient.java` and supply your application’s `SignatureProvider`:

```java
import com.transloadit.sdk.SignatureProvider;
import com.transloadit.sdk.Transloadit;

public final class SignedClient {
  private SignedClient() {}

  public static Transloadit create(String authKey, SignatureProvider approvedSignatures) {
    return new Transloadit(authKey, approvedSignatures);
  }
}

```

<span aria-hidden="true" id="user-content-java-sdk-create-an-assembly"></span>

<span aria-hidden="true" id="create-an-assembly"></span>

### Create an Assembly

To create an assembly, you use the `newAssembly` method.

```java
import com.transloadit.sdk.Assembly;
import com.transloadit.sdk.Transloadit;
import com.transloadit.sdk.exceptions.LocalOperationException;
import com.transloadit.sdk.exceptions.RequestException;
import com.transloadit.sdk.response.AssemblyResponse;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class Main {

  public static void main(String[] args) {
    Transloadit transloadit = new Transloadit("YOUR_TRANSLOADIT_KEY", "YOUR_TRANSLOADIT_SECRET");

    Assembly assembly = transloadit.newAssembly();

    Map<String, Object> stepOptions = new HashMap<>();
    stepOptions.put("width", 75);
    stepOptions.put("height", 75);
    assembly.addStep("resize", "/image/resize", stepOptions);

    assembly.addFile(new File("PATH/TO/FILE.jpg"));
    // you can skip this part if you don't want to wait till the
    // assembly is complete
    assembly.setShouldWaitForCompletion(true);
    try {
      AssemblyResponse response = assembly.save();

      System.out.println(response.getId());
      System.out.println(response.getUrl());
      System.out.println(response.json());
    } catch (RequestException | LocalOperationException e) {
      // handle exception here
    }
  }
}


```

<span aria-hidden="true" id="user-content-java-sdk-get-an-assembly"></span>

<span aria-hidden="true" id="get-an-assembly"></span>

### Get an Assembly

The method, `getAssembly`, retrieves the JSON status of an assembly identified by the given `assembly_Id`.

```java
import com.transloadit.sdk.Transloadit;
import com.transloadit.sdk.exceptions.LocalOperationException;
import com.transloadit.sdk.exceptions.RequestException;
import com.transloadit.sdk.response.AssemblyResponse;

public class Main {

  public static void main(String[] args) {
    Transloadit transloadit = new Transloadit("YOUR_TRANSLOADIT_KEY", "YOUR_TRANSLOADIT_SECRET");

    try {
      AssemblyResponse response = transloadit.getAssembly("ASSEMBLY_ID");

      System.out.println(response.getUrl());
      System.out.println(response.json());
    } catch (RequestException | LocalOperationException e) {
      // handle exception here
    }
  }
}


```

You may also get an assembly by url with the `getAssemblyByUrl` method.

```java
    AssemblyResponse response = transloadit.getAssemblyByUrl("ASSEMBLY_URL");

```

<span aria-hidden="true" id="user-content-java-sdk-cancel-an-assembly"></span>

<span aria-hidden="true" id="cancel-an-assembly"></span>

### Cancel an Assembly

To cancel an executing assembly, use the `cancelAssembly` method with the full HTTPS Assembly URL from `AssemblyResponse.getSslUrl()` (the `assembly_ssl_url` response field).

```java
import com.transloadit.sdk.Transloadit;
import com.transloadit.sdk.exceptions.LocalOperationException;
import com.transloadit.sdk.exceptions.RequestException;
import com.transloadit.sdk.response.AssemblyResponse;

public class Main {

  public static void main(String[] args) {
    Transloadit transloadit = new Transloadit("YOUR_TRANSLOADIT_KEY", "YOUR_TRANSLOADIT_SECRET");

    try {
      AssemblyResponse response = transloadit.cancelAssembly("ASSEMBLY_URL");

      System.out.println(response.isCanceled()); // true when cancellation succeeded
    } catch (RequestException | LocalOperationException e) {
      // handle exception here
    }
  }
}


```

<span aria-hidden="true" id="user-content-java-sdk-list-assemblies"></span>

<span aria-hidden="true" id="list-assemblies"></span>

### List Assemblies

The method, `listAssemblies`, retrieves an array of assemblies according to the given `options`. Valid `options` can be `page`, `pagesize`, `type`, `fromdate` and `todate`. Please consult the [Transloadit API docs](/docs/api/assemblies-get.md) for details.

```java
import com.transloadit.sdk.Transloadit;
import com.transloadit.sdk.exceptions.LocalOperationException;
import com.transloadit.sdk.exceptions.RequestException;
import com.transloadit.sdk.response.ListResponse;
import java.util.HashMap;
import java.util.Map;

public class Main {

  public static void main(String[] args) {
    Transloadit transloadit = new Transloadit("YOUR_TRANSLOADIT_KEY", "YOUR_TRANSLOADIT_SECRET");

    try {
      Map<String, Object> options = new HashMap<>();
      options.put("pagesize", 10);
      ListResponse response = transloadit.listAssemblies(options);

      System.out.println(response.size()); // number of assemblies on the list.
      System.out.println(response.getItems()); // returns an iterable json array
    } catch (RequestException | LocalOperationException e) {
      // handle exception here
    }
  }
}


```

<span aria-hidden="true" id="user-content-java-sdk-create-a-template"></span>

<span aria-hidden="true" id="create-a-template"></span>

### Create a Template

To create a new template, you use the `newTemplate` method, passing the template name as a parameter. Step names must be unique: this example resizes and optimizes the original image in separate Steps.

```java
import com.transloadit.sdk.Template;
import com.transloadit.sdk.Transloadit;
import com.transloadit.sdk.exceptions.LocalOperationException;
import com.transloadit.sdk.exceptions.RequestException;
import com.transloadit.sdk.response.Response;
import java.util.HashMap;
import java.util.Map;

public class Main {

  public static void main(String[] args) {
    Transloadit transloadit = new Transloadit("YOUR_TRANSLOADIT_KEY", "YOUR_TRANSLOADIT_SECRET");

    Template template = transloadit.newTemplate("MY_TEMPLATE_NAME");

    Map<String, Object> resizeOptions = new HashMap<>();
    resizeOptions.put("width", 75);
    resizeOptions.put("height", 75);

    Map<String, Object> optimizeOptions = new HashMap<>();
    optimizeOptions.put("use", ":original");

    template.addStep("resize", "/image/resize", resizeOptions);
    template.addStep("optimize", "/image/optimize", optimizeOptions);

    try {
      Response response = template.save();

      System.out.println(response.json());
      System.out.println(response.json().getString("id")); // gets the Template ID.
    } catch (RequestException | LocalOperationException e) {
      // handle exception here
    }
  }
}


```

<span aria-hidden="true" id="user-content-java-sdk-update-a-template"></span>

<span aria-hidden="true" id="update-a-template"></span>

### Update a Template

To update a template, use the `updateTemplate` method with the Template ID and options to update. Pass Instructions as JSON in the `template` option, alongside the new `name`; see the [Template update API](/docs/api/templates-template-id-put.md).

```java
import com.transloadit.sdk.Steps;
import com.transloadit.sdk.Transloadit;
import com.transloadit.sdk.exceptions.LocalOperationException;
import com.transloadit.sdk.exceptions.RequestException;
import com.transloadit.sdk.response.Response;
import java.util.HashMap;
import java.util.Map;
import org.json.JSONObject;

public class Main {

  public static void main(String[] args) {
    Transloadit transloadit = new Transloadit("YOUR_TRANSLOADIT_KEY", "YOUR_TRANSLOADIT_SECRET");

    Steps steps = new Steps();

    Map<String, Object> stepOptions = new HashMap<>();
    stepOptions.put("width", 150);
    stepOptions.put("height", 150);
    steps.addStep("resize", "/image/resize", stepOptions);

    Map<String, Object> instructions = new HashMap<>();
    instructions.put("steps", steps.toMap());

    Map<String, Object> templateOptions = new HashMap<>();
    templateOptions.put("template", new JSONObject(instructions).toString());
    templateOptions.put("name", "MY_NEW_TEMPLATE_NAME");

    try {
      Response response = transloadit.updateTemplate("TEMPLATE_ID", templateOptions);
    } catch (RequestException | LocalOperationException e) {
      // handle exception here
    }
  }
}


```

<span aria-hidden="true" id="user-content-java-sdk-delete-a-template"></span>

<span aria-hidden="true" id="delete-a-template"></span>

### Delete a Template

To delete a template, you use the `deleteTemplate` method, passing the Template ID as a parameter.

```java
import com.transloadit.sdk.Transloadit;
import com.transloadit.sdk.exceptions.LocalOperationException;
import com.transloadit.sdk.exceptions.RequestException;
import com.transloadit.sdk.response.Response;
import java.util.HashMap;

public class Main {

  public static void main(String[] args) {
    Transloadit transloadit = new Transloadit("YOUR_TRANSLOADIT_KEY", "YOUR_TRANSLOADIT_SECRET");

    try {
      Response response = transloadit.deleteTemplate("TEMPLATE_ID");
    } catch (RequestException | LocalOperationException e) {
      // handle exception here
    }
  }
}


```

<span aria-hidden="true" id="user-content-java-sdk-list-templates"></span>

<span aria-hidden="true" id="list-templates"></span>

### List Templates

To get a list of all templates under your account, you use the `listTemplates` method.

```java
import com.transloadit.sdk.Transloadit;
import com.transloadit.sdk.exceptions.LocalOperationException;
import com.transloadit.sdk.exceptions.RequestException;
import com.transloadit.sdk.response.ListResponse;

public class Main {

  public static void main(String[] args) {
    Transloadit transloadit = new Transloadit("YOUR_TRANSLOADIT_KEY", "YOUR_TRANSLOADIT_SECRET");

    try {
      ListResponse response = transloadit.listTemplates();

      System.out.println(response.size()); // number of assemblies on the list.
      System.out.println(response.getItems()); // returns an iterable json array
    } catch (RequestException | LocalOperationException e) {
      // handle exception here
    }
  }
}


```

<span aria-hidden="true" id="user-content-java-sdk-get-a-template"></span>

<span aria-hidden="true" id="get-a-template"></span>

### Get a template

To get a particular template, you use the `getTemplate` method, passing the Template ID as a parameter.

```java
import com.transloadit.sdk.Transloadit;
import com.transloadit.sdk.exceptions.LocalOperationException;
import com.transloadit.sdk.exceptions.RequestException;
import com.transloadit.sdk.response.Response;

public class Main {

  public static void main(String[] args) {
    Transloadit transloadit = new Transloadit("YOUR_TRANSLOADIT_KEY", "YOUR_TRANSLOADIT_SECRET");

    try {
      Response response = transloadit.getTemplate("TEMPLATE_ID");
      System.out.println(response.json());
    } catch (RequestException | LocalOperationException e) {
      // handle exception here
    }
  }
}


```

<span aria-hidden="true" id="user-content-java-sdk-get-bill"></span>

<span aria-hidden="true" id="get-bill"></span>

### Get bill

To get your account billing details for a particular month, you use the `getBill` method, passing the month and year as parameters.

```java
import com.transloadit.sdk.Transloadit;
import com.transloadit.sdk.exceptions.LocalOperationException;
import com.transloadit.sdk.exceptions.RequestException;
import com.transloadit.sdk.response.Response;

public class Main {

  public static void main(String[] args) {
    Transloadit transloadit = new Transloadit("YOUR_TRANSLOADIT_KEY", "YOUR_TRANSLOADIT_SECRET");

    try {
      Response response = transloadit.getBill(3, 2017);

      System.out.println(response.json());
    } catch (RequestException | LocalOperationException e) {
      // handle exception here
    }
  }
}


```

<span aria-hidden="true" id="user-content-java-sdk-example"></span>

<span aria-hidden="true" id="example"></span>

## Example

For fully working examples take a look at [/examples⁠](https://github.com/transloadit/java-sdk/tree/main/examples).

<span aria-hidden="true" id="user-content-java-sdk-documentation"></span>

<span aria-hidden="true" id="documentation"></span>

## Documentation

See [Javadoc⁠](https://javadoc.io/doc/com.transloadit.sdk/transloadit) for full API documentation.
