API result filters

Updated at:

The default response structure of an API may not be suitable for every use case. To meet specific requirements, you can configure filters to transform the original response structure. This reduces API integration and custom development costs. This topic describes how to use API result filters.

Usage

After a filter is configured and enabled, it automatically transforms the response format.

Example result

The following example shows the original response structure of an API.

Parameter

Description

code

The status code of the response. Valid values: SUCCESS and ERROR.

data

The returned data. The structure can vary based on the data source type. It is usually an ArrayList or a jsonObject.

traceId

The unique ID for tracking this API call.

message

If the `code` is ERROR, this field returns the reason for the fault.

An API user might want to remove the encapsulation of fields, such as `code` and `traceId`, and return only the content of the `data` field.

After the filter is configured, the response structure is as follows:

The response contains only the content of the `data` field. You do not need to manually transform it.

Configuration methods

Method 1: Configure a filter during API development

You can configure a filter during API development. Once configured, the filter applies to all API calls.

Procedure

On the API Configuration and Testing page in step 3 of the API creation/editing process, click the Filter button above Response Content to enable the filter.

You can set up a filter in two ways:

  • Format processing: Processes the response in a predefined way. You can filter out the `code`, `message`, or `traceId` fields. You can also choose to return only the content of the `data` field. You can select multiple options.

  • Custom logic: Use a custom script to transform the entire structure of the response data. Currently, JavaScript (JS) functions are supported. Support for more scripting languages will be added in the future. The following code is an example of a JS function script for custom logic:

    function filter(response) {
      // Get the first group of data from the response data.
      var data = response.data[0];
      // Concatenate the certificate number and customer name, then return the result.
      return data.certificate_number + " : " + data.customer_name;
    }

    The transformation result of the preceding script is as follows:

After you complete the settings, the filter is enabled. To disable the filter, turn off the filter button.

Method 2: Configure a filter for a granted API

You can set a separate filter for a granted API. This filter takes effect only when the API is called by the authorized application. It does not affect the API's default response structure or calls from other applications.

Note

If a filter is configured during API development and another filter is enabled for the application authorization, both filters take effect. The application authorization filter processes the response that was already transformed by the development-stage filter. The final result is then returned to the caller.

Procedure

  1. On the Granted APIs list page, find the authorized API. In the Actions column, click Test to open the test page for the granted API.

  2. On the Test page for the granted API, click the Filter button to configure the filter. The configuration method is the same as that for filters configured during API development.