---
source_url: https://www.pubnub.com/docs/security/access-control/initialize-client-sdk
title: Initialize a client SDK with an auth key
updated_at: 2026-09-30T07:20:08.000Z
---

# Initialize a client SDK with an auth key

> For AI agents: documentation index at https://www.pubnub.com/llms-full.txt

After your server grants a token, the client sets it as the `authKey` and sends it with every subsequent PubNub API call. This page covers two operations: setting the token at initialization and updating it after the client is already running.

## Before you start

Confirm you have:

* Access Manager enabled on your keyset. See [Configure access control](https://www.pubnub.com/docs/security/access-control/configure-access-control.md).
* A server that grants tokens. See [Grant, change, and revoke permissions](https://www.pubnub.com/docs/security/access-control/manage-permissions.md).
* A token received from your server for the current user.

## Initialize with an auth key

Set the token in the SDK configuration when the client starts. The client sends it with every PubNub API call until you replace it.

### JavaScript

```javascript
const pubnub = new PubNub({
  subscribeKey: "mySubscribeKey",
  publishKey: "myPublishKey",
  userId: "myUniqueUserId",
  authKey: "yourAuthKey"
});
```

### Python

```python
from pubnub.pnconfiguration import PNConfiguration
from pubnub.pubnub import PubNub

pnconfig = PNConfiguration()
pnconfig.subscribe_key = "my_subscribe_key"
pnconfig.publish_key = "my_publish_key"
pnconfig.user_id = "my_unique_user_id"
pnconfig.auth_key = "your_auth_key"
pnconfig.ssl = True

pubnub = PubNub(pnconfig)
```

### Swift

```swift
import PubNub

let config = PubNubConfiguration(
  publishKey: "myPublishKey",
  subscribeKey: "mySubscribeKey",
  userId: "myUniqueUserId",
  authKey: "yourAuthKey"
)

let pubnub = PubNub(configuration: config)
```

### Objective-C

```objectivec
#import <PubNub/PubNub.h>

PNConfiguration *config = [PNConfiguration configurationWithPublishKey:@"myPublishKey"
                                                           subscribeKey:@"mySubscribeKey"
                                                                 userID:@"myUniqueUserId"];
config.authKey = @"yourAuthKey";

self.client = [PubNub clientWithConfiguration:config];
```

### Go

```go
package main

import pubnub "github.com/pubnub/go"

func main() {
    config := pubnub.NewConfigWithUserId("myUniqueUserId")
    config.SubscribeKey = "mySubscribeKey"
    config.PublishKey = "myPublishKey"
    config.AuthKey = "yourAuthKey"
    config.Secure = true

    pn := pubnub.NewPubNub(config)
}
```

### Unity

```csharp
using PubnubApi;

PNConfiguration pnConfiguration = new PNConfiguration(new UserId("myUniqueUserId"));
pnConfiguration.SubscribeKey = "mySubscribeKey";
pnConfiguration.PublishKey = "myPublishKey";
pnConfiguration.AuthKey = "yourAuthKey";
pnConfiguration.Secure = true;

Pubnub pubnub = new Pubnub(pnConfiguration);
```

### C#

```csharp
using PubnubApi;

PNConfiguration pnConfiguration = new PNConfiguration(new UserId("myUniqueUserId"));
pnConfiguration.SubscribeKey = "mySubscribeKey";
pnConfiguration.PublishKey = "myPublishKey";
pnConfiguration.AuthKey = "yourAuthKey";
pnConfiguration.Secure = true;

Pubnub pubnub = new Pubnub(pnConfiguration);
```

### Dart

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

final myKeyset = Keyset(
  subscribeKey: 'mySubscribeKey',
  publishKey: 'myPublishKey',
  authKey: 'yourAuthKey',
  userId: UserId('myUniqueUserId'),
);

final pubnub = PubNub(defaultKeyset: myKeyset);
```

### PHP

```php
use PubNub\PNConfiguration;
use PubNub\PubNub;

$pnConfiguration = new PNConfiguration();
$pnConfiguration->setSubscribeKey("mySubscribeKey");
$pnConfiguration->setPublishKey("myPublishKey");
$pnConfiguration->setUserId("myUniqueUserId");
$pnConfiguration->setAuthKey("yourAuthKey");
$pnConfiguration->setSecure(true);

$pubnub = new PubNub($pnConfiguration);
```

### Ruby

```ruby
require 'pubnub'

pubnub = Pubnub.new(
    subscribe_key: 'mySubscribeKey',
    publish_key: 'myPublishKey',
    user_id: 'myUniqueUserId',
    auth_key: 'yourAuthKey',
    ssl: true
)
```

## Update the token after initialization

When your server issues a new token (for example, after a permission change or token refresh), set it on the running client instance using `setToken`. The client uses the new token for all subsequent calls.

### JavaScript

```javascript
pubnub.setToken("newToken")
```

### Python

```python
pubnub.set_token("new_token")
```

### Java

```java
pubnub.setToken("newToken")
```

### Kotlin

```kotlin
pubnub.setToken("newToken")
```

### Swift

```swift
pubnub.set(token: "newToken")
```

### Objective-C

```objectivec
[self.client setAuthToken:@"NewToken"];
```

### Go

```go
pn.SetToken("NewToken")
```

### C-Core (legacy)

:::note New SDK available
C-Core (legacy) is still supported. If you are starting a new project, use the new [C SDK](https://www.pubnub.com/docs/sdks/c.md).
:::

```c
pubnub.pubnub_set_token(<pubnub_context>, "NewToken")
```

### Unity

```csharp
pubnub.SetToken("newToken")
```

### C#

```csharp
pubnub.SetAuthToken("newToken")
```

### Dart

```dart
pubnub.setToken("newToken")
```

### PHP

```php
$pubnub->setToken("newToken");
```

### Ruby

```ruby
pubnub.set_token("newToken")
```

## Related pages

* [Grant, change, and revoke permissions](https://www.pubnub.com/docs/security/access-control/manage-permissions.md). Issue tokens from your server.
* [Check available permissions](https://www.pubnub.com/docs/security/access-control/check-permissions.md). Decode a token to inspect its permissions.
* [Change or extend access](https://www.pubnub.com/docs/security/access-control/change-or-extend-access.md). Strategies for keeping tokens current.
* [Update an expired token](https://www.pubnub.com/docs/security/access-control/update-expired-token.md). Handle `403` errors when a token has expired.

Last updated at: 2026-09-30T07:20:08.000Z
