Beta

Java SDK

Install stigmer-sdk and call the Stigmer API from Java services.

stigmer-sdk is the official Java client for the Stigmer API. It provides typed resource clients, structured error handling, and server-streaming support over gRPC.

Installation

Add the dependency to your pom.xml:

<dependency>
  <groupId>ai.stigmer</groupId>
  <artifactId>stigmer-sdk</artifactId>
</dependency>

Create a client

Authenticate with an API key from an environment variable. StigmerClient implements AutoCloseable — use try-with-resources so the gRPC channel shuts down cleanly:

import ai.stigmer.sdk.StigmerClient;

try (var client = StigmerClient.builder(
        System.getenv("STIGMER_API_KEY")
    ).build()) {
    // use client
}

In a CI job or a script, use a service account's key, not a person's: see SDK Overview > Automation.

The client connects to api.stigmer.ai:443 by default; override with .baseUrl(target). See SDK Overview > Authentication for authentication methods and SDK Overview > Configuration for all options.

Run an agent

This program connects to the Stigmer running on your machine, sets up a small agent, asks it one thing and prints its answer. Start Stigmer first with stigmer up and give it a model key (Start Stigmer and add your model key); without a key the server refuses the message, and the program prints why.

// The Stigmer quick start: connect to the Stigmer running on this machine,
// set up a small agent, ask it one thing and print its answer.
//
// Start Stigmer first with `stigmer up` (given a model key), then run this
// file with the SDK on the classpath: java -cp <classpath> Quickstart.java

import ai.stigmer.agentic.agent.v1.Agent;
import ai.stigmer.sdk.SendAndWaitInput;
import ai.stigmer.sdk.StigmerClient;
import ai.stigmer.sdk.TurnOutcome;
import ai.stigmer.sdk.gen.AgentInput;
import ai.stigmer.sdk.gen.ResourceRef;
import ai.stigmer.sdk.gen.RunInput;
import ai.stigmer.sdk.gen.StigmerException;

public class Quickstart {
    public static void main(String[] args) {
        // `stigmer up` serves on localhost:7234 and needs no key on your machine.
        try (StigmerClient client = StigmerClient.builder().baseUrl("localhost:7234").insecure().build()) {
            // apply creates the agent, or updates it when you run this again.
            Agent agent = client.agent.apply(AgentInput.builder()
                    .name("greeter")
                    .org("stigmer")
                    .instructions("You greet people in one short, friendly sentence.")
                    .build());

            TurnOutcome turn = client.sendAndWait(SendAndWaitInput.builder()
                    .org("stigmer")
                    .sessionSpec(RunInput.SessionSpecInput.builder()
                            .agentRef(ResourceRef.of("stigmer", agent.getMetadata().getSlug()))
                            .build())
                    .message("Say hello to a developer trying Stigmer for the first time.")
                    .build());
            if (!turn.phase().equals("completed")) {
                System.err.println("the turn did not complete (" + turn.phase() + "): " + turn.error());
                System.exit(1);
            }
            System.out.println(turn.answer());
        } catch (StigmerException e) {
            System.err.println("stigmer: " + e.getMessage());
            System.exit(1);
        }
    }
}

sendAndWait sends the message and waits for the agent's answer, reconnecting if the stream drops. A turn that waits for a person's approval returns with phase TurnOutcome.WAITING and its asks; answer them with client.session.confirmToolUse, then call client.waitForTurn with the outcome's lastSeq.

On Stigmer Cloud, connect with StigmerClient.builder(System.getenv("STIGMER_API_KEY")).build() (keys read stk_…), and use your own organization where the program says "stigmer".

Make your first call

Every resource has its own client with methods like get, create, list, apply, update, rename, and delete (Update and apply). Java uses singular camelCase fields on the client:

import ai.stigmer.sdk.gen.StigmerException;

try {
    var agent = client.agent.get(id);
    System.out.println(agent.getMetadata().getName());
} catch (StigmerException e) {
    if (e.isNotFound()) {
        System.out.println("Agent does not exist");
    } else {
        throw e;
    }
}

Act as one of your users

When your backend starts work that belongs to a person signed in to your product, build a second client on a token minted for them, kept in a token cache. What that client starts (a Session, a run, an upload) is the person's. The service account's client mints; the cache mints again only shortly before the token expires.

import ai.stigmer.sdk.MintUserTokenInput;
import ai.stigmer.sdk.StigmerClient;
import ai.stigmer.sdk.TokenCache;

TokenCache tokens = TokenCache.of(() -> admin.tokens().mintUserToken(
    MintUserTokenInput.builder().org("acme").userId(userId).build()));
StigmerClient asUser = StigmerClient.builder().tokenProvider(tokens).build();

admin is the client built on the service account's key.

The whole flow, and what the person's role allows, is in From your backend, as the user.

What's next