REST API Java Examples
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.
Connect to Sirv with Java
Section titled “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:
-
Download the Sirv Java Samples (zip).
-
Unzip the file.
-
Open the samples in any IDE (e.g. IDEA, Eclipse) as a maven project.
-
Create an API client via the API settings page of your Sirv account.
-
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
Section titled “Get folder contents”- Endpoint: https://api.sirv.com/v2/files/readdir
- Documentation: Read folder contents
// Read folder contentsirvClient.readFolderContents("/", null, token.getToken());Search files
Section titled “Search files”- Endpoint: https://api.sirv.com/v2/files/search
- Documentation: Search files
// Search filesMap < String, String > sort = new HashMap < String, String > ();sort.put("filename.raw", "asc");sirvClient.searchFiles("filename:sirv.*", sort, 0, 10, token.getToken());Upload file
Section titled “Upload file”- Endpoint: https://api.sirv.com/v2/files/upload
- Documentation: Upload file
// Upload filessirvClient.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
Section titled “Convert video to spin”- Endpoint: https://api.sirv.com/v2/files/video2spin
- Documentation: Convert video to spin
// Convert video to spinsirvClient.convertVideoToSpin("/example.mp4", 0, 30, 100, token.getToken());Fetch file via URL
Section titled “Fetch file via URL”- Endpoint: https://api.sirv.com/v2/files/fetch
- Documentation: Fetch URL
// Fetch a remote file via URLMap < 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.
