APIs
APIs provide a flexible way to invoke the V2 version APIs Connector. For creating V2 version APIs, see the documentation.
Note
The v3 version uses the open capabilities of the HTTP API when calling general APIs.
APIs Usage Reference
interface apis {
[apisLabel: string]: {
[
method: "request" | "post" | "get" | "head" | "patch" | "delete" | "put"
]: (
callApiOptions: ICallApiOptions,
opts?: IRequestOptions
) => Promise<ResponseObject["data"]>;
};
}
APIs module supports the following HTTP methods:
| Method | Description | Example |
|---|---|---|
| get | GET request | apis["apisLabel"].get(callApiOptions) |
| post | POST request | apis["apisLabel"].post(callApiOptions) |
| put | PUT request | apis["apisLabel"].put(callApiOptions) |
| delete | DELETE request | apis["apisLabel"].delete(callApiOptions) |
| head | HEAD request | apis["apisLabel"].head(callApiOptions) |
| patch | PATCH request | apis["apisLabel"].patch(callApiOptions) |
| request | Custom method request | apis["apisLabel"].request(callApiOptions) |
Tip
apisLabel is the APIs identifier, generated when creating the V2 version APIs Connector, and can be found in the connector's basic information.
参数
callApiOptions
ICallApiOptions
opts
IRequestOptions
Request object parameters, generally not required to be passed, include url, method, headers, and body. They serve as supplementary options for customization needs and will override parameters of the same name in callApiOptions.
返回
res
ResponseObject['data']
data content returned by the HTTP interface
示例
import cloudbase from "@cloudbase/js-sdk";
// Initialize the CloudBase app
const app = cloudbase.init({
env: "your-env-id", // Replace with your Environment ID
region: "ap-shanghai", // region, defaults to Shanghai
accessKey: "", // fill in the generated Publishable Key
});
// obtain apis instance
const apis = app.apis;
// Call the methodLabel method of the API connector named "apisLabel" using a POST call
async function getUserProfile(userId: string) {
try {
const result = await apis["apisLabel"].post({
path: "methodLabel",
body: { userId },
});
console.log("Call successful:", result);
return result;
} catch (error) {
console.error("Call failed:", error);
throw error;
}
}
// Call the methodLabel method of the API connector named "apisLabel" using a GET call
async function getProductList(category: string) {
try {
const result = await apis["apisLabel"].get({
path: "methodLabel",
body: { category },
});
return result;
} catch (error) {
console.error("Call failed:", error);
throw error;
}
}
// Custom request configuration
async function customApiCall() {
const result = await apis["apisLabel"].request({
method: "PUT", // Specify HTTP method
path: "methodLabel", // API path
headers: {
"X-Custom-Header": "value",
},
body: {
data: "custom data",
},
});
return result;
}
// Using different HTTP methods
async function multiMethodExample() {
// POST request
const createResult = await apis["apisLabel"].post({
path: "methodLabel",
body: { name: "New Item" },
});
// GET request
const getResult = await apis["apisLabel"].get({
path: "methodLabel",
});
// DELETE request
const deleteResult = await apis["apisLabel"].delete({
path: "methodLabel",
});
}
async function safeApiCall() {
try {
const result = await apis["apisLabel"].post({
path: 'methodLabel'
body: { data: "test" },
});
return result;
} catch (error) {
// Handle API call errors
if (error.code === "INVALID_PARAMS") {
console.error("Parameter error:", error.msg);
} else {
console.error("API call failed:", error);
}
throw error;
}
}