The Mini Program development framework provides a rich set of native WeChat APIs, enabling you to conveniently access WeChat capabilities such as retrieving user information, local storage, and payment features. For detailed information, please refer to the API Documentation.
Typically, Mini Program APIs are categorized into the following types:
Event Listening APIs
APIs that start with on are used to listen for specific events, such as: wx.onSocketOpen, wx.onCompassChange, etc.
These APIs accept a callback function as a parameter. The callback function is invoked when the event is triggered, and the relevant data is passed as arguments.
Code Example
wx.onCompassChange(function (res) {
console.log(res.direction)
})
Synchronous APIs
APIs ending with Sync are synchronous APIs, such as wx.setStorageSync, wx.getSystemInfoSync, etc. Additionally, there are other synchronous APIs, such as wx.createWorker, wx.getBackgroundAudioManager, etc. For details, refer to the descriptions in the API documentation.
The execution result of a synchronous API can be directly obtained via the function's return value. If an error occurs during execution, an exception is thrown.
Code Example
try {
wx.setStorageSync('key', 'value')
} catch (e) {
console.error(e)
}
Asynchronous API
Most APIs are asynchronous APIs, such as wx.request, wx.login, etc. These API interfaces typically accept a parameter of type Object, which supports specifying the following fields on demand to receive the result of the API call:
Object Parameter Description
| Parameter Name | Type | Required | Description |
|---|---|---|---|
| success | function | No | Callback function for API call success |
| fail | function | No | Callback function for API call failure |
| complete | function | No | Callback function for API call completion (executed for both success and failure) |
| Others | Any | - | Other parameters defined by the API |
Callback Function Parameters
When success, fail, or complete functions are called, they are passed a parameter of type Object, which includes the following fields:
| Attribute | Type | Description |
|---|---|---|
| errMsg | string | Error message. Returns ${apiName}:ok if the call is successful |
| errCode | number | Error code. Supported by only some APIs. Refer to the corresponding API documentation for specific meanings. 0 if successful. |
| Others | Any | Other data returned by the API |
The execution result of asynchronous APIs needs to be obtained through the corresponding callback functions passed in the Object type parameter. Some asynchronous APIs also have return values, which can be used to implement more advanced features, such as wx.request, wx.connectSocket, etc.
Code Example
wx.login({
success(res) {
console.log(res.code)
}
})
Asynchronous APIs Return Promises
Base Library 2.10.2 or later is required. For earlier versions, see compatibility handling.
Asynchronous APIs support both callback and promise invocation methods. If the interface parameter Object does not contain success/fail/complete, a promise is returned by default. Otherwise, it executes in callback mode and returns no value.
Notes
- Some APIs such as
downloadFile,request,uploadFile,connectSocket, andcreateCamera(Mini Game) inherently return values. Their promisify wrappers need to be implemented by developers themselves. - When no callback parameter is provided, asynchronous APIs return a Promise. If the function call fails and enters the fail logic, an error message
Uncaught (in promise)will be raised, which can be caught usingcatch. - wx.onUnhandledRejection can be used to listen for unhandled Promise rejection events.
Code Example
// callback style invocation
wx.chooseImage({
success(res) {
console.log('res:', res)
}
})
// promise style invocation
wx.chooseImage().then(res => console.log('res: ', res))
Cloud Development API
Activate and use WeChat Cloud Development to access the Cloud Development API, enabling you to invoke server-side Cloud Functions directly from the Mini Program side.
Code Example
wx.cloud.callFunction({
// Cloud Function name
name: 'cloudFunc',
// Parameters passed to the Cloud Function
data: {
a: 1,
b: 2,
},
success: function(res) {
console.log(res.result) // Example
},
fail: console.error
})
Cloud Functions also support invocation via Promises.