# Dart SDK

The **Turnix Dart SDK** provides a simple and secure way to interact with the Turnix API from your backend. It helps you authenticate using your API token and easily request ICE credentials for peer-to-peer connections.

> 📦 Source: [turnix\_dart on pub.dev](https://pub.dev/packages/turnix_dart)

---

### 📥 Installation

Install the SDK using Dart:

```bash
dart pub add turnix_dart
```

---

### 📦 Import the SDK

Import the `TurnixIO` class in your Dart server-side application:

```dart
import 'package:turnix_dart/turnix_dart.dart';
```

---

### 🔐 Authenticate with Your API Token

Use your Turnix API token to make a call to request credentials (see [Quickstart](https://turnix.io/docs/getting-started/quickstart)):

```dart
final creds = await TurnixIO.getIceCredentials(
  apiToken: 'your-bearer-token',
);
```

> **Warning: Sensitive information**
>
>
>  Always keep your API token secret - use it only in server-side environments.
>

---

### 🌐 Request ICE Credentials

Use the SDK to request ICE credentials with optional parameters like client IDs, room, region, and TTL:

```dart
final creds = await TurnixIO.getIceCredentials(
  apiToken: 'your-bearer-token',
  initiatorClient: 'alice@example.com',
  receiverClient: 'bob@example.com',
  room: 'demo-room',
  ttl: 60, // optional time-to-live in seconds
);
```

---

### 🔎 Retrieve Available Regions

Fetch the full list of TURN regions supported by Turnix, including metadata and status flags:

```dart
final regions = await TurnixIO.getAvailableRegions(
  apiToken: 'your-bearer-token',
);

print(
  'Available regions: ${jsonEncode(
    regions.map((region) => region.toString()).join(",")
  )}',
);
```

Each `Region` includes:

* `slug`: region identifier (e.g., `us-east`)
* `nat2`: ISO country code
* `city`: primary city location
* `name`: human-readable region name
* `lat`, `lon`: geographic coordinates
* `isOnline`: whether the server is reachable
* `isOperational`: whether allocation is functional

---

### 🎯 Use ICE Servers in WebRTC

Once you have the `iceServers` array, pass it into your WebRTC client setup on the frontend, e.g. in Flutter using 'flutter\_webrtc':

```dart
final pc = await createPeerConnection({
  'iceServers': creds.iceServers.map((s) => {
    'urls': s.urls,
    if (s.username != null) 'username': s.username,
    if (s.credential != null) 'credential': s.credential,
  }).toList(),
});
```

This completes your setup for establishing a secure and reliable peer-to-peer connection using Turnix.
