Quickstart with Java
Call your PromptHelm prompts from any JVM application.
This quickstart walks you through minting an API token, adding the
PromptHelm Java SDK to a Maven or Gradle project, and making your
first call from Java. By the end you will have a working completion,
an Iterator-based streaming example, and a checklist for
production deployment.
SDK status
The Java SDK is in pre-release. The 0.1.0 artifact is being
staged on Maven Central; track
Runivox/prompt-helm-sdk-java
for the release announcement. The public API below is stable.
Prerequisites
- Java 17 LTS or newer (records and pattern matching used in examples).
- Maven 3.9+ or Gradle 8.2+.
- A PromptHelm account. Join the waitlist if you need an invite.
Sign in to the dashboard and open Settings → API tokens. Click New token, name it (e.g.
backend-service-dev), and copy the value immediately — tokens are revealed exactly once.Export the token in your shell or load it from your secrets manager at startup:
.env PROMPTHELM_API_KEY=ph_live_your_token_hereAdd the dependency to your build file. The artifact is published to Maven Central, so no extra repository configuration is required.
pom.xml <dependency> <groupId>app.prompthelm</groupId> <artifactId>sdk-java</artifactId> <version>0.1.0</version> </dependency>In the dashboard, navigate to Prompts → New prompt. Give it a slug (for example,
welcome), pick a default model, and use{{ variable_name }}syntax for runtime variables. Saving publishesv1on themainenvironment.Learn more
See Concepts → Prompts for versions, environments, and promotion semantics.
Build the client once at startup using the fluent builder. The instance is thread-safe and meant to be reused across requests.
src/main/java/app/Quickstart.java import app.prompthelm.sdk.*; PromptHelm ph = PromptHelm.builder() .apiKey(System.getenv("PROMPTHELM_API_KEY")) .build(); ExecuteResponse response = ph.execute(new ExecuteRequest( "welcome", // promptSlug null, // promptId Map.of("name", "World"), // variables null, null, null, null, null, null, null, null, null )); System.out.println(response.output());The call appears in the dashboard's Logs view with the full request/response payload, the per-call cost, and the round-trip latency.
For server-side streaming endpoints, the SDK exposes a blocking
Iterator<StreamEvent>. Eachnext()call yields a chunk, a terminalDoneEvent, or anErrorEvent.src/main/java/app/Stream.java ExecuteRequest req = new ExecuteRequest( "welcome", null, Map.of("name", "World"), null, null, null, null, null, null, null, null, null ); Iterator<StreamEvent> stream = ph.stream(req); while (stream.hasNext()) { StreamEvent event = stream.next(); if (event instanceof ChunkEvent c) { System.out.print(c.content()); } else if (event instanceof DoneEvent d) { System.out.printf("%n%d tokens, $%.4f%n", d.totalTokens(), d.cost()); } else if (event instanceof ErrorEvent e) { System.err.printf("Error %s: %s%n", e.errorCode(), e.message()); } }Closing the iterator (or the enclosing
try-with-resourceson the client) closes the underlying SSE connection cleanly.Before you point real traffic at PromptHelm, run through this checklist:
- Construct
PromptHelmonce per process. It owns an HTTP client pool — sharing the instance across requests avoids connection churn. Wrap it intry-with-resources(it implementsAutoCloseable) during graceful shutdown. - Source the API key from a secrets manager. Inject it via
System.getenvfor plain Java apps, or@Value("${prompthelm.api-key}")for Spring Boot. - Tune retries and timeouts on the builder. The default policy retries idempotent failures with jittered backoff; raise the timeout past 15 s only when calling models with long thinking budgets.
- Catch typed errors. Every SDK call throws
PromptHelmExceptionwith a stableerrorCode(). Map well-known codes to retries or user-facing messages. - Pin an environment. Default
mainto production traffic and promote new prompt versions throughstaginganddevfirst. - Run on Java 17 LTS or newer. Earlier versions are not supported.
- Construct