SDK integration is independent of ChatKit. For general server integration, see Development SDK. This page retains SDK and ChatKit UI embedding details.
Integration Overview
If you are only calling the XpertAI API from your own server, you do not need to obtain a
client_secret first. The client_secret is primarily used for ChatKit UI or other browser-side short-term access scenarios.
Prerequisites
Before getting started, prepare the following information:apiUrl- Public cloud example:
https://api.xpertai.cn/api/ai - For private deployments, replace with your own XpertAI AI API address
- Public cloud example:
apiKey- It is recommended to use a long-term credential stored securely on the server side
- Can be passed via
x-api-keyorAuthorization: Bearer <apiKey>
xpertId- The target published agent ID: the
assistant_idfield returned byclient.assistants.search()/get()or the corresponding HTTP endpoints.xpertIdis the local variable name used in examples.
- The target published agent ID: the
organization-idx-principal-user-id
Integration via SDK
The XpertAI SDK repository isxpert-sdk-js, but the actual npm package name is @xpert-ai/xpert-sdk.
1. Install the SDK
2. Initialize the Client
ClientConfig supports the following common parameters:
apiUrlapiKeytimeoutMsdefaultHeadersonRequest
defaultHeaders:
3. Minimal Conversation Example
The following example demonstrates the complete main flow:- Use
client.assistantsto query an xpert - Use
client.threadsto create a thread - Use
client.runsto initiate a conversation run
assistantId and use the same agent ID for the run. If creation succeeds but the run returns 403, see the permission troubleshooting section in the SDK guide.
4. Common Capabilities
The most commonly used sub-clients in the SDK include:client.assistants- Query or read xpert information
client.threads- Create and manage threads
client.runs- Use
wait()to get the final result, or usestream()to consume the SSE stream in real time
- Use
client.contexts- Upload context files, e.g.,
client.contexts.uploadFile()
- Upload context files, e.g.,
client.knowledges- Create or manage knowledge base resources
Direct API Calls
The following assistants, threads and runs endpoints are the same HTTP endpoints used by the SDK; they are not deprecated. The official SDK is recommended for general application integration. Legacy AI/v1 OpenAPI integration is separate: those old endpoints and examples are no longer maintained, and their availability is not guaranteed.
1. Authentication with apiKey
You can choose either method:
x-api-key: sk-x-...Authorization: Bearer sk-x-...
x-api-key consistently.
2. Query an Xpert
If you already know the xpert ID, you can callGET /api/ai/assistants/{id} directly. If you want to filter assistants by criteria first, you can use the search endpoint:
assistant_id as <xpert-id> in thread creation and run requests. If you already have that ID, you can confirm the details:
3. Create a Thread
thread_id. You will need to continue using this thread ID for subsequent xpert runs.
The HTTP field is assistant_id; the TypeScript SDK uses assistantId. Although the protocol accepts an unbound thread, agent integrations should bind the target agent at creation and use the same ID for the run. Successful thread creation does not guarantee run permission; see the SDK guide for 403 troubleshooting.
4. Call runs/stream
5. Optional: Upload a File Before Starting a Conversation
If your xpert needs to read attachments, you can upload a context file first:files. In the example below, replace <file-id> with the upload response’s id (the FileAsset ID); storageFileId identifies the underlying storage file and is not the attachment ID:
Authentication & Security
Difference Between apiKey and client_secret
apiKey- Long-term credential
- Suitable for server-to-server calls
- Should never be exposed to the browser
client_secret- Short-term credential
- Suitable for ChatKit UI or frontend-side short-term access
- Issued by
POST /api/ai/v1/chatkit/sessions
When Do You Need client_secret?
If you are:
- Calling XpertAI from your own server using the SDK
- Making direct REST calls from your own server
apiKey.
If you are:
- Embedding ChatKit UI in a browser
- Need to provide a short-term credential to the frontend runtime
client_secret and return it to the frontend.
Correct Usage of chatkit/sessions
POST /api/ai/v1/chatkit/sessions obtains a short-term client_secret. The following example authenticates with a server-side API Key. expires_after is an optional positive integer in seconds; omitting it uses the server default:
- In this API Key exchange, you do not need to pass
assistantoruser; the key supplies the resource scope - A session created with a logged-in user credential instead of an API Key follows a different flow and requires
assistant.idfor agent delegation - If you need to fix organization or business user context, pass
organization-idandx-principal-user-idvia request headers
Security Recommendations
- Never expose long-term
apiKeyin the browser - The frontend should only hold the short-term
client_secret - If using an xpert-specific key, make sure it is bound to the same xpert as the actual access target
- When you need to audit end-user identity, pass
x-principal-user-idduring theclient_secretexchange on the server side
Next Steps
- If you want to embed a frontend chat UI, start by reading 💬 ChatKit SDK
- If you want to further customize visuals and interactions, check out Themes and Customization, Widgets, Client Tools