WeChat Mini Program Cloud Server SDK

3.0.4 · active · verified Sun Apr 19

The `wx-server-sdk` is the official Software Development Kit for interacting with WeChat Mini Program Cloud Development services from within Cloud Functions. It provides a comprehensive set of APIs for accessing cloud resources such as Cloud Database (MongoDB-like), Cloud Storage (object storage), and invoking other Cloud Functions. The SDK is designed to run in a Node.js environment, specifically within the WeChat Cloud Function runtime. The current stable version is 3.0.4, last published approximately two months ago (as of early 2026). While there isn't a strict, publicly defined release cadence, updates are regularly issued to introduce new features, improve performance, and address bugs. Its primary differentiation lies in its deep integration with the WeChat ecosystem, offering seamless backend support for Mini Programs without needing to manage traditional servers.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates initializing the SDK, performing basic database operations (add and query), and invoking another Cloud Function within a WeChat Cloud Function. It highlights best practices for error handling and accessing user context.

import cloud from 'wx-server-sdk';

// Initialize the cloud environment
cloud.init({
  env: process.env.WX_CLOUD_ENV_ID ?? '', // Ensure environment ID is configured
  traceUser: true, // Enable user tracking for logging
});

const db = cloud.database();
const _ = db.command;

// Cloud Function entry point
export async function main(event: any, context: any) {
  try {
    // Example: Add a new record to a 'todos' collection
    const addResult = await db.collection('todos').add({
      data: {
        description: 'Learn wx-server-sdk',
        completed: false,
        createdDate: new Date(),
        _openid: event.userInfo.openId, // Automatically available in Cloud Function event
      },
    });
    console.log('Added todo:', addResult);

    // Example: Query records from 'todos' collection
    const queryResult = await db.collection('todos')
      .where({
        _openid: event.userInfo.openId,
        completed: false,
      })
      .orderBy('createdDate', 'desc')
      .limit(10)
      .get();
    console.log('Queried todos:', queryResult.data);

    // Example: Call another Cloud Function (replace 'anotherFunction' with actual function name)
    const callFunctionResult = await cloud.callFunction({
      name: 'anotherFunction',
      data: { message: 'Hello from main function' },
    });
    console.log('Called another function:', callFunctionResult.result);

    return {
      statusCode: 200,
      body: 'Operation successful',
      addResult,
      queryResult: queryResult.data,
      callFunctionResult: callFunctionResult.result
    };
  } catch (e: any) {
    console.error('Operation failed:', e);
    return {
      statusCode: 500,
      body: `Operation failed: ${e.message}`,
    };
  }
}

view raw JSON →