Chat API
Stream real-time conversations with AI agents for analytics and data exploration.Endpoint
Overview
The Chat API enables real-time streaming conversations with Cube D3’s AI agents. This endpoint allows applications to integrate AI-powered analytics conversations directly into their user interfaces.Authentication
Use the session-based authentication flow:- Generate a session using your API key. This endpoint will automatically create (insert) or update the external user based on the
externalId
provided:
Accounts are limited to 10,000 external users. To increase this limit, please contact support.
- Exchange session ID for a token:
User Attributes
User attributes allow you to pass contextual information about the user to the AI agent. This enables personalized responses and automatic data filtering based on user permissions through row-level security policies.User attributes must first be configured in your Cube D3 admin panel before they can be used. See the User Attributes Administration Guide for setup instructions.
externalId
(required): Unique identifier for the useremail
(optional): User’s email addressuserAttributes
(optional): Array of key-value pairs containing user metadata
- Row-Level Security: Automatically filter data based on user attributes through access policies
- Personalized Responses: AI agent can tailor responses based on user context
- Access Control: Restrict data access to only what the user should see
- Filter sales data by user’s assigned territory
- Show only customers from user’s city
- Limit financial data based on user’s department
- Personalize dashboards by user role
securityContext.cubeCloud.userAttributes.{attributeName}
for use in access policies.
Endpoint Reference
Path Parameters
The tenant identifier (e.g., “acme”). It is in your account URL, e.g. https://acme.cubecloud.dev
The AI agent identifier (e.g., “1”). You can find it in the Admin -> Agents -> Click on Agent row in the table.
Request Body
Unique identifier for the chat session. We recommend generating a UUID for this.
User message or query to send to the AI agent, e.g. “What is our revenue last month?”
Response Format
The API returns streaming JSON responses. Each line contains a separate JSON object representing a chat message or state update.
Response Fields
Unique message identifier
Message sender:
"user"
or "assistant"
Message content (streamed incrementally for assistant messages)
Agent’s internal reasoning process (visible in development mode)
Information about tool calls made by the agent during processing
Whether this is an incremental content update
Whether the message is still being generated
Message ordering sequence number
Current streaming state information
Response Examples
Example Streaming Response
Example Streaming Response
The API returns a series of JSON objects, each on a separate line. Here’s what a typical conversation looks like:Initial State Message:User Message Echo:Assistant Response Start:Thinking Process:Content Streaming:Tool Call Initiated:Tool Call Result:SQL Query Tool Call:SQL Query Result:Final Complete Message:Final State Update:
Tool Calls
When the AI agent performs actions like querying data or searching metadata, tool calls are included in the response stream. These provide transparency into the agent’s reasoning and data retrieval process.All tool results are returned as JSON strings within the
toolCall.result
field, not as parsed JSON objects. You must parse these strings to access the structured data.toolCall.input
- JSON string containing tool parameterstoolCall.result
- JSON string containing tool response (only present whenisInProcess: false
)- Both input and result contain escaped JSON that requires parsing
- Results may contain nested JSON structures, especially for complex data queries
- Error responses are also JSON strings with consistent error formatting
cubeMeta
Searches cube metadata and schema informationcubeMeta Tool Result Format
cubeMeta Tool Result Format
The Input Structure:Input Fields:Result Fields:
cubeMeta
tool returns cube schema information as a JSON string. When successful, the result contains:searchQuery
(string): Search query for data model
cubes
(array): Available cube/view definitions with memberssearchQuery
(string): The original search query used
cubeSqlApi
Executes SQL queries against cube datacubeSqlApi Tool Result Format
cubeSqlApi Tool Result Format
The Input Structure:Input Fields:Result Fields:
cubeSqlApi
tool executes SQL queries and returns data with metadata. The input typically includes additional context fields:sqlQuery
(string): The SQL query to executequeryTitle
(string): Human-readable title for the querydescription
(string): Detailed description of what the query doesuserRequest
(string): Original user request that triggered this querymemoryId
(string): Reference to previous analysis or contextvegaSpec
(string): Vega-Lite v5 visualization specification as JSON string (when visualization is generated)
sqlQuery
(string): The executed SQL queryqueryTitle
(string): Human-readable title for the querydescription
(string): Detailed description of what the query doesuserRequest
(string): Original user request that triggered this queryschema
(array): Column definitions withname
andcolumn_type
for each fielddata
(array): Query result rows as arrays of valuestotalRows
(number): Total number of rows returneduuid
(string): Unique identifier for this query resultvegaSpec
(string): Vega-Lite v5 visualization specification as JSON string (when visualization is generated)
Loading All Results
Query results in cubeSqlApi
toolCall.result.data
are limited to 100 rows by default. Use totalRows
to check if more data is available.sqlQuery
from the results to make a call to the Cube API.
Displaying Charts
The cubeSqlApi tool call returnsvegaSpec
that can be used to display data. Please note, it doesn’t contain data. You need to use data from toolCall.result.data
.
Error Handling
When a tool encounters an error, the result field contains structured error information as a JSON string: Standard Error Format: When an error occurs, theresult
property of the toolCall will contain an error
property.
Code Examples
Error Handling
Invalid or missing JWT token. Verify your token is properly signed and not expired.
Invalid request body or parameters. Check required fields and data types.
Agent or tenant not found. Verify the tenantName and agentId parameters.
Server processing error. Contact support if the issue persists.