Skip to main content

Custom API

Tencent Cloud WeDa low-code custom API supports calling third-party service interfaces or using code to implement business logic. Developers can use the API in applications, workflows, or other custom APIs.

Custom API Method Implementation

Currently, custom APIs support three methods to implement custom business logic:

  • HTTP Request: Calls external services using HTTP. The method can be configured by simply setting the HTTP request URL, method, parameters, etc.
  • Custom Code: Integrates common NPM packages, data models, APIs, and other methods, only supports the JS development language, and can be used to implement custom business logic.
  • Cloud Development Cloud Function: Users can create and use cloud development cloud functions to implement complete server-side functionality and bind and invoke them via custom API methods.

For details on how to implement custom APIs, see Creating Custom APIs.

Custom API Usage

Currently, custom APIs can be used in both the Application Editor and custom code, supporting two methods: visual and custom code.

Invoking via Component Behavior

Enter the application editor, where you can call API methods in component behavior events. For details, see Behavior Events - Execution Actions.

Invocation via Low-code Editor

Enter the Application Editor, where it can be used in the application's low-code editor and the component code of WeDa components. For details, see the Code Editor. Sample Code:

export default async function ({ event, data }) {
const result = await app.cloud.callConnector({
name: "custom API identifier",
methodName: "method identifier",
params: {}, // method input parameters
});
}

Invoking in Custom Code

In custom APIs, you can call other custom APIs via the custom code approach. For details, see Custom Code. Sample Code:

module.exports = async function (params, context) {
const result = await context.callConnector({
name: "custom API identifier",
methodName: "method identifier",
params: {}, // method input parameters
});

return {
_id: "123456",
};
};

Using the custom code capability in custom APIs enables developers to implement different business scenarios. Developers can invoke external HTTP services, WeDa data models, APIs, custom APIs, or CloudBase cloud functions using the built-in APIs or npm packages in custom code.

Custom Code

Custom code is NodeJS code running on the server side, with CloudBase cloud functions as its underlying infrastructure. The following shows the main components of custom code:

/**
* You can reference built-in NPM packages here, for example:
**/
const fetch = require("node-fetch");

/**
* Within the function body, you can implement business logic.
**/
module.exports = async function (params, context) {
// `params` is the structure defined for input parameters
// `context` is a platform built-in object that contains built-in APIs.

// Return the result of this method here, which must map to the structure defined in the output parameters.
return {
_id: 123456,
};
};

Use Case Example

Make an HTTP Request

Developers can use the node-fetch library to make HTTP requests. For details, please refer to the node-fetch documentation.

const fetch = require("node-fetch");

module.exports = async function (params, context) {
const response = await fetch("https://reqres.in/api/users");
const result = await response.json();

// Return the result of this method here, which must map to the structure defined in the output parameters.
return {
_id: "123456",
};
};

Data Model

Data Model Interface (Legacy)

Developers can use the context.callModel method to operate the data model. For complete methods, please refer to Data Model Methods.

module.exports = async function (params, context) {
const result = await context.callModel({
name: "example_xxxxxxx", // Data model identifier, which can be viewed on the "Data Source - Data Model" list page
methodName: "wedaCreate", // Data model method identifier. For supported methods, navigate to the details page of any data model under "Data Source - Data Model" to view methods supported by the current model.
params: {}, // input parameters for the data model method
});

// Return the result of this method here, which must map to the structure defined in the output parameters.
return {
_id: "123456",
};
};
Data Model v2 Interface

The current data model has been upgraded to v2.

Developers can use the $w.cloud.callDataSource method to operate the data model. For complete methods, please refer to Data Model v2 Methods.

export default async function ({ event, data }) {
try {
const data = await $w.cloud.callDataSource({
dataSourceName: "sjmx_ftf41oj",
methodName: "wedaCreateV2",
params: {
data: {
email: "bar@weda.io",
name: "foo",
},
},
});
console.log("Request result", data); // {"id": "f8f6930864c11bde006ff6c4662f9bf6"}
} catch (e) {
console.log("Error code", e.code, "Error message", e.message);
}
}

API

Developers can use context.callConnector to call API methods:

module.exports = async function (params, context) {
const result = await context.callConnector({
name: "API identifier",
methodName: "method identifier",
params: {}, // method input parameters
});

// Return the result of this method here, which must map to the structure defined in the output parameters.
return {
_id: "123456",
};
};

Cloud Development Cloud Functions

Developers can use context.app.callFunction to call Cloud Development Cloud Functions in the same environment:

module.exports = async function (params, context) {
const result = await context.app.callFunction({
name: "Cloud Development Cloud Function name",
data: {}, // method input parameters
});

// Return the result of this method here, which must map to the structure defined in the output parameters.
return {
_id: 123456,
};
};

! This method can only call Cloud Functions within the same Cloud Development environment as WeDa.

If developers have their own databases (Tencent Cloud or self-owned databases), they can use Cloud Development Cloud Functions to implement database connections and read/write operations. Create new cloud functions in the same Cloud Development environment as WeDa, as shown in the following example:

const mysql = require("mysql2/promise");
exports.main = async (event, context) => {
try {
const connection = await mysql.createConnection({
host: process.env.HOST,
user: process.env.USERNAME,
password: process.env.PASSWORD,
port: process.env.PORT,
database: process.env.DB,
});
console.log("Connected");
const [rows, fields] = await connection.execute(
"SELECT * FROM `weda_model_example`;"
);
// Here, the returned data can be processed.
return rows;
} catch (err) {
console.log("Connection error", err);
return err;
}
};

Then, in custom code, call the aforementioned cloud function, referring to the usage sample of context.app.callFunction above.

Cloud Development Database

Developers can use context.database to operate the Cloud Development Database. Refer to the Cloud Development Database API.

module.exports = async function (params, context) {
const result = await context.database.collection("database collection name").get();

// Return the result of this method here, which must map to the structure defined in the output parameters.
return {
_id: 123456,
};
};

! This method can only operate databases within the WeDa Cloud Development environment and cannot perform cross-environment database operations.