For AI agents: this page in Markdown: https://www.pubnub.com/docs/security/access-control/initialize-client-sdk.md. Documentation index: https://www.pubnub.com/llms-full.txt.
Initialize a client SDK with an auth key
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.
- A server that grants tokens. See Grant, change, and revoke permissions.
- 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
- Python
- Swift
- Objective-C
- Go
- Unity
- C#
- Dart
- PHP
- Ruby
1const pubnub = new PubNub({
2 subscribeKey: "mySubscribeKey",
3 publishKey: "myPublishKey",
4 userId: "myUniqueUserId",
5 authKey: "yourAuthKey"
6});
1from pubnub.pnconfiguration import PNConfiguration
2from pubnub.pubnub import PubNub
3
4pnconfig = PNConfiguration()
5pnconfig.subscribe_key = "my_subscribe_key"
6pnconfig.publish_key = "my_publish_key"
7pnconfig.user_id = "my_unique_user_id"
8pnconfig.auth_key = "your_auth_key"
9pnconfig.ssl = True
10
11pubnub = PubNub(pnconfig)
1import PubNub
2
3let config = PubNubConfiguration(
4 publishKey: "myPublishKey",
5 subscribeKey: "mySubscribeKey",
6 userId: "myUniqueUserId",
7 authKey: "yourAuthKey"
8)
9
10let pubnub = PubNub(configuration: config)
1#import <PubNub/PubNub.h>
2
3PNConfiguration *config = [PNConfiguration configurationWithPublishKey:@"myPublishKey"
4 subscribeKey:@"mySubscribeKey"
5 userID:@"myUniqueUserId"];
6config.authKey = @"yourAuthKey";
7
8self.client = [PubNub clientWithConfiguration:config];
1package main
2
3import pubnub "github.com/pubnub/go"
4
5func main() {
6 config := pubnub.NewConfigWithUserId("myUniqueUserId")
7 config.SubscribeKey = "mySubscribeKey"
8 config.PublishKey = "myPublishKey"
9 config.AuthKey = "yourAuthKey"
10 config.Secure = true
11
12 pn := pubnub.NewPubNub(config)
13}
1using PubnubApi;
2
3PNConfiguration pnConfiguration = new PNConfiguration(new UserId("myUniqueUserId"));
4pnConfiguration.SubscribeKey = "mySubscribeKey";
5pnConfiguration.PublishKey = "myPublishKey";
6pnConfiguration.AuthKey = "yourAuthKey";
7pnConfiguration.Secure = true;
8
9Pubnub pubnub = new Pubnub(pnConfiguration);
1using PubnubApi;
2
3PNConfiguration pnConfiguration = new PNConfiguration(new UserId("myUniqueUserId"));
4pnConfiguration.SubscribeKey = "mySubscribeKey";
5pnConfiguration.PublishKey = "myPublishKey";
6pnConfiguration.AuthKey = "yourAuthKey";
7pnConfiguration.Secure = true;
8
9Pubnub pubnub = new Pubnub(pnConfiguration);
1import 'package:pubnub/pubnub.dart';
2
3final myKeyset = Keyset(
4 subscribeKey: 'mySubscribeKey',
5 publishKey: 'myPublishKey',
6 authKey: 'yourAuthKey',
7 userId: UserId('myUniqueUserId'),
8);
9
10final pubnub = PubNub(defaultKeyset: myKeyset);
1use PubNub\PNConfiguration;
2use PubNub\PubNub;
3
4$pnConfiguration = new PNConfiguration();
5$pnConfiguration->setSubscribeKey("mySubscribeKey");
6$pnConfiguration->setPublishKey("myPublishKey");
7$pnConfiguration->setUserId("myUniqueUserId");
8$pnConfiguration->setAuthKey("yourAuthKey");
9$pnConfiguration->setSecure(true);
10
11$pubnub = new PubNub($pnConfiguration);
1require 'pubnub'
2
3pubnub = Pubnub.new(
4 subscribe_key: 'mySubscribeKey',
5 publish_key: 'myPublishKey',
6 user_id: 'myUniqueUserId',
7 auth_key: 'yourAuthKey',
8 ssl: true
9)
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
- Python
- Java
- Kotlin
- Swift
- Objective-C
- Go
- C-Core (legacy)
- Unity
- C#
- Dart
- PHP
- Ruby
1pubnub.setToken("newToken")
1pubnub.set_token("new_token")
1pubnub.setToken("newToken")
1pubnub.setToken("newToken")
1pubnub.set(token: "newToken")
1[self.client setAuthToken:@"NewToken"];
1pn.SetToken("NewToken")
New SDK available
C-Core (legacy) is still supported. If you are starting a new project, use the new C SDK.
1pubnub.pubnub_set_token(<pubnub_context>, "NewToken")
1pubnub.SetToken("newToken")
1pubnub.SetAuthToken("newToken")
1pubnub.setToken("newToken")
1$pubnub->setToken("newToken");
1pubnub.set_token("newToken")
Related pages
- Grant, change, and revoke permissions. Issue tokens from your server.
- Check available permissions. Decode a token to inspect its permissions.
- Change or extend access. Strategies for keeping tokens current.
- Update an expired token. Handle
403errors when a token has expired.