Calling Regular Cloud Functions
CloudBase provides various SDKs and APIs for developers to call cloud functions, including Mini-Program SDK, JS SDK, Node SDK, HTTP API, etc., to meet the needs of different scenarios and platforms.
Call Methods Overview
| Call Method | Applicable Scenarios | Features |
|---|---|---|
| Mini-Program SDK | WeChat Mini Programs | Native support, automatically carries user identity |
| Web SDK | Browser environment, frontend applications | Simple and easy to use, automatic authentication |
| Node.js SDK | Server-side, inter-function calls | Supports server-side calls, fully functional |
| HTTP API | Cross-language calls, third-party system integration | Standard REST API, supports all languages |
| HTTP Access Service | Direct frontend access, custom domain | Supports custom paths and domains, cross-domain access |
Call Examples
- Mini-Program SDK
- Web SDK
- Node.js SDK
- HTTP API
- HTTP Access Service
WeChat Mini Programs can directly use the wx.cloud.callFunction API to call cloud functions without additional configuration.
// Basic call method
wx.cloud.callFunction({
name: 'hello-world',
data: {
name: 'CloudBase',
message: 'Hello from MiniProgram'
},
success: res => {
console.log('Call successful:', res.result);
},
fail: err => {
console.error('Call failed:', err);
}
});
💡 Tip: When mini-programs call cloud functions, they automatically carry the user's OPENID, which can be obtained in the cloud function through
event.userInfo.openId.
Related Documentation
Call cloud functions through CloudBase Web SDK, suitable for browser environments and frontend applications.
import tcb from '@cloudbase/js-sdk';
// Initialize
const app = tcb.init({
env: 'your-env-id'
});
// Anonymous login (if needed)
await app.auth().signInAnonymously();
async function callFunction() {
try {
const result = await app.callFunction({
name: 'hello-world',
data: {
name: 'CloudBase',
message: 'Hello from Web'
}
});
console.log('Call successful:', result);
return result.result;
} catch (error) {
console.error('Call failed:', error);
throw error;
}
}
// Usage example
callFunction().then(result => {
console.log('Function return:', result);
});
Call cloud functions through CloudBase Node.js SDK, suitable for server-side and inter-function call scenarios.
const tcb = require('@cloudbase/node-sdk');
// Initialize
const app = tcb.init({
env: 'your-env-id',
secretId: 'your-secret-id', // Optional, for server-side calls
secretKey: 'your-secret-key' // Optional, for server-side calls
});
async function callFunction() {
try {
const result = await app.callFunction({
name: 'hello-world',
data: {
name: 'CloudBase',
message: 'Hello from Node.js'
}
});
console.log('Call successful:', result);
return result.result;
} catch (error) {
console.error('Call failed:', error);
throw error;
}
}
Call cloud functions through HTTP API, supports cross-language access, suitable for third-party system integration.
# Basic call
curl -L 'https://your-env-id.api.tcloudbasegateway.com/v1/functions/hello-world' \
-H 'Accept: application/json' \
-H 'Authorization: Bearer your-access-token' \
-H 'Content-Type: application/json' \
-d '{
"message": "Hello CloudBase",
"timestamp": 1640995200000
}'
# HTTP cloud function call
curl -L 'https://your-env-id.api.tcloudbasegateway.com/v1/functions/web-function?webfn=true' \
-H 'Accept: application/json' \
-H 'Authorization: Bearer your-access-token' \
-H 'Content-Type: application/json' \
-d '{
"path": "/api/users",
"method": "GET"
}'
Call cloud functions through HTTP access service, supports custom domains and paths, suitable for direct frontend access.
Related Documentation
- HTTP API Authentication - Learn how to get and use access tokens
- HTTP Access Service - Configure custom domains and access paths
- Cloud Function Development Guide - Learn how to write cloud function code
- Cloud Function Best Practices - Learn best practices for cloud function development