# Manage Servers

The **VPN SDK** provides a set of APIs to allow your users to manage the servers they choose to use to establish a VPN connection.

The `SudoVPNServer` object contains information about the server.

| Property        | Description                                                                |
| --------------- | -------------------------------------------------------------------------- |
| **country**     | The ISO 3166-1 Alpha-2 code of the country location.                       |
| **region**      | The region within the server country location (e.g. Florida).              |
| **coordinates** | Longitude and latitude coordinates of the server location.                 |
| **load**        | Server utilization which ranges from 0-100. 0 is low, whereas 100 is high. |
| **ipAddress**   | IP Address of the server.                                                  |

## Retrieving Servers

In order to establish a connection using the **VPN SDK**, a server is required. A list of available servers can be retrieved using the list servers API.

{% tabs %}
{% tab title="iOS" %}
There are two options for retrieving the list of servers.&#x20;

The user must be entitled in order to use the method `listServers(cachePolicy:)`. This method updates an internal cache which is required in order for a connection to complete, so there may be efficiency gains to using this method once the user has been confirmed to be entitled.&#x20;

Note that it is not mandatory for clients to call this method explicitly, however it will be called internally upon connection, if its internal cache has not already been populated.

Unentitled users must use the paginated method&#x20;

```
  listServers(
      countriesFilter: [],
      limit:,
      nextToken:,
      cachePolicy:)
```

to retrieve a list of servers, which can be filtered by an array of ISO3166 Alpha-2 two letter country codes. The `limit` and `nextToken` parameters are used to paginate through a potentially large list. This method uses an independent cache.

Each method will either return a list of valid servers to connect to, or an empty list if none are available. In the event of an error, a `SudoVPNError` will be thrown.

An example for unentitled users is:

```swift
var allServers: [SudoVPNServer] = []
do {
    var serverResult = 
        try await vpnClient.listServers(
            countriesFilter: ["AU"], 
            limit: 2000, 
            nextToken: nil)
    allServers.append(contentsOf: serverResult.items)
    // If you wish to handle all retrievals in a single task context
    while serverResult.nextToken != nil {
      serverResult = 
          try await vpnClient.listServers(
              countriesFilter:["AU"],
              limit: 2000,
              nextToken: serverResult.nextToken)
} catch {
    // Handle errors
}
```

And for entitled users:

```swift
var allServers: [SudoVPNServer] = []
do {
    allServers = 
        try await vpnClient.listServers(cachePolicy: .remoteOnly)
} catch {
    // Handle errors
}
```

{% endtab %}

{% tab title="Android" %}
A call to the `listServers` method provides a list of servers that can be used to establish a connection. An example implementation of retrieving the available servers is:

```kotlin
launch {
    try {
        val servers = withContext(Dispatchers.IO) {
            vpnClient.listServers()
        }
        // If no exception is thrown then the list of [servers] is returned.
    } catch (e: SudoVPNException) {
        // Handle/notify user of exception
    }
 }
```

The call to `listServers` will return the list of available servers. If an exception occurs, the `e.localizedMessage` can be presented to the user to notify them as to what caused the exception.
{% endtab %}

{% tab title="Typescript" %}
Clients should use the paginated method&#x20;

```typescript
  listServersByCountries(
      countriesFilter: string[],
      limit?: number,
      nextToken?: string:,
      cachePolicy?: CachePolicy)
```

to retrieve a list of servers, which can be filtered by an array of ISO3166 Alpha-2 two letter country codes. The `limit` and `nextToken` parameters are used to paginate through a potentially large list.&#x20;

This method will return a list of valid servers to connect to, or an empty list if none are available.&#x20;

For example:

```swift
let serverList: SudoVPNServer[] = []
try {
    let serverResult = 
        await vpnClient.listServersByCountries(
            countriesFilter: ['AU'], 
            limit: 200)
    serverList.push(...serverResult.items)
    // If you wish to handle all retrievals in a single task context
    while (serverResult.nextToken) {
      serverResult = 
          await vpnClient.listServersByCountries(
              countriesFilter: ['AU'],
              limit: 200,
              nextToken: serverResult.nextToken)
      serverList.push(...serverResult.items)
} catch {
    // Handle errors
}
```

{% endtab %}
{% endtabs %}
