Initialize a client SDK with an auth key

Showing JavaScript examples.

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:

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.

1const pubnub = new PubNub({
2 subscribeKey: "mySubscribeKey",
3 publishKey: "myPublishKey",
4 userId: "myUniqueUserId",
5 authKey: "yourAuthKey"
6});

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.

1pubnub.setToken("newToken")

Was this page useful?

Last updated on