# Optimal Location and Specific Locations

## Overview

See [Optimal Location and Specific Locations](/paas/sdk/unified-vpn-sdk/features/optimal-location-and-specific-locations.md)

## Optimal Location Implementation

To start a VPN session with an optimal server:

```kotlin
import unified.vpn.sdk.*

val sessionConfig = SessionConfig.Builder()
    .withLocation(UnifiedSdk.COUNTRY_OPTIMAL)
    .withReason(TrackingConstants.GprReasons.M_UI)
    .build()
```

To retrieve information about the connected server, including the country, you can use the `UnifiedSdk.getStatus` method. This method takes a `Callback<SessionInfo>` object as a parameter, allowing you to handle the success and failure scenarios.

To get the country info for any kind of connection:

```kotlin
import unified.vpn.sdk.*

UnifiedSdk.getStatus(object : Callback<SessionInfo> {
    override fun success(sessionInfo: SessionInfo) {
        val serverInfo = sessionInfo.connectedServerInfo
        // serverInfo.ip
        // serverInfo.country
    }

    override fun failure(e: VpnException) {
        // Handle failure to get session information
    }
})
```

In the `success` method of the callback, you will receive a `SessionInfo` object containing information about the current VPN session. Retrieve the `ConnectedServerInfo` object from the `SessionInfo` object using the `connectedServerInfo` property. This object holds specific details about the connected server.

You can access the desired information from the `ConnectedServerInfo` object. For example, you can retrieve the IP address of the connected server using `serverInfo.ip` or the country using `serverInfo.country`. Uncomment these lines in the provided code snippet to utilize the information.

Handle any failures in retrieving the session information by implementing the appropriate logic in the `failure` method of the callback. This method receives a `VpnException` object, which contains details about the encountered error.

## Specific Locations Implementation

To set specific locations:

```kotlin
import unified.vpn.sdk.*

val city = "San Jose"
UnifiedSdk.getInstance().backend.locations(
    ConnectionType.HYDRA_TCP,
    object : Callback<AvailableLocations> {
        override fun success(data: AvailableLocations) {
            val cityFound = data.getLocations().firstOrNull { it.getLabels().getCity() == city }
            Log.d("CitySearch", "Found matching location $cityFound")
        }

        override fun failure(e: VpnException) {
            // show error
        }
    },
)
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://pango.gitbook.io/paas/sdk/vpn-sdk-for-android/features/multihop/optimal-location-and-specific-locations.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
