> ## Documentation Index
> Fetch the complete documentation index at: https://developers.telnyx.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Java SDK advanced usage

> Logging, ProGuard and R8, Jackson, undocumented API functionality, FAQ, and versioning for the Telnyx Java SDK.

## Logging

Enable logging by setting the `TELNYX_LOG` environment variable to `info`:

```sh theme={null}
export TELNYX_LOG=info
```

Or to `debug` for more verbose logging:

```sh theme={null}
export TELNYX_LOG=debug
```

Or configure the client manually using the `logLevel` method:

```java theme={null}
import com.telnyx.sdk.client.TelnyxClient;
import com.telnyx.sdk.client.okhttp.TelnyxOkHttpClient;
import com.telnyx.sdk.core.LogLevel;

TelnyxClient client = TelnyxOkHttpClient.builder()
    .fromEnv()
    .logLevel(LogLevel.INFO)
    .build();
```

## ProGuard and R8

Although the SDK uses reflection, it is still usable with [ProGuard](https://github.com/Guardsquare/proguard) and [R8](https://developer.android.com/topic/performance/app-optimization/enable-app-optimization) because `telnyx-core` is published with a [configuration file](https://github.com/team-telnyx/telnyx-java/blob/master/telnyx-core/src/main/resources/META-INF/proguard/telnyx-core.pro) containing [keep rules](https://www.guardsquare.com/manual/configuration/usage).

ProGuard and R8 should automatically detect and use the published rules, but you can also manually copy the keep rules if necessary.

## Jackson

The SDK depends on [Jackson](https://github.com/FasterXML/jackson) for JSON serialization/deserialization. It is compatible with version 2.13.4 or higher, but depends on version 2.18.2 by default.

The SDK throws an exception if it detects an incompatible Jackson version at runtime (e.g. if the default version was overridden in your Maven or Gradle config).

If the SDK threw an exception, but you're *certain* the version is compatible, then disable the version check using the `checkJacksonVersionCompatibility` on [`TelnyxOkHttpClient`](https://github.com/team-telnyx/telnyx-java/blob/master/telnyx-client-okhttp/src/main/kotlin/com/telnyx/sdk/client/okhttp/TelnyxOkHttpClient.kt) or [`TelnyxOkHttpClientAsync`](https://github.com/team-telnyx/telnyx-java/blob/master/telnyx-client-okhttp/src/main/kotlin/com/telnyx/sdk/client/okhttp/TelnyxOkHttpClientAsync.kt).

> \[!CAUTION]
> We make no guarantee that the SDK works correctly when the Jackson version check is disabled.

Also note that there are bugs in older Jackson versions that can affect the SDK. We don't work around all Jackson bugs ([example](https://github.com/FasterXML/jackson-databind/issues/3240)) and expect users to upgrade Jackson for those instead.

## Undocumented API functionality

The SDK is typed for convenient usage of the documented API. However, it also supports working with undocumented or not yet supported parts of the API.

### Parameters

To set undocumented parameters, call the `putAdditionalHeader`, `putAdditionalQueryParam`, or `putAdditionalBodyProperty` methods on any `Params` class:

```java theme={null}
import com.telnyx.sdk.core.JsonValue;
import com.telnyx.sdk.models.calls.CallDialParams;

CallDialParams params = CallDialParams.builder()
    .putAdditionalHeader("Secret-Header", "42")
    .putAdditionalQueryParam("secret_query_param", "42")
    .putAdditionalBodyProperty("secretProperty", JsonValue.from("42"))
    .build();
```

These can be accessed on the built object later using the `_additionalHeaders()`, `_additionalQueryParams()`, and `_additionalBodyProperties()` methods.

To set undocumented parameters on *nested* headers, query params, or body classes, call the `putAdditionalProperty` method on the nested class:

```java theme={null}
import com.telnyx.sdk.core.JsonValue;
import com.telnyx.sdk.models.calls.CallDialParams;

CallDialParams params = CallDialParams.builder()
    .answeringMachineDetectionConfig(CallDialParams.AnsweringMachineDetectionConfig.builder()
        .putAdditionalProperty("secretProperty", JsonValue.from("42"))
        .build())
    .build();
```

These properties can be accessed on the nested built object later using the `_additionalProperties()` method.

To set a documented parameter or property to an undocumented or not yet supported *value*, pass a [`JsonValue`](https://github.com/team-telnyx/telnyx-java/blob/master/telnyx-core/src/main/kotlin/com/telnyx/sdk/core/Values.kt) object to its setter:

```java theme={null}
import com.telnyx.sdk.core.JsonValue;
import com.telnyx.sdk.models.calls.CallDialParams;

CallDialParams params = CallDialParams.builder()
    .connectionId(JsonValue.from(42))
    .from("+15557654321")
    .to("+15551234567")
    .webhookUrl("https://your-webhook.url/events")
    .build();
```

The most straightforward way to create a [`JsonValue`](https://github.com/team-telnyx/telnyx-java/blob/master/telnyx-core/src/main/kotlin/com/telnyx/sdk/core/Values.kt) is using its `from(...)` method:

```java theme={null}
import com.telnyx.sdk.core.JsonValue;
import java.util.List;
import java.util.Map;

// Create primitive JSON values
JsonValue nullValue = JsonValue.from(null);
JsonValue booleanValue = JsonValue.from(true);
JsonValue numberValue = JsonValue.from(42);
JsonValue stringValue = JsonValue.from("Hello World!");

// Create a JSON array value equivalent to `["Hello", "World"]`
JsonValue arrayValue = JsonValue.from(List.of(
  "Hello", "World"
));

// Create a JSON object value equivalent to `{ "a": 1, "b": 2 }`
JsonValue objectValue = JsonValue.from(Map.of(
  "a", 1,
  "b", 2
));

// Create an arbitrarily nested JSON equivalent to:
// {
//   "a": [1, 2],
//   "b": [3, 4]
// }
JsonValue complexValue = JsonValue.from(Map.of(
  "a", List.of(
    1, 2
  ),
  "b", List.of(
    3, 4
  )
));
```

Normally a `Builder` class's `build` method will throw [`IllegalStateException`](https://docs.oracle.com/javase/8/docs/api/java/lang/IllegalStateException.html) if any required parameter or property is unset.

To forcibly omit a required parameter or property, pass [`JsonMissing`](https://github.com/team-telnyx/telnyx-java/blob/master/telnyx-core/src/main/kotlin/com/telnyx/sdk/core/Values.kt):

```java theme={null}
import com.telnyx.sdk.core.JsonMissing;
import com.telnyx.sdk.models.calls.CallDialParams;

CallDialParams params = CallDialParams.builder()
    .from("+18005550101")
    .to("+18005550100 or sip:username@sip.telnyx.com;secure=srtp")
    .connectionId(JsonMissing.of())
    .build();
```

### Response properties

To access undocumented response properties, call the `_additionalProperties()` method:

```java theme={null}
import com.telnyx.sdk.core.JsonValue;
import java.util.Map;

Map<String, JsonValue> additionalProperties = client.calls().dial(params)._additionalProperties();
JsonValue secretPropertyValue = additionalProperties.get("secretProperty");

String result = secretPropertyValue.accept(new JsonValue.Visitor<>() {
    @Override
    public String visitNull() {
        return "It's null!";
    }

    @Override
    public String visitBoolean(boolean value) {
        return "It's a boolean!";
    }

    @Override
    public String visitNumber(Number value) {
        return "It's a number!";
    }

    // Other methods include `visitMissing`, `visitString`, `visitArray`, and `visitObject`
    // The default implementation of each unimplemented method delegates to `visitDefault`, which throws by default, but can also be overridden
});
```

To access a property's raw JSON value, which may be undocumented, call its `_` prefixed method:

```java theme={null}
import com.telnyx.sdk.core.JsonField;
import java.util.Optional;

JsonField<String> connectionId = client.calls().dial(params)._connectionId();

if (connectionId.isMissing()) {
  // The property is absent from the JSON response
} else if (connectionId.isNull()) {
  // The property was set to literal null
} else {
  // Check if value was provided as a string
  // Other methods include `asNumber()`, `asBoolean()`, etc.
  Optional<String> jsonString = connectionId.asString();

  // Try to deserialize into a custom type
  MyClass myObject = connectionId.asUnknown().orElseThrow().convert(MyClass.class);
}
```

### Response validation

In rare cases, the API may return a response that doesn't match the expected type. For example, the SDK may expect a property to contain a `String`, but the API could return something else.

By default, the SDK will not throw an exception in this case. It will throw [`TelnyxInvalidDataException`](https://github.com/team-telnyx/telnyx-java/blob/master/telnyx-core/src/main/kotlin/com/telnyx/sdk/errors/TelnyxInvalidDataException.kt) only if you directly access the property.

Validating the response is *not* forwards compatible with new types from the API for existing fields.

If you would still prefer to check that the response is completely well-typed upfront, then either call `validate()`:

```java theme={null}
import com.telnyx.sdk.models.calls.CallDialResponse;

CallDialResponse response = client.calls().dial(params).validate();
```

Or configure the method call to validate the response using the `responseValidation` method:

```java theme={null}
import com.telnyx.sdk.models.calls.CallDialResponse;

CallDialResponse response = client.calls().dial(
  params, RequestOptions.builder().responseValidation(true).build()
);
```

Or configure the default for all method calls at the client level:

```java theme={null}
import com.telnyx.sdk.client.TelnyxClient;
import com.telnyx.sdk.client.okhttp.TelnyxOkHttpClient;

TelnyxClient client = TelnyxOkHttpClient.builder()
    .fromEnv()
    .responseValidation(true)
    .build();
```

## Proxies

To route requests through a proxy, configure the client using the `proxy` method:

```java theme={null}
import com.telnyx.sdk.client.TelnyxClient;
import com.telnyx.sdk.client.okhttp.TelnyxOkHttpClient;
import java.net.InetSocketAddress;
import java.net.Proxy;

TelnyxClient client = TelnyxOkHttpClient.builder()
    .fromEnv()
    .proxy(new Proxy(
      Proxy.Type.HTTP, new InetSocketAddress(
        "https://example.com", 8080
      )
    ))
    .build();
```

If the proxy responds with `407 Proxy Authentication Required`, supply credentials by also configuring `proxyAuthenticator`:

```java theme={null}
import com.telnyx.sdk.client.TelnyxClient;
import com.telnyx.sdk.client.okhttp.TelnyxOkHttpClient;
import com.telnyx.sdk.core.http.ProxyAuthenticator;

TelnyxClient client = TelnyxOkHttpClient.builder()
    .fromEnv()
    .proxy(...)
    // Or a custom implementation of `ProxyAuthenticator`.
    .proxyAuthenticator(ProxyAuthenticator.basic("username", "password"))
    .build();
```
