Call Sume video generation from Java 11 with HttpClient

Java 11 HttpClient against Sume /v1/videos: submit a sume/auto job, poll until completed, and see what a default 8-second clip costs. No SDK needed.

5 min readSume
All posts

Java 11 and later ship java.net.http.HttpClient, which is all you need for Sume's asynchronous video API: POST a job, then GET the polling_url until the status leaves pending and in_progress. No SDK or JSON library is required if you only read two fields.

The code

Set SUME_API_KEY. The program submits one job, polls polling_url every 20 seconds until completed, and prints the first download URL.

import java.net.URI;
import java.net.http.*;
import java.util.regex.*;
public class Video {
  static final HttpClient C = HttpClient.newHttpClient();
  static String send(HttpRequest.Builder b) throws Exception {
    var r = b.header("Authorization", "Bearer " + System.getenv("SUME_API_KEY")).build();
    return C.send(r, HttpResponse.BodyHandlers.ofString()).body();
  }
  static String field(String json, String name) {
    Matcher m = Pattern.compile("\"" + name + "\":\\s*\\[?\\s*\"([^\"]+)\"").matcher(json);
    return m.find() ? m.group(1) : "";
  }
  public static void main(String[] a) throws Exception {
    String body = "{\"model\":\"sume/auto\",\"prompt\":\"Lanterns rising over a harbor\",\"aspect_ratio\":\"16:9\"}";
    var post = HttpRequest.newBuilder(URI.create("https://api.sume.com/v1/videos"))
        .header("Content-Type", "application/json")
        .POST(HttpRequest.BodyPublishers.ofString(body));
    String poll = field(send(post), "polling_url");
    String s, out;
    do {
      Thread.sleep(20000);
      out = send(HttpRequest.newBuilder(URI.create(poll)).GET());
      s = field(out, "status");
    } while (s.equals("pending") || s.equals("in_progress"));
    System.out.println(s + " " + field(out, "unsigned_urls"));
  }
}

What to know

sume/auto currently resolves to Gemini Omni Flash 1.1 and takes 3 to 10 seconds in 16:9 or 9:16. The defaults are 720p and 8 seconds, so this call bills $1.00 (list $0.10 per second x 1.25 x 8 s). A failed or cancelled job ends the loop with a non-success status; check error in the poll body.

The regex reads the first string value of a field, which is enough for status and the first entry of unsigned_urls; switch to Jackson or Gson once you need more. Run the file with java Video.java on Java 11 or newer. This sample was written against the documented response shape and not run end to end.

Sources

Related posts

More in Developers

All Developers posts

Written by Sume