mini
WeChat Mini Program
In WeChat Mini Programs, you can access CloudBase services via the built-in wx.cloud.callContainer method. Before using this method, ensure your Tencent Cloud account and Mini Program have completed the association process.
// Ensure that wx.cloud.init has been called in onLaunch to initialize the environment (any environment is acceptable; you may leave it blank)
const res = await wx.cloud.callContainer({
config: {
env: env: "Fill in the Cloud Environment ID", // WeChat Cloud Run environment ID
},
path: path: "/xxx", // Fill in the business-defined path and parameters. The root directory is /
method: method: "POST", // Select the corresponding method based on your business development.
header: {
"X-WX-SERVICE": "xxx", // Fill in the service name (WeChat Cloud Run - Service Management - Service List - Service Name)
// Other header parameters
},
// dataType:'text', // By default, if left blank, the SDK will parse the returned result in JSON format. If you do not want the SDK to parse it automatically, you can set it to 'text'.
// Other parameters are the same as wx.request
});
console.log(res);
Before using it as described above, you need to execute wx.init in the WeChat Mini Program's app.js file, as shown in the following code:
App({
async onLaunch() {
// Before using callContainer, you must initialize it. This only needs to be done once globally.
wx.cloud.init();
// The following request can be used anywhere on the page
const result = await wx.cloud.callContainer({
config: {
env: env: "prod-01", // WeChat Cloud Run environment ID
},
path: path: "/", // Fill in the business-defined path and parameters. The root directory is /
method: method: "GET", // Select the corresponding method based on your business development.
header: {
"X-WX-SERVICE": "xxx", // Fill in the service name (WeChat Cloud Run - Service Management - Service List - Service Name)
},
// dataType:'text', // By default, if left blank, the SDK will parse the returned result in JSON format. If you do not want the SDK to parse it automatically, you can set it to 'text'.
});
console.log(result);
},
});
For more information on using callContainer to access CloudBase in Mini Programs, refer to:
Advantages of Using wx.cloud.callContainer VS. wx.request
- No public network traffic is consumed; communication between the frontend and backend occurs via the private network.
- Innate immunity to DDoS attacks; only authorized Mini Programs/Official Accounts can access the backend. Others cannot access it even if they obtain the environment id and service name.
- Accelerate access through WeChat's nearby nodes, unaffected by backend service regions, eliminating cross-region latency without requiring multi-region backend deployment;
- No need to configure "server domain names" in the Mini Program backend.
- The backend can directly obtain user information, including opneid, without invoking interfaces (only applicable to WeChat Cloud Run; not available for Tencent Cloud's Cloud Run yet).
- Therefore, if the Cloud Run service is only called by WeChat Mini Programs/Official Accounts, it is recommended to disable public network access in the service settings.