Skip to main content

Sirv REST API - Java examples

Use the Sirv REST API to perform 40+ tasks on your Sirv account.

For convenience, we provide a Java client, with examples of common actions including connecting to Sirv, file uploading, search, folder query, video conversion and remote fetching.

Connect to Sirv with Java​

Each API call must be authenticated with a bearer token (JSON Web Token). Here's how to get a token with a POST request:

  1. Download the Sirv Java Samples (zip).

  2. Unzip the file.

  3. Open the samples in any IDE (e.g. IDEA, Eclipse) as a maven project.

  4. Create an API client via the API settings page of your Sirv account.

  5. In your IDE, replace the references for CLIENT_ID and CLIENT_SECRET with the client ID and client secret for your API client:

    package com.sirv.examples;

import com.sirv.examples.dto.TokenResponseDto;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class Main {
private static final String CLIENT_ID = System.getenv("CLIENT_ID");
private static final String CLIENT_SECRET = System.getenv("CLIENT_SECRET");

public static void main(String[] args) throws IOException {
// create Sirv client
SirvClient sirvClient = new SirvClient(CLIENT_ID, CLIENT_SECRET);

// get token
TokenResponseDto token = sirvClient.getToken();
}
}

Now use your token to perform any of the Sirv REST API methods. Java code samples of some common actions are below.

Get folder contents​

// Read folder content
sirvClient.readFolderContents("/", null, token.getToken());

Search files​

// Search files
Map < String, String > sort = new HashMap < String, String > ();
sort.put("filename.raw", "asc");
sirvClient.searchFiles("filename:sirv.*", sort, 0, 10, token.getToken());

Upload file​

// Upload files
sirvClient.uploadFile(new File("./example.png"), "/path/to/uploaded-image.png", token.getToken());
sirvClient.uploadFile(new File("./example.txt"), "/path/to/uploaded-file.txt", token.getToken());
sirvClient.uploadFile(new File("./example.mp4"), "/path/to/uploaded-movie.mp4", token.getToken());

Convert video to spin​

// Convert video to spin
sirvClient.convertVideoToSpin("/example.mp4", 0, 30, 100, token.getToken());

Fetch file via URL​

// Fetch a remote file via URL
Map < String, String > urlToFilenameMap = new HashMap < String, String > ();
urlToFilenameMap.put("https://demo.sirv.com/example.jpg", "/path/to/uploaded-file.jpg");
sirvClient.fetchUrls(urlToFilenameMap, true, token.getToken());