Implementation:Apache Druid Api Singleton
| Knowledge Sources | |
|---|---|
| Domains | Web_Console, HTTP_Client |
| Last Updated | 2026-02-10 10:00 GMT |
Overview
Api is a singleton class that provides a centralized Axios HTTP client instance for all Druid API communication in the web console.
Description
The class exposes a static AxiosInstance (`Api.instance`) that is initialized once during application startup with configurable base URL, custom headers, and a BigInt-safe JSON response transformer. It installs a response interceptor that extracts meaningful error messages from Druid API error responses (preferring the `message` field from the response body over the generic Axios status code message). The class also provides utility methods for URL-encoding paths with special characters and detecting network errors.
Usage
Used as the sole HTTP client throughout the Druid web console; every API call to the Druid cluster passes through Api.instance. It is initialized in the entry point (entry.tsx) with configuration from the console config object.
Code Reference
Source Location
- Repository: Apache Druid
- File: web-console/src/singletons/api.ts
- Lines: 1-81
Signature
export class Api {
static instance: AxiosInstance;
static initialize(config?: CreateAxiosDefaults): void;
static getDefaultConfig(): CreateAxiosDefaults;
static encodePath(path: string): string;
static isNetworkError(e: Error): boolean;
}
Import
import { Api } from './singletons/api';
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| config | CreateAxiosDefaults | No | Axios configuration including baseURL, headers, and transformResponse (passed to initialize) |
Outputs
| Name | Type | Description |
|---|---|---|
| Api.instance | AxiosInstance | The initialized Axios HTTP client instance used for all API requests |
| Api.getDefaultConfig() | CreateAxiosDefaults | Returns default Axios configuration with BigInt-safe JSON parsing |
| Api.encodePath(path) | string | URL-encodes special characters in a path segment |
| Api.isNetworkError(e) | boolean | Returns true if the error represents a network connectivity failure |
Usage Examples
Initializing and using the API singleton
// Initialization (in entry.tsx)
const apiConfig = Api.getDefaultConfig();
apiConfig.baseURL = '/proxy';
Api.initialize(apiConfig);
// Usage throughout the app
const resp = await Api.instance.get('/druid/coordinator/v1/datasources');
const datasources = resp.data;