Sign in →
1 min read

Java / Spring SDK

Spring Boot auto-configuration and servlet filter for Java metering.

Updated 2026-07-29Suggest edits
Get the Java SDK
The Java SDK lives in the public repo github.com/aforoai/SDKs under aforo-metering-sdks/java.
View on GitHub Download ZIPgit clone https://github.com/aforoai/SDKs.git

Install & Quickstart#

Add Aforo metering to your Java service. Spring Boot auto-configuration and a manual TrackEvent client.

Requires Java 17+ (uses java.net.http.HttpClient and records). The artifact is com.aforo:metering.

pom.xml
<dependency>
  <groupId>com.aforo</groupId>
  <artifactId>metering</artifactId>
  <version>1.0.0</version>
</dependency>
terminal
# Not yet on Maven Central — build from source into your local ~/.m2:
git clone https://github.com/aforoai/SDKs.git
cd SDKs/aforo-metering-sdks/java
mvn clean install

Manual Tracking

Usage.java
import com.aforo.metering.AforoClient;
import com.aforo.metering.AforoOptions;
import com.aforo.metering.TrackEvent;

// AforoClient is AutoCloseable — the buffer flushes on close
try (AforoClient client = new AforoClient(new AforoOptions(System.getenv("AFORO_API_KEY")))) {
    client.track(TrackEvent.builder("cust_acme_001", "api_calls")
            .quantity(1)
            .metadata(java.util.Map.of("route", "POST /v1/charges"))
            .build());
}

Spring Boot Auto-Configuration

application.yml
aforo:
  enabled: true          # auto-config is off unless this is exactly "true"
  api-key: ${AFORO_API_KEY}
  base-url: https://ingest.aforo.ai

The auto-configuration wires an AforoClient bean and a request-end servlet filter that runs after the response is committed (no added latency). It resolves the customer from the Spring Security principal, then X-Customer-Id, then X-Api-Key; requests with none of those are skipped.

See also