> For the complete documentation index, see [llms.txt](https://koople.gitbook.io/koople/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://koople.gitbook.io/koople/sdk-reference/java.md).

# Java

## Getting Started

If still not registered, sign up and create an account at <https://koople.io>.

1\. Install the Koople SDK as a dependency in your application.

{% tabs %}
{% tab title="Maven" %}

```
<dependency>
  <groupId>io.koople</groupId>
  <artifactId>server-sdk-java</artifactId>
  <version>0.1.2</version>
</dependency>
```

{% endtab %}

{% tab title="Gradle" %}

```
implementation 'io.koople:server-sdk-java:0.1.2'
```

{% endtab %}
{% endtabs %}

{% hint style="info" %}
Refer to the [Maven Central Repository](https://search.maven.org/search?q=io.koople) to identify the latest version.
{% endhint %}

2\. Initliaze the SDK. The SDK key authorizes your application to connect to a specific project and environment. The java SDK uses the `SERVER` key.

```java
KClient client = KClient.initialize("YOUR_API_KEY");
```

3\. Get your feature flags value.

```java
Boolean isEnabled = client.isEnabled("myAwesomeFeature");
if (isEnabled) {
    // Do something when the feature is enabled.
} else {
    // Do something when the feature is not enabled.
}
```

Identify users to allow specific targets. See [KUser](/koople/sdk-reference/java.md#pfuser).

```java
KUser user = KUser.create("aUserId");
Boolean isEnabled = client.isEnabled("myAwesomeFeature", user);
if (isEnabled) {
    // Do something when the feature is enabled.
} else {
    // Do something when the feature is not enabled.
}
```

## Methods

### Initialize

Use `initialize` to get a validated client object.

| Parameter         | Description                                                                                          |          |
| ----------------- | ---------------------------------------------------------------------------------------------------- | -------- |
| `apiKey`          | The server ApiKey for your project environment.                                                      | required |
| `poolingInterval` | Interval in seconds with which the SDK makes requests to the server to update the evaluation object. | optional |

Interval in seconds with which the SDK makes requests to the server to update the evaluation object. The minimum value is `10` seconds. If the value is less than `10` it will be ignored and automatically set to `10`. The default value is `60` seconds.

```javascript
KClient client = KClient.initialize("YOUR_API_KEY", 30);
```

### IsEnabled

The `isEnabled` method will return a boolean indicating whether a release toggle is enabled or disabled.

| Name               | Type     |          |
| ------------------ | -------- | -------- |
| `releaseToggleKey` | `string` | required |
| `user`             | `KUser`  | optional |

```javascript
//anonymous evaluation
kKlient.isEnabled("myAwesomeFeature");
```

```javascript
KUser user = KUser.create("aUserId");
client.isEnabled("myAwesomeFeature", user);
```

The evaluation of an unexistent `releaseToggleKey` will return `false` as an evaluation result.

### ValueOf

The valueOf method will return a value for a remote config. You can pass an optional second parameter that will be used as a fallback if there is no value.

| Name              | Type     |          |
| ----------------- | -------- | -------- |
| `remoteConfigKey` | `string` | required |
| `user`            | `KUser`  | optional |
| `defaultValue`    | `string` | optional |

```javascript
client.valueOf("theme");
// or
client.valueOf("theme", "defaultValue");
```

```javascript
KUser user = KKer.create("aUserId");

client.valueOf("theme", user);
//or
client.valueOf("theme", user, "defaultValue");
```

## Objects

### KUser

The `KUser` object represents a user with the properties to be evaluated. The properties can be added in the following ways.

| Name         | Type                   |          |
| ------------ | ---------------------- | -------- |
| `identity`   | `string`               | required |
| `attributes` | `List<KUserAttribute>` | optional |

```java
ArrayList<KUserAttribute> mylist = new ArrayList<KUserAttribute>() {{
    add(new KUserAttribute("age", 17));
    add(new KUserAttribute("gender", "male"));
}};

KUser user = KUser.create("userId", attributes);
```

The user can be created first for adding attributes later.

```java
KUser user = KUser.create("userId")
                .with("age", 17)
                .with("gender", "male");
```

Also, the user can be created as an anonymous user.

```java
KUser user = KUser.anonymous();
```

### KUserAttribute

The `KUserAttribute` represents a key-value object to set the name and value of a user attribute.

| Name    | Type     |          |
| ------- | -------- | -------- |
| `name`  | `string` | required |
| `value` | `Object` | required |

{% hint style="info" %}
Value object can be a string, a number or a boolean type.
{% endhint %}

```java
new KUserAttribute("age", 17);
new KUserAttribute("gender", "male");
new KUserAttribute("vip", true);
```

##
