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:
-
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
- Endpoint: https://api.sirv.com/v2/files/readdir
- Documentation: https://apidocs.sirv.com/#read-folder-contents
// Read folder content sirvClient.readFolderContents("/", null, token.getToken());
Search files
- Endpoint: https://api.sirv.com/v2/files/search
- Documentation: https://apidocs.sirv.com/#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
- Endpoint: https://api.sirv.com/v2/files/upload
- Documentation: https://apidocs.sirv.com/#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
- Endpoint: https://api.sirv.com/v2/files/video2spin
- Documentation: https://apidocs.sirv.com/#convert-video-to-spin
// Convert video to spin sirvClient.convertVideoToSpin("/example.mp4", 0, 30, 100, token.getToken());
Fetch file via URL
- Endpoint: https://api.sirv.com/v2/files/fetch
- Documentation: https://apidocs.sirv.com/#fetch-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());