OpenAPI SDK¶
OpenAPI SDK is used to call Guance Open API in different programming languages. The SDK encapsulates interfaces by module and handles URL concatenation, DF-API-KEY authentication, request parameter encoding, common response structures, and SDK errors.
The current SDK list provides GitHub repository links. You can choose the corresponding SDK based on your development language.
| Development Language | SDK | Description |
|---|---|---|
| Python | Python SDK | Python SDK for Guance OpenAPI |
| JavaScript / TypeScript | JavaScript / TypeScript SDK | Node.js 18+ / TypeScript SDK |
| Java | Java SDK | Java SDK for Guance OpenAPI |
| PHP | PHP SDK | PHP 8.1+ SDK |
Quick Start¶
After preparing according to the following steps, you can use the SDK to call Open API.
1 Confirm Permissions¶
Creating and managing API Keys requires Administrator or Owner permissions for the workspace. It is recommended to assign roles to API Keys based on the principle of least privilege, avoiding permissions that exceed actual needs.
2 Create an API Key¶
- Enter the Guance workspace.
- Click Management > API Key Management in the left navigation bar.
- Click Create Key in the upper right corner.
- Configure the name, role, and remarks.
- After successful creation, go to the API Key details page and copy the Key (Secret).
For detailed instructions, see API Keys Management.
3 Select Open API Endpoint¶
The Open API Endpoint needs to be passed during SDK initialization. Common SaaS Endpoints are as follows:
For more site Endpoints, see Overview. For private deployment versions, use the actual deployed Endpoint.
4 Set Environment Variables¶
It is recommended to first write the Endpoint and API Key into environment variables, which can be reused directly in subsequent examples for various languages.
export DF_OPENAPI_ENDPOINT="https://openapi.guance.com"
export DF_API_KEY="Replace with your API Key"
5 Select Language and Prepare SDK¶
The SDK repository already contains the dist artifact. When integrating into a business project, you can use @guance/openapi-sdk as per the repository README.
To install into the local Maven repository, execute:
6 Initialize Client¶
When initializing the Client, at least the Endpoint and API Key need to be passed.
First Call¶
It is recommended to choose a read-only interface for the first call, such as "Get Dashboard List". This interface corresponds to Dashboard List in the Open API documentation:
When calling with the SDK, only business parameters need to be passed; the DF-API-KEY will be automatically placed in the request header by the SDK.
If the call is successful, success in the response will be true, code will be 200, and the business data will be located in content.
How to Fill Parameters¶
When reading each interface document, first look at the request method and path below the interface title, then look at the parameter location. Open API only uses GET and POST request methods: GET is used for data query and retrieval, POST is used for data changes such as creation, modification, and deletion.
| Parameter Location in Interface Documentation | SDK Parameter | Example |
|---|---|---|
| Query Request Parameters | query |
{"pageIndex": 1, "pageSize": 10} |
| Path Parameters | path |
{"dashboard_uuid": "dsbd_xxxx32"} |
| Body Request Parameters | body |
{"name": "demo workspace"} |
| Additional Request Headers | headers |
{"X-Source": "internal-tool"} |
Query Parameters¶
Query parameters are encoded into the URL query string. Taking Get Dashboard List as an example:
Path Parameters¶
Path parameters replace variables in the interface path. Taking Get Specified Dashboard as an example:
Body Parameters¶
Body parameters are used for POST requests. Taking Modify Current Workspace as an example:
How to Interpret the Response¶
Open API uses a unified response structure. Common fields are as follows:
| Field | Description |
|---|---|
code |
Returned status code, consistent with HTTP status codes, fixed at 200 when there is no error |
content |
Business data, specific type determined by the interface |
pageInfo |
Pagination information for list interfaces |
errorCode |
Error status code, empty indicates no error |
message |
Error description |
success |
Whether the interface call was successful |
traceId |
Request trace ID, used for troubleshooting |
List interfaces typically return both content and pageInfo. For example, pageInfo.totalCount represents the total number of data items matching the criteria.
How to Handle Errors¶
The SDK wraps Open API errors into exception types corresponding to the language. When troubleshooting, first check the HTTP status code, errorCode, message, and traceId.
try {
ApiResponseEnvelope response = client.board.list(
RequestOptions.withQuery(Map.of("pageIndex", 1, "pageSize", 10))
);
} catch (ApiException error) {
System.out.println(error.httpStatus);
System.out.println(error.errorCode);
System.out.println(error.traceId);
System.out.println(error.envelope.rawBody);
}
Common errors and limitations:
| Scenario | Description |
|---|---|
| Invalid API Key | Returns ft.InvalidAPIKey |
| API Key Level Rate Limit | The same API Key can make up to 200 requests per minute. Triggering this limit returns ft.TriggerApiAkCurrentLimiting |
| Workspace Level Rate Limit | The same workspace supports up to 1000 Open API calls per minute. Triggering this limit returns ft.TriggerApiWorkspaceCurrentLimiting |
For more information, see Common Response Structure, Common Error Definitions, and Usage Limits.
Data Query Example¶
If you need to query Metrics, Logs, Events, and other data, you can use the DQL data query interface. For DQL syntax, see DQL Query. For interface details, see DQL Data Query.
The following example queries the cpu metric:
const response = await client.queryData.queryDataV1({
body: {
queries: [
{
qtype: "dql",
query: {
q: "M::`cpu`:(avg(`usage_idle`))",
timeRange: [1708911106000, 1708912906999],
interval: 10,
maxPointCount: 720,
tz: "Asia/Shanghai",
},
},
],
fieldTagDescNeeded: false,
},
});
console.log(response.content);
RequestOptions req = RequestOptions.create();
req.bodyJson = """
{
"queries": [
{
"qtype": "dql",
"query": {
"q": "M::`cpu`:(avg(`usage_idle`))",
"timeRange": [1708911106000, 1708912906999],
"interval": 10,
"maxPointCount": 720,
"tz": "Asia/Shanghai"
}
}
],
"fieldTagDescNeeded": false
}
""";
QueryDataContent content = client.queryData.queryDataV1Content(req);
System.out.println(content.data);
$response = $client->queryData->queryDataV1([
'body' => [
'queries' => [[
'qtype' => 'dql',
'query' => [
'q' => 'M::`cpu`:(avg(`usage_idle`))',
'timeRange' => [1708911106000, 1708912906999],
'interval' => 10,
'maxPointCount' => 720,
'tz' => 'Asia/Shanghai',
],
]],
'fieldTagDescNeeded' => false,
],
]);
var_dump($response->content);
Note
When performing data queries via Open API, the default role is administrator, but data access rules may still apply.
Troubleshooting Checklist¶
If the first call fails, check in the following order:
- Is
DF_OPENAPI_ENDPOINTthe Open API address corresponding to the current site? - Is
DF_API_KEYfilled with the Key (Secret) from the API Key details page, not the Key ID? - Does the workspace to which the API Key belongs match the workspace you want to query or modify?
- Does the API Key role have the permissions required for the target interface?
- Are Query, Path, and Body parameters placed in the corresponding SDK parameters?
- For list interfaces, are reasonable
pageIndexandpageSizeset? - Has the API Key or workspace level rate limit been triggered?
- Is the
traceIdin the error message recorded for subsequent troubleshooting?
Related Documentation¶
- Overview: View Endpoint, authentication methods, and common response structure.
- Common Request Parameters: View common Headers.
- Request Examples: View raw curl call methods for GET / POST.
- API Keys Management: View API Key creation and permission configuration.
- Open API Authentication Instructions: View Open API authentication methods, routing specifications, and return results.
- DQL Query: View DQL syntax instructions.