APIs
APIs provide a flexible way to call the V2 APIs connector. For ways to create V2 APIs, refer to the documentation.
Note: info
v3 version uses the open capability of HTTP API when calling general APIs.
:::
API Usage Reference
interface apis {
[apisLabel: string]: {
[
method: "request" | "post" | "get" | "head" | "patch" | "delete" | "put"
]: (
callApiOptions: ICallApiOptions,
opts?: IRequestOptions
) => Promise<ResponseObject["data"]>;
};
}
The APIs module supports the following HTTP methods:
| Method Name | Description | Sample Code |
|---|---|---|
| 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) |
Note: tip apisLabel is the APIs identification, generated when creating a V2 version APIs connector. You can view it in basic information. :::
参数
callApiOptions
ICallApiOptions
opts
IRequestOptions
Request object parameter, normally does not need to pass in, includes url, method, headers, body, as a supplement to customization needs, will override parameters with the same name in callApiOptions
返回
res
ResponseObject['data']
data content in the HTTP API response
示例
import cloudbase from "@cloudbase/js-sdk";
Initializing a CloudBase app
const app = cloudbase.init({
env: "your-env-id", // Replace this value with your environment ID
region: "ap-shanghai", // Region, defaults to Shanghai
accessKey: "", // Fill in the generated Publishable Key
});
// Get the apis instance
const apis = app.apis;
// Call the methodLabel method of the connector named "apisLabel" APIs with a POST call
async function getUserProfile(userId: string) {
try {
const result = await apis["apisLabel"].post({
path: "methodLabel",
body: { userId },
});
console.log("call success:", result);
return result;
} catch (error) {
console.error("call failed:", error);
throw error;
}
}
// Call the methodLabel method of the connector named "apisLabel" APIs 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 the HTTP method
path: "methodLabel", // API path
headers: {
"X-Custom-Header": "value",
},
body: {
data: "custom data",
},
});
return result;
}
// Allowed 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 failure:", error);
}
throw error;
}
}