API Reference
Complete API documentation for the Metreek Frontend SDK.
MetreekSDK
The global SDK object available on window.MetreekSDK after loading the script.
init(config)
Initialize the SDK with configuration.
TypeScript
MetreekSDK.init(config: MetreekConfig): void
// Example
MetreekSDK.init({
apiKey: 'wk_YOUR_API_KEY',
baseUrl: 'https://api.metreek.com',
customerUid: 'customer-123',
debug: true
})version
Returns the current SDK version string.
JavaScript
console.log(MetreekSDK.version) // "1.0.1"
Chat API
Control the chat component programmatically via MetreekSDK.chat.
chat.open()Opens the chat window.
JavaScript
MetreekSDK.chat.open()
chat.close()Closes the chat window.
JavaScript
MetreekSDK.chat.close()
chat.sendMessage(message: string): Promise<void>Sends a message to the chat.
JavaScript
await MetreekSDK.chat.sendMessage('What are my sales this month?')chat.clear()Clears the chat history.
JavaScript
MetreekSDK.chat.clear()
Widget API
Control widgets via MetreekSDK.widget.
widget.refresh(widgetId: string): Promise<void>Refreshes a specific widget by its ID.
JavaScript
await MetreekSDK.widget.refresh('sales-chart')widget.refreshAll(): Promise<void>Refreshes all widgets on the page.
JavaScript
await MetreekSDK.widget.refreshAll()
Event Methods
on(event: string, callback: Function)Subscribe to an SDK event.
JavaScript
MetreekSDK.on('message:received', (event) => {
console.log('Message:', event.detail.message)
})
MetreekSDK.on('sdk:initialized', () => {
console.log('SDK is ready!')
})off(event: string, callback: Function)Unsubscribe from an SDK event.
JavaScript
const handler = (event) => {
console.log('Message:', event.detail)
}
// Subscribe
MetreekSDK.on('message:received', handler)
// Unsubscribe
MetreekSDK.off('message:received', handler)Internal APIs
These APIs are exposed for debugging purposes. Use with caution in production.
| Global | Description |
|---|---|
window.__METREEK_API_CLIENT__ | Raw API client instance |
window.__METREEK_DEBUG__(bool) | Toggle debug mode at runtime |
window.__METREEK_DEBUG_MODE__ | Current debug mode state |
TypeScript Types
types.d.ts
interface MetreekConfig {
apiKey: string
baseUrl: string
customerUid?: string
theme?: 'light' | 'dark' | 'auto'
locale?: string
debug?: boolean
autoInit?: boolean
chat?: ChatConfig
}
interface ChatConfig {
enabled?: boolean
position?: 'bottom-right' | 'bottom-left' | 'top-right' | 'top-left'
analysisMode?: 'fast' | 'deep'
chatId?: string
}
interface ChatMessage {
id: string
content: string
type: 'user' | 'assistant' | 'system'
timestamp: Date
meta?: {
isStreaming?: boolean
streamProgress?: number
streamStep?: string
}
}
interface WidgetBlock {
blockId: string
label: string
description?: string
viewMappingCodeLanguage?: 'echart-object' | 'javascript'
executionResult?: {
success: boolean
result_data: any[]
data_row_count: number
execution_time: number
}
}
interface ApiResponse<T = any> {
success: boolean
message?: string
data?: T
error?: {
message: string
code?: string
statusCode?: number
}
}