Skip to main content

Overview

The SDK has two levels of identity: The SDK works anonymously out of the box. Call identify(token:) to tie conversations to a specific user. Every request includes the device ID in an X-Device-Id header. Once the user is signed in, requests also include the JWT in X-User-Token. The SDK sets both headers for you.

Device ID

Every install gets a device ID the first time the SDK needs one:
The device ID is a UUID saved in UserDefaults.standard under the key com.chatbase.sdk.deviceId. It stays the same across app launches and across clients, and it is created only once per install, even if you create several clients at the same time on a cold start.
Because it lives in UserDefaults, the device ID does not survive a reinstall, and it is included in device backups, so restoring a backup on a new device reuses the same ID. Anonymous conversations follow that ID. If you want conversations to survive a reinstall, sign the user in with a JWT.

identify

Checks a JWT with the Chatbase server and signs the user in. Later requests belong to that user, and the token is saved so the user stays signed in after the app restarts.
String
required
An HS256 JWT created by your backend and signed with your agent’s identity verification secret. The payload needs a user_id (or sub) claim.
1

Create a JWT on your backend

Sign a JWT with your agent’s identity verification secret, with the user ID in the payload. See Identity Verification for the token format and where to find the secret.
Never put the identity verification secret in your app. Anyone can pull it out of an app bundle. Create tokens on your server and fetch them through your own API.
2

Pass the token to the SDK

Conversations now belong to this user.
3

Check that it worked

identify(token:) throws if the token is invalid, expired, signed with the wrong secret, or if identity verification is not set up for the agent. When it throws, the SDK’s sign-in state does not change, so a failed call cannot leave you half signed in.
When sign-in succeeds, the server also moves conversations created anonymously on this device into the user’s account, so history from before sign-in is kept.The server does this in the background. A listConversations() call made right after identify returns may not show them yet. Refresh a moment later, or the next time the screen appears.

AuthState

There is no isIdentified property. Check authState, or add your own shortcut:

Staying Signed In

The token is saved in the Keychain (service com.chatbase.sdk, account userToken), and it can be read after the device is first unlocked. A new ChatbaseClient loads it when it is created, so the user stays signed in across app launches without calling identify again:
The SDK does not check when the token expires. An expired token stays in authState until a request fails with 401 AUTH_INVALID_JWT. Handle that by getting a new token and calling identify(token:) again:
Calling identify(token:) on every launch where the user is already signed in is cheap, and it keeps the saved token fresh.

Identity Properties

currentUserId comes from the end of a reply, so it appears after the first send or retry finishes, not right after identify. It is also set for anonymous users once the server assigns a user record to the device.

What Identity Changes

Once signed in, conversations belong to the user, so listConversations() returns that user’s conversations from every device they have signed in on. Without sign-in, conversations belong to the device.

logout

Removes the saved token and goes back to anonymous. There is no network call.
logout() also deletes the token from the Keychain and clears currentUserId and currentConversationId, so the next message starts a new anonymous conversation.
logout() does not clear message lists you are already showing. Call ConversationState.clear(), or reset your own state, at the same time, so the previous user’s messages are not left on screen.

Switching Users

To switch from one user to another, log out first so nothing carries over:

Identity Verification

Creating signed JWTs on your backend

Conversations & History

List conversations and load old messages

Error Handling

Handling expired and rejected tokens

Overview

Setup and configuration