Skip to content

spotify/confidence-sdk-java

Java Confidence SDK

Java library for Confidence. This library is designed to work in a java backend environment. For Android, please visit Confidence-SDK-Android.

Install

Maven

<dependency>
    <groupId>com.spotify.confidence</groupId>
    <artifactId>sdk-java</artifactId>
    <version>0.1.3-SNAPSHOT</version>
</dependency>

Note: we strongly recommend to adopt the latest non-SNAPSHOT release available here.

Depending on a development snapshot

We deploy snapshots from the main branch to Sonatype OSSRH. To use a snapshot, add the following repository to your pom.xml:

<distributionManagement>
    <snapshotRepository>
        <id>oss.snapshots</id>
        <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
    </snapshotRepository>
</distributionManagement>

Usage

The SDK is instantiated using a client secret that is configured in the Confidence UI or via the management console.

Resolving flags

Flag values are evaluated remotely and returned to the application:

final Confidence confidence = Confidence.builder("<CLIENT_TOKEN>").build();
confidence.setContext(Map.of("country", ConfidenceValue.of("SE")));
final String propertyValue =
    confidence
        .withContext(
            Map.of(
                "user_id", ConfidenceValue.of("<some-user-id>"),
                "country", ConfidenceValue.of("SE")))
        .getValue("flag-name.property-name", "defaultValue");

Tracking events

Events are emitted to the Confidence backend:

confidence.track("my-event", ConfidenceValue.of(Map.of("field", ConfidenceValue.of("data"))));

OpenFeature

The library includes a Provider for the OpenFeature Java SDK, that can be used to resolve feature flag values from the Confidence platform.

To learn more about the basic concepts (flags, targeting key, evaluation contexts), the OpenFeature reference documentation can be useful.