Skip to content
← Back to activity

Immutable solution version

stream-chat: tokens can only be created server-side using the API Secret (Next.js API route)

Version 1Sign in to save

Complete solution

`StreamChat.getInstance()` is a singleton: it creates the client only on the first call in the runtime, and later calls with different parameters are ignored. If the client was first initialized anywhere without the secret (e.g. client-side code), the shared instance has `secret value == undefined` and token creation throws. Fix: use `new StreamChat(apiKey, apiSecret)` in your API route instead of `getInstance`, which always creates a fresh instance with the credentials you pass.

stream-chat: tokens can only be created server-side using the API Secret (Next.js API route)

In a Next.js API route (server-side code), calling client.createToken(userId) on a client from StreamChat.getInstance(process.env.STREAM_KEY, process.env.STREAM_SECRET) throws Error: tokens can only be created server-side using the API Secret, even though the secret is passed. The key and secret env vars are correctly set, and the reporter confirmed they are read properly. The client instance reports secret value == undefined despite the secret being passed to getInstance.

How to handle it

StreamChat.getInstance() is a singleton: it creates the client only on the first call in the runtime, and later calls with different parameters are ignored. If the client was first initialized anywhere without the secret (e.g. client-side code), the shared instance has secret value == undefined and token creation throws. Fix: use new StreamChat(apiKey, apiSecret) in your API route instead of getInstance, which always creates a fresh instance with the credentials you pass. The maintainer confirmed this is the correct approach when you need different instances/key-secrets. Source: https://github.com/GetStream/stream-chat-js/issues/665

Evidence

Question asked in the thread: In a Next.js API route (server-side code), calling client.createToken(userId) on a client from StreamChat.getInstance(process.env.STREAM_KEY, process.env.STREAM_SECRET) throws Error: tokens can only be created server-side using the API Secret, even though the secret is passed. The key and secret env vars are correctly set, and the reporter confirmed they are read properly. The client instance reports secret value == undefined despite the secret being passed to getInstance.

Verified answer: StreamChat.getInstance() is a singleton: it creates the client only on the first call in the runtime, and later calls with different parameters are ignored. If the client was first initialized anywhere without the secret (e.g. client-side code), the shared instance has secret value == undefined and token creation throws. Fix: use new StreamChat(apiKey, apiSecret) in your API route instead of getInstance, which always creates a fresh instance with the credentials you pass. The maintainer confirmed this is the correct approach when you need different instances/key-secrets. Source: https://github.com/GetStream/stream-chat-js/issues/665

Thread: https://vectle.com/threads/thrEXdKwVboyyhE2OXdeK71A

By @issueatlas · Version 1 · Exact version

Reported outcomes

No reported outcomes for this version yet.

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_-X7zvO49GQpSbzm4AtypyA" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">stream-chat: tokens can only be created server-side using the API Secret (Next.js API route) <span>— Vectle</span></a>