# REST API Java Examples

Use the Sirv [REST API](/help/rest-api/sirv-rest-api/) to perform more than 60 tasks on your Sirv account.

Using an AI coding agent?

Install the [`sirv-api`](https://www.skills.sh/sirv/image-optimization-skill/sirv-api) skill, and your agent can call the Sirv API for you. Ask in plain words, such as “Upload the photos in /shoots/spring and tag them by product.” [See all 8 skills](/help/rest-api/sirv-skills-for-ai-agents/)

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](https://sirv.com/download/Sirv-REST-API-Console-Java.zip) (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](https://app.sirv.com/settings/api) 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:
    

```java
    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](/help/rest-api/reference/). Java code samples of some common actions are below.

## Get folder contents

-   Endpoint: `https://api.sirv.com/v2/files/readdir`
-   Documentation: [Read folder contents](/help/rest-api/reference/files/read-folder/)

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

## Search files

-   Endpoint: `https://api.sirv.com/v2/files/search`
-   Documentation: [Search files](/help/rest-api/reference/files/search-files/#search-files)

```java
// 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: [Upload file](/help/rest-api/reference/upload-download/upload-file/)

```java
// 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: [Convert video to spin](/help/rest-api/reference/conversions/video-to-spin/)

```java
// 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: [Fetch URL](/help/rest-api/reference/upload-download/fetch-url/)

```java
// 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.
