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.identify
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.
2
Pass the token to the SDK
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 (servicecom.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:
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
logout() also deletes the token from the Keychain and clears currentUserId and currentConversationId, so the next message starts a new anonymous conversation.
Switching Users
To switch from one user to another, log out first so nothing carries over:Related
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
