Skip to main content

Cloud Function Invocation Methods

CloudBase cloud function supports multiple invocation methods to meet the needs of different scenarios and platforms. You can choose the most suitable invocation method based on your actual situation.

Overview of Invocation Methods

Invocation MethodsApplicable ScenariosFeatures
SDK InvocationMini Programs, Web applications, mobile applicationsSimple and easy to use, automatically handles authentication
HTTP APICross-language invocation, third-party system integrationStandard REST API, supports all programming languages
Web ClientBrowser environments, frontend applicationsSupports CORS, direct HTTP access
Mini Program InvocationWeChat Mini ProgramsNative support, no additional configuration required

SDK Invocation

Invoking cloud functions via CloudBase SDK is the simplest approach. The SDK automatically handles authentication and request encapsulation.

Web SDK Invocation

import tcb from '@cloudbase/js-sdk';

// Initialize it.
const app = tcb.init({
env: 'your-env-id'
});

// Invoke a cloud function
async function callFunction() {
try {
const result = await app.callFunction({
name: 'hello-world',
data: {
name: 'CloudBase',
message: 'Hello from Web'
}
});

console.log('Invoked successfully:', result);
return result.result;
} catch (error) {
console.error('Invocation failed:', error);
throw error;
}
}

// Usage example
callFunction().then(result => {
console.log('Function returned:', result);
});

Node.js SDK Invocation

const tcb = require('@cloudbase/node-sdk');

// Initialize it.
const app = tcb.init({
env: 'your-env-id',
secretId: 'your-secret-id',
secretKey: 'your-secret-key'
});

// Invoke a cloud function
async function callFunction() {
try {
const result = await app.callFunction({
name: 'hello-world',
data: {
name: 'CloudBase',
message: 'Hello from Node.js'
}
});

return result.result;
} catch (error) {
console.error('Invocation failed:', error);
throw error;
}
}

Mini Program SDK Invocation

// Invoke cloud function in Mini Program
wx.cloud.callFunction({
name: 'hello-world',
data: {
name: 'CloudBase',
message: 'Hello from MiniProgram'
},
success: res => {
console.log('Invoked successfully:', res.result);
},
fail: err => {
console.error('Invocation failed:', err);
}
});

// Use the Promise approach
async function callFunction() {
try {
const result = await wx.cloud.callFunction({
name: 'hello-world',
data: { message: 'Hello' }
});

return result.result;
} catch (error) {
console.error('Invocation failed:', error);
throw error;
}
}

Advantages of SDK Invocation

  • Automatic Authentication - The SDK automatically handles user authentication and permission verification
  • Error Handling - Unified error handling and retry mechanisms
  • Type Safety - TypeScript support with complete type definitions
  • Simple and Easy to Use - Encapsulates the underlying HTTP request details

Choosing an Invocation Method

Select Based on Scenarios

  • Mini Program development → use Mini Program SDK invocation
  • Web Application Development → use Web SDK invocation
  • Server-side development → Use Node.js SDK invocation
  • Third-party system integration - Use HTTP API invocation
  • Direct frontend access → Use Web client invocation

Performance Comparison

Invocation MethodsPerformanceEase of UseFeature Completeness
SDK Invocation⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐
HTTP API⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐
Web Client⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐
Mini Program Invocation⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐