This article explains how to integrate XpertAI backend capabilities in ChatKit-related scenarios, including integration via SDK and direct HTTP API calls.
Integration Overview
| Method | Use Case | Requires client_secret? | Recommended Credential | Typical Entry Point |
|---|---|---|---|---|
| SDK Integration | Node.js / TypeScript server-side, scripting tools, backend workflows | No, not needed for server-to-server | apiKey | Client, client.assistants, client.threads, client.runs |
| Direct API Call | Any language, gateway, server-to-server integration | No, not needed for server-to-server | apiKey | /api/ai/assistants/search, /api/ai/threads, /api/ai/threads/{thread_id}/runs/stream |
| ChatKit UI Embedding | Browser-side ChatKit widget embedding | Yes | Store apiKey on server, frontend only receives client_secret | POST /api/ai/v1/chatkit/sessions |
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
xpert_idof the target published xpert
- 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
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
If you are not using the SDK, you can also call the public REST API directly.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:
3. Create a Thread
thread_id. You will need to continue using this thread ID for subsequent xpert runs.
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 run request:
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 is only used to obtain a short-term client_secret. The current endpoint request body only requires expires_after:
- You do not need to pass
xpertin the request body - You do not need to pass
userin the request body - 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