Initialize a server SDK with a secret key

Showing JavaScript examples.

A server-side PubNub instance initialized with a secretKey is required to grant and revoke Access Manager tokens. Only a server holding the secretKey can request permission grants. Never initialize a client device with the secretKey.

Before you start​

Confirm you have:

  • Access Manager enabled on your keyset. See Configure access control.
  • Your keyset's publish key, subscribe key, and secret key from Admin Portal.
  • A server environment where the secretKey is never exposed to end users.
Secret key security

Use the secretKey only on a secure server. Never expose it to client devices. If it is compromised, regenerate it in the Admin Portal.

Initialize the server SDK​

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

When initialized with a secretKey, the server instance has root permissions for Access Manager. It can read and write to all resources on the keyset without a token grant.

Secret key rotation​

Paid plans support secret key rotation. The Admin Portal manages up to five secret keys, including the current key. Rotation records are written to the Audit Log.

Rotation lets you replace a compromised or aging key without reusing it. You can stage replacement keys with future expiry dates, so your servers can switch to the new key before the old one expires.

To rotate the secret key of a keyset:

  1. In the Admin Portal, open Apps & Keysets and select the keyset.

  2. Under the current secret key, review the active and expired keys.

  3. Select Rotate and use the date picker to choose when the current key expires.

  4. Update your servers to initialize the SDK with the new secret key before the expiry time.

Was this page useful?

Last updated on