OpenAPI SDK¶
The OpenAPI SDK is used to call the Guance Open API in different programming languages. The SDK wraps the APIs by module and handles URL concatenation, DF-API-KEY authentication, request parameter encoding, the common response structure, and SDK errors.
The current SDK list provides GitHub repository links. You can choose the SDK that matches your programming language.
| 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 completing the following steps, you can use the SDK to call the Open API.
1 Confirm Permissions¶
Creating and managing API Keys requires Administrator or Owner permissions in the workspace. It is recommended to assign the API Key a role based on the principle of least privilege, avoiding more permissions than actually needed.
2 Create an API Key¶
- Go to the Guance workspace.
- In the left navigation bar, click Manage > API Key Management.
- Click Create Key in the upper-right corner.
- Configure the name, role, and notes.
- After it is created, go to the API Key details page and copy the Key (secret).
For details, see API Key Management.
3 Select an Open API Endpoint¶
You need to pass the Open API Endpoint when initializing the SDK. Common SaaS Endpoints are shown below:
For Endpoints of other sites, see Overview. For private deployments, use the Endpoint of your actual deployment.
4 Set Environment Variables¶
It is recommended to write the Endpoint and API Key into environment variables first, so that all the language examples below can reuse them directly.
export DF_OPENAPI_ENDPOINT="https://openapi.guance.com"
export DF_API_KEY="Replace with your API Key"
5 Choose a Language and Prepare the SDK¶
The SDK repository already contains the dist artifacts. When integrating it into a business project, use @guance/openapi-sdk as described in the repository README.
To install it to your local Maven repository, run:
6 Initialize the Client¶
To initialize the Client, you must pass at least the Endpoint and API Key.
First Call¶
For your first call, choose a read-only interface, such as "Get Dashboard List". This interface corresponds to Get Dashboard List in the Open API documentation:
When calling from the SDK, you only need to pass business parameters; the SDK automatically adds DF-API-KEY to the request header.
If the call succeeds, success in the response is true, code is 200, and the business data is in content.
How to Fill In Parameters¶
When reading an API document, first check the request method and path below the interface title, and then check where the parameters are located. The Open API uses only two request methods, GET and POST: GET is for data query and retrieval, and POST is for data changes such as creating, modifying, and deleting.
| Location in the API 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. Take Get Dashboard List as an example:
Path Parameters¶
Path parameters replace the variables in the interface path. Take Get the Specified Dashboard as an example:
Body Parameters¶
Body parameters are used for POST requests. Take Modify the Current Workspace as an example:
Understanding the Response¶
The Open API uses a unified response structure. Common fields are listed below:
| Field | Description |
|---|---|
code |
Returned status code, the same as the HTTP status code; it is always 200 when there is no error |
content |
Business data; the specific type is determined by the interface |
pageInfo |
Pagination information for list interfaces |
errorCode |
Error status code; empty means no error |
message |
Error description |
success |
Whether the interface call succeeded |
traceId |
Request trace ID, used for troubleshooting |
List interfaces usually return both content and pageInfo. For example, pageInfo.totalCount indicates the total number of records that meet the conditions.
Error Handling¶
The SDK wraps Open API errors into the exception type of the corresponding 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 limiting | A single API Key supports up to 200 requests per minute; once triggered, returns ft.TriggerApiAkCurrentLimiting |
| Workspace-level rate limiting | A single workspace supports up to 1000 Open API calls per minute; once triggered, returns ft.TriggerApiWorkspaceCurrentLimiting |
For more information, see Common Response Structure, Common Error Definitions, and Usage Limits.
Query Data Example¶
To query data such as metrics, logs, and events, use the DQL data query interface. For DQL syntax, see DQL Query; for the interface, 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
Open API data queries default to the Administrator role and may still be subject to Data Access rules.
Troubleshooting Checklist¶
If the first call fails, check the following in order:
- Whether
DF_OPENAPI_ENDPOINTis the Open API address corresponding to the current site; - Whether
DF_API_KEYis set to the Key (secret) on the API Key details page, not the Key ID; - Whether the workspace that the API Key belongs to is the workspace you want to query or modify;
- Whether the API Key role has the permissions required by the target interface;
- Whether the Query, Path, and Body parameters are placed in the corresponding SDK parameters;
- Whether the list interface has reasonable
pageIndexandpageSizeset; - Whether API Key-level or workspace-level rate limiting was triggered;
- Whether the
traceIdin the error message has been recorded for further troubleshooting.
Related Documentation¶
- Overview: View the Endpoint, authentication methods, and common response structure.
- Common Request Parameters: View common headers.
- Request Example: View raw curl calls for GET / POST.
- API Key Management: View API Key creation and permission configuration.
- Open API Authentication: View Open API authentication methods, routing rules, and return results.
- DQL Query: View DQL syntax.