Skip to content

REST API Java Examples

By , Customer Success DirectorUpdated

Open this page in a new chat

Short answer

Download the Sirv Java samples, add your API client ID and secret, and request a bearer token with a POST request. Every other call sends that token. The samples show how to read folder contents, search files, upload a file, convert a video to a spin and fetch a file from a URL.

After reading this you will know

  • How to connect to the Sirv REST API with Java
  • How to upload, search and list files with Java
  • How to convert a video to a spin and fetch remote files

Use the Sirv REST API to perform more than 60 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.

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.

// Read folder content
sirvClient.readFolderContents("/", null, token.getToken());
// 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 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
sirvClient.convertVideoToSpin("/example.mp4", 0, 30, 100, token.getToken());
// 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());

Sirv downloads the file from the URL and saves it to the path you chose in your account.

Back to top