The DMS data service lets you quickly expose data in DMS as APIs. This topic explains the entire API lifecycle: development, testing, publishing, go-live, and deactivation.
Prerequisites
You have added a data source to DMS and enabled either secure management (for instances in stable change or flexible management mode) or security collaboration. For more information, see Add an Alibaba Cloud Database, Add a Third-Party Cloud or Self-Managed Database, Enable Secure Management, and Change Control Mode.
Notes
-
This feature is in private preview and available only to selected users.
-
Modified APIs must be republished to take effect.
-
When you delete an API, it is unpublished and removed from the service development and service management tabs.
-
This action is irreversible, and the related data is unrecoverable. Please proceed with caution.
Service development
The service development tab lets you create, edit, and test APIs. After you publish an API, you can call it from the production environment by using the HTTPS or SSE protocol.
Access the service development tab
Log in to DMS 5.0.
-
From the top navigation bar, choose .
NoteIf you use the console in simplified mode, click the
icon in the upper-left corner and choose . -
Optional: Click the service development tab.
Create a directory
-
Click the
icon to the right of directory and select Create Directory. -
In the Create Directory dialog box, enter a Directory Name.
NoteA directory name can contain Chinese characters, letters, digits, and underscores (_) and must meet the following requirements:
-
Be unique.
-
Start with a letter or a Chinese character.
-
Be 4 to 50 characters long.
-
-
Optional: Enter a business description.
-
Click Confirm.
Create and configure an API
-
Optional: Create a directory.
-
Create an API.
-
To the right of the target directory, click the
icon and select Create API. -
In the Create API dialog box, enter an API Name.
NoteAn API name can contain Chinese characters, letters, digits, and underscores (_) and must meet the following requirements:
-
Be unique.
-
Start with a letter or a Chinese character.
-
Be 4 to 50 characters long.
-
-
Optional: Enter a business description.
-
In the path text box, enter the call path for the API.
NoteThe path becomes part of the API call URL. It must be unique, start with a forward slash (
/), and contain only letters, digits, underscores (_), and hyphens (-). For example:/item/add. -
Click Confirm.
-
-
Configure the API.
-
Click the name of the target API.
-
Configure API parameters.
Parameter
Condition
Description
Category
Parameter
API mode
Select Mode
Required
Supports wizard mode and script mode.
-
wizard mode: Visually define the API's data query by selecting tables and fields.
-
script mode: Define the API's data query by writing SQL scripts and variables.
NoteDue to limitations on special character handling in script mode, the less than sign (
<) and greater than sign (>) must be represented as HTML entities, specifically<and>. Otherwise, parsing errors may occur.
Select Table
data source type
Required
The type of the database to query. Currently, only MySQL and PolarDB for MySQL are supported.
data source name
The database to query.
NoteEnter a keyword to search for databases you have permission to query.
Table Name
wizard mode
The data table to query.
NoteYou can enter a keyword to quickly find the target table.
Select Parameters
Paginate Results
wizard mode
Specifies whether to paginate the response.
Set as Request Parameter
Specifies whether to set the field as a request parameter.
If you select Set as Request Parameter for a field, the field is added to request parameters on the right.
NoteAfter setting a field as a request parameter, you can click request parameters on the right to configure field information.
Set as Response Parameter
Specifies whether to set the field as a response parameter.
If you select Set as Response Parameter for a field, the field is added to response parameters on the right.
NoteAfter setting a field as a response parameter, you can click response parameters on the right to configure field information.
Add to Sort Fields
Click Add next to a field to add it to the sort fields area.
sort fields
sort order
wizard mode
Specifies the sort order. Options are ascending and descending.
Actions
Click Move Up or Move Down to reorder fields. Click Remove to remove the field.
Write Query SQL
script mode
Enter an SQL statement and click auto-parse parameters to generate request and response parameters.
NoteAfter the request and response parameters are generated, you can click request parameters or response parameters on the right to configure field information.
-
-
Optional: Set the execution properties for the API.
Click Properties on the right side of the page and configure the parameters.
Parameter
Description
protocol
The protocol for API calls. Supported values: HTTPS and SSE.
maximum number of returned records
The maximum number of records that an API call can return.
timeout
The timeout for an API call.
return field metadata
Specifies whether to return metadata in the API response.
-
Above the API information, click Save to save the API.
-
Test an API
-
Click the API that you created and configured.
-
Above the API information, click Test.
-
On the API Test page, enter a Value for each request parameter.
For example, for the ARRAY parameter
id_list, enter[1,2]. -
Click Start Test.
-
If the API call succeeds, check the response body to verify the result.
Publish an API
-
Click the API that you have tested.
-
Above the API information, click Publish.
-
In the API publishing request panel, confirm the API's basic information and parameters.
-
Click Confirm and wait for the API to be published.
NoteIf the API is already published, this action overwrites its online configuration.
Clone an API
-
Hover over the target API and click the
icon that appears. -
Click Clone.
This action clones the original API's settings. Make sure that the parameters meet the requirements.
Note-
In the Clone API dialog box, the cloned API's API Name and path are appended with the
_copysuffix. -
Make sure that the API Name is unique across all directories.
-
-
Optional: Based on the parameter requirements and your business needs, modify the API Name, business description, path, and target directory.
-
Click Confirm.
Modify an API
-
Click the name of the target API.
-
Note
Changing the request or response parameters might affect API calls. Adjust the logic for handling request and response parameters in your client application accordingly.
-
Above the API information, click Save to save the API.
Delete an API
To delete an API you no longer need, follow these steps.
If the API is published, you must first take it offline.
-
Hover over the target API and click the
icon that appears. -
Click Delete.
-
In the Delete dialog box, click Confirm Deletion.
The message API deleted successfully appears.
Edit a directory
-
Hover over the target directory and click the
icon that appears. -
Click Edit.
-
In the Edit Directory dialog box, modify the directory information.
-
Click Confirm.
The message Directory edited successfully appears.
Delete a directory
You can delete a directory only if it contains no online APIs.
-
Hover over the target directory and click the
icon that appears. -
Click Delete.
NoteMake sure that the directory does not contain any APIs that are online (with a service status of Running).
-
In the Delete dialog box, click Confirm Deletion.
Service management
The service management tab displays APIs that are published or have been taken offline but not deleted.
Go to the service development tab
Log in to DMS 5.0.
-
From the top navigation bar, choose .
NoteIf you use the console in simplified mode, click the
icon in the upper-left corner and choose . -
Click the service management tab.
Authorize an API
-
In the left-side navigation pane, click API management.
-
On the API management page, find the target API and click authorize in the actions column.
-
In the authorize dialog box, select an Authorized Account and click confirm.
The Authorization successful message appears.
Take an API offline
-
In the left-side navigation pane, click API management.
-
On the API management page, find the target API and click take offline in the actions column.
NoteAfter you take the API offline, its service status changes to Stopped.
Publish an API
-
In the left-side navigation pane, click API management.
-
On the API management page, find the target API and click publish in the actions column.
NoteAfter you publish the API, its service status changes to Running.
Test an API
-
In the left-side navigation pane, click API test.
-
In the API test section, select the target API.
-
In the request parameters section, enter a Value for each request parameter.
-
Click Start Test.
-
If the API call succeeds, check the response body to verify the result.
Call an API
-
In the left-side navigation pane, click API call.
-
View the API call information and call the API based on your business requirements.
Delete an API
If the API is published, you must first take it offline.
-
In the left-side navigation pane, click API management.
-
On the API management page, find the target API and click delete in the actions column.
-
In the Delete dialog box, click Confirm Deletion.
The message API deleted successfully appears.
Appendix
Supported data sources
-
MySQL
Includes RDS MySQL, on-premises self-managed MySQL databases, and MySQL databases from other cloud providers.
-
PolarDB for MySQL
Field information
Request parameters and response parameters generated in script mode do not have an example value, default value, or description.
|
Type |
Parameter |
Description |
|
Request parameters |
Parameter name |
The name of the input parameter. The name can contain letters, digits, underscores (_), and hyphens (-). It must start with a letter or an underscore (_) and be 1 to 50 characters in length. By default, the field name is used. |
|
Bound field |
The name of the corresponding field in the data source. |
|
|
Parameter type |
The data type of the field. Supported types are STRING (string), NUMBER (numeric, including integers and floating-point numbers), BOOLEAN (Boolean), and ARRAY (array of strings, numbers, or booleans). |
|
|
Operator |
The operator that works with the parameter name and its value to filter data. |
|
|
Required |
Specifies whether the parameter is required in the API call. |
|
|
Example value |
An example of a valid parameter value. |
|
|
Default value |
The value used if the parameter is omitted from the call. |
|
|
Description |
A description of the parameter. |
|
|
Response parameters |
Alias |
The name of the parameter in the response. The name can contain letters, digits, underscores (_), and hyphens (-). It must start with a letter or an underscore (_) and be 1 to 50 characters in length. By default, the field name is used. |
|
Bound field |
The name of the corresponding field in the data source. |
|
|
Parameter type |
The data type of the field. Supported types are STRING (string), NUMBER (numeric, including integers and floating-point numbers), BOOLEAN (Boolean), and ARRAY (array of strings, numbers, or booleans). |
|
|
Example value |
An example of the parameter's returned value. |
|
|
Default value |
The default returned value for the parameter. |
|
|
Description |
A description of the parameter. |
Supported SQL
|
Format |
Description |
Example |
|
Standard SQL |
Wrap request parameter names in |
|
|
Pagination parameters |
Two formats are supported:
|
|
|
if tag |
The parameter is added to the query condition only if the condition in the |
|
|
choose tag |
Follows a "when-otherwise" logic. Only the first condition that evaluates to true is included in the query. |
|
|
where tag |
Renders a |
|
|
foreach tag |
Iterates over a collection, such as a list-type request parameter. Important
You must manually change the type of the |
|
Example calls
You can call a deployed data service API using Java code. The service supports both the HTTPS and SSE protocols. The following examples are based on Java 11 or later and require the following dependency:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>2.0.60</version>
</dependency>
Request structure
The request body has the same structure for both HTTPS and SSE calls.
{
"accessKey" : "xxx", # Required. Your credential AccessKey from DMS.
"accessSecret" : "xxx", # Required. Your credential AccessSecret from DMS.
"query" : "call api ${apiPath}", # Required. Replace ${apiPath} with the actual path of your API.
"params" : { # Optional. Fill in based on the actual data types of the parameters.
"key1": value1,
"key2": value2
}
}
|
Parameter |
Type |
Required |
Description |
|
|
String |
Yes |
The AccessKey for the credential that you generated in DMS. |
|
|
String |
Yes |
The AccessSecret for the credential that you generated in DMS. |
|
|
String |
Yes |
The fixed format is |
|
|
Object |
No |
A JSON object that contains key-value pairs for all the request parameters and their values required to call the API. You can omit this field if the API does not require parameters. Important
In the |
Https protocol
This method is suitable for standard query scenarios where you need to retrieve the complete result set in a single response.
import com.alibaba.fastjson.JSON;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class HttpsCallDataServiceApi {
private static final HttpClient httpClient = HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_1_1)
.connectTimeout(Duration.ofSeconds(10))
.build();
public static void main(String[] args) {
try {
String response = callDmsApi();
System.out.println("Response content: " + response);
} catch (Exception e) {
System.err.println("Request failed: " + e.getMessage());
e.printStackTrace();
}
}
public static String callDmsApi() throws Exception {
// Replace with the service endpoint for your region. You can find it on the data service homepage.
String url = "https://onequery-cn-hangzhou.proxy.dms.aliyuncs.com/service/http";
Map<String, Object> requestBody = new HashMap<>();
// Replace ${accessKey} and ${accessSecret} with your DMS credential information, not your RAM AccessKey pair.
requestBody.put("accessKey", "${accessKey}");
requestBody.put("accessSecret", "${accessSecret}");
// Replace ${apiPath} with the actual path of your API.
requestBody.put("query", "call api ${apiPath}");
// Build the params object. This object can be omitted if no parameters are needed.
Map<String, Object> params = new HashMap<>();
// In an actual call, replace the sample parameters with your actual parameters.
// Numeric parameter
params.put("userId", 111);
// String parameter
params.put("name", "abc");
// Boolean parameter
params.put("isActive", true);
// List parameter
params.put("tags", Arrays.asList(1, 2, 3));
// If the params object is empty, you do not need to add it to the request body.
requestBody.put("params", params);
String jsonBody = JSON.toJSONString(requestBody);
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(url))
.header("Content-Type", "application/json;charset=utf-8")
.method("POST", HttpRequest.BodyPublishers.ofString(jsonBody))
.timeout(Duration.ofSeconds(30))
.build();
HttpResponse<String> response = httpClient.send(request,
HttpResponse.BodyHandlers.ofString());
// Check the response status code.
if (response.statusCode() != 200) {
throw new RuntimeException("HTTP error code: " + response.statusCode());
}
return response.body();
}
}
After a successful call, the service returns a JSON object that contains the complete result set.
|
Field |
Type |
Description |
|
|
String |
The result type. |
|
|
String |
A unique query ID used for troubleshooting. |
|
|
Number |
The number of data rows in the result set. |
|
|
Array |
The result set data. Each element is a JSON object that represents a data row. |
|
|
Array |
A list of field metadata. |
|
|
Object |
Additional attributes related to the query, such as the execution time |
|
|
String |
If the |
{
"attributes": {
"QueryTime": 13
},
"columnMetas": [
{
"autoIncrement": true,
"catalogName": "test_schema",
"columnLabel": "id",
"columnName": "id",
"columnTypeName": "BIGINT UNSIGNED",
"nullable": false,
"precision": 20,
"scale": 0,
"schemaName": "",
"tableName": "test_table"
},
{
"autoIncrement": false,
"catalogName": "test_schema",
"columnLabel": "gmt_create",
"columnName": "gmt_create",
"columnTypeName": "DATETIME",
"nullable": true,
"precision": 19,
"scale": 0,
"schemaName": "",
"tableName": "test_table"
}
],
"count": 1,
"message": null,
"rows": [
{
"id": 1,
"gmt_create": "2025-12-17 16:23:53"
}
],
"sessionId": 1560935281,
"traceId": "HZ1560935281OCQJOTKW",
"type": "RESULTSET"
}
Sse protocol
This method is suitable for long-running queries or scenarios where you need to process results incrementally. The client receives a stream of messages, with each message containing an event.
import com.alibaba.fastjson.JSON;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;
public class SseCallDataServiceApi {
private static final HttpClient httpClient = HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_1_1)
.connectTimeout(Duration.ofSeconds(10))
.build();
public static void main(String[] args) {
try {
callDmsApiSse();
} catch (Exception e) {
System.err.println("SSE request failed: " + e.getMessage());
e.printStackTrace();
}
}
public static void callDmsApiSse() throws Exception {
// Replace with the service endpoint for your region. You can find it on the data service homepage.
String url = "https://onequery-cn-hangzhou.proxy.dms.aliyuncs.com/service/sse";
Map<String, Object> requestBody = new HashMap<>();
// Replace ${accessKey} and ${accessSecret} with your DMS credential information, not your RAM AccessKey pair.
requestBody.put("accessKey", "${accessKey}");
requestBody.put("accessSecret", "${accessSecret}");
// Replace ${apiPath} with the actual path of your API.
requestBody.put("query", "call api ${apiPath}");
// Build the params object. This object can be omitted if no parameters are needed.
Map<String, Object> params = new HashMap<>();
// In an actual call, replace the sample parameters with your actual parameters.
// Numeric parameter
params.put("userId", 111);
// String parameter
params.put("name", "abc");
// Boolean parameter
params.put("isActive", true);
// List parameter
params.put("tags", Arrays.asList(1, 2, 3));
// If the params object is empty, you do not need to add it to the request body.
requestBody.put("params", params);
String jsonBody = JSON.toJSONString(requestBody);
// Build the HttpRequest and add the necessary headers for SSE.
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(url))
.header("Content-Type", "application/json;charset=utf-8")
.header("Accept", "text/event-stream") // Specify that you accept an SSE stream.
.header("Cache-Control", "no-cache")
.method("POST", HttpRequest.BodyPublishers.ofString(jsonBody))
.timeout(Duration.ofMinutes(5)) // SSE connections are typically long-lived, so set a longer timeout.
.build();
// Send the request and use ofLines to process the streaming response.
System.out.println("Starting to receive SSE stream...");
httpClient.sendAsync(request, HttpResponse.BodyHandlers.ofLines())
.thenAccept(response -> {
if (response.statusCode() != 200) {
System.err.println("HTTP error: " + response.statusCode());
return;
}
// Parse the response content line by line.
try (Stream<String> lines = response.body()) {
lines.forEach(line -> {
if (line.startsWith("data:")) {
// Extract the content after "data:".
String data = line.substring(5).trim();
System.out.println("Data received: " + data);
} else if (line.startsWith("error:")) {
System.err.println("Error received: " + line);
}
});
}
})
.join(); // For demonstration purposes, the main thread waits for the async operation to complete.
}
}
The SSE response is an event stream. Each event starts with data:, followed by a JSON object. You must check the type field in the JSON object to determine the event type.
|
|
Event type |
Description |
|
|
Metadata event |
The first valid event in the stream. It contains the field metadata for the result set. |
|
|
Data row event |
Contains a batch of (or a single) result data rows. A single query might trigger multiple |
|
|
End-of-file event |
Stands for End-Of-File and indicates that all data has been sent. |
|
|
Error event |
Indicates that an error occurred during the query and contains detailed error information. |
JSON examples for each event type:
-
META event:
{ "attributes": { "QueryTime": 13 }, "columnMetas": [ { "autoIncrement": true, "catalogName": "test_schema", "columnLabel": "id", "columnName": "id", "columnTypeName": "BIGINT UNSIGNED", "nullable": false, "precision": 20, "scale": 0, "schemaName": "", "tableName": "test_table" }, { "autoIncrement": false, "catalogName": "test_schema", "columnLabel": "gmt_create", "columnName": "gmt_create", "columnTypeName": "DATETIME", "nullable": true, "precision": 19, "scale": 0, "schemaName": "", "tableName": "test_table" } ], "message": null, "resultIndex": 1, "sessionId": 1560946281, "traceId": "HZ1560946281OGMJSFEX", "type": "META" } -
ROWS event:
{ "attributes": {}, "count": 1, "message": null, "resultIndex": 1, "rows": [ [ 1, "2025-12-17 16:23:53" ] ], "sessionId": 1560946281, "traceId": "HZ1560946281OGMJSFEX", "type": "ROWS" } -
EOF event:
{ "attributes": { "TotalRowCount": 1 }, "lastResult": true, "message": null, "resultIndex": 1, "sessionId": 1560946281, "traceId": "HZ1560946281OGMJSFEX", "type": "EOF" } -
ERROR event:
{ "attributes": {}, "message": "[3050: Building Plan Error] PlanDAG build failed: The API does not exist. Publish it and try again.", "sessionId": 1560944281, "traceId": "HZ1560944281VWJLYLAK", "type": "ERROR" }