# SDK setup
Use these instructions to install, authenticate and instantiate the Chat SDK.
## Authentication
An API key is required to authenticate with Ably. API keys are used either to authenticate directly with Ably using basic authentication, or to generate tokens for untrusted clients using [token authentication](https://ably.com/docs/auth/token).
[Sign up](https://ably.com/sign-up) to Ably to create an API key in the dashboard or use the [Control API](https://ably.com/docs/platform/account/control-api) to create an API key programmatically.
API keys and tokens have a set of [capabilities](https://ably.com/docs/auth/capabilities) assigned to them that specify which operations, such as subscribe or publish can be performed on which resources. To use the Chat SDK, the API key requires the following capabilities depending on which features are being used:
| Feature | Capabilities |
| ------- | ------------ |
| Send and receive messages | `publish`, `subscribe` |
| Update message | `message-update-any` or `message-update-own` |
| Delete message | `message-delete-any` or `message-delete-own` |
| Message history | `subscribe`, `history` |
| Message reactions | `annotation-publish`, optionally `annotation-subscribe` |
| Presence | `subscribe`, `presence` |
| Room occupancy | `subscribe`, `channel-metadata` |
| Typing indicators | `publish`, `subscribe` |
| Room reactions | `publish`, `subscribe` |
When setting the capabilities for Chat, you can apply them to specific chat rooms, a group of chat rooms in a common namespace, or all chat rooms:
* `my-chat-room` or
* `dms:*` or
* `*`
For more guidance, see the [capabilities documentation](https://ably.com/docs/auth/capabilities).
## Install
The Chat SDK is built on top of the Ably Pub/Sub SDK and uses that to establish a connection with Ably.
### NPM
Install the Pub/Sub SDK and the Chat SDK:
```shell
npm install ably @ably/chat
```
Import the SDKs into your project:
```javascript
```
```react
```
### CDN
Reference the Pub/Sub SDK and the Chat SDK within your HTML file:
```javascript
```
### Swift Package Manager
The SDK is distributed as a Swift Package and can be installed using Xcode or by adding it as a dependency in your package's `Package.swift`.
To install the `ably-chat-swift` package in your Xcode Project:
* Paste `https://github.com/ably/ably-chat-swift` in the Swift Packages search box (Xcode project → Swift Packages.. . → `+` button).
* Select the Ably Chat SDK for your target.
To install the `ably-chat-swift` package in another Swift Package, add the following to your `Package.swift`:
```swift
.package(url: "https://github.com/ably/ably-chat-swift", from: "1.0.0"),
```
Import the SDK:
```swift
const realtimeClient = new Ably.Realtime({ key: 'your-api-key', clientId: ''});
const chatClient = new ChatClient(realtimeClient, { logLevel: LogLevel.Error });
```
```react
const realtimeClient = new Ably.Realtime({ key: 'your-api-key', clientId: ''});
const chatClient = new ChatClient(realtimeClient, { logLevel: LogLevel.Error });
const App = () => {
return (
);
};
```
```swift
let realtimeOptions = ARTClientOptions()
realtimeOptions.key = "your-api-key"
realtimeOptions.clientId = ""
let realtime = ARTRealtime(options: realtimeOptions)
let chatClient = ChatClient(realtime: realtime)
```
```kotlin
const ably = new Ably.Realtime({ key: 'your-api-key', clientId: ''});
const chatClient = new ChatClient(ably, {logHandler: logWriteFunc, logLevel: 'debug' });
const App = => {
return (
);
};
```
```swift
let realtimeOptions = ARTClientOptions()
realtimeOptions.key = "your-api-key"
realtimeOptions.clientId = ""
let realtime = ARTRealtime(options: realtimeOptions)
let clientOptions = ChatClientOptions(logHandler: SomeLogHandler(), logLevel: .debug)
return ChatClient(realtime: realtime, clientOptions: clientOptions)
```
```kotlin
val realtimeClient = AblyRealtime(
ClientOptions().apply {
key = "your-api-key"
clientId = ""
},
)
val chatClient = ChatClient(realtimeClient) {
logHandler = CustomLogHandler() // Implements com.ably.chat.LogHandler interface
logLevel = LogLevel.Debug
}
```
```jetpack
val realtimeClient = AblyRealtime(
ClientOptions().apply {
key = "your-api-key"
clientId = ""
},
)
val chatClient = ChatClient(realtimeClient) {
logHandler = CustomLogHandler() // Implements com.ably.chat.LogHandler interface
logLevel = LogLevel.Debug
}
```
The `logHandler` property is your own function that will be called for each line of log output generated by the Chat SDK.
The `logHandler` property is your custom `LogHandler` implementation that will be called for each line of log output generated by the Chat SDK.
The `logLevel` sets the verbosity of logs that will be output by the SDK. The following log levels are available to set:
| Level | Description |
| ----- | ----------- |
| trace | Something routine and expected has occurred. This level will provide logs for the vast majority of operations and function calls. |
| debug | Development information, messages that are useful when trying to debug library behavior, but superfluous to normal operation. |
| info | Informational messages. Operationally significant to the library but not out of the ordinary. |
| warn | Anything that is not immediately an error, but could cause unexpected behavior in the future. For example, passing an invalid value to an option. Indicates that some action should be taken to prevent future errors. |
| error | A given operation has failed and cannot be automatically recovered. The error may threaten the continuity of operation. |
| silent | No logging will be performed. |