Install the client SDK
The EMAS Serverless SDK lets you access EMAS Serverless services directly from your miniapp.
Prerequisites
Activate the EMAS service before you use the EMAS Serverless service for the first time. For more information, see Create your first service space.
Install a Node.js development environment. For more information, see Install the Node.js client SDK.
Obtain the latest version of the SDK package. For example, if the version number is 3.1.1, replace the version number in the following steps with 3.1.1.
Use the SDK in an Alipay mini program
To use the SDK in an Alipay mini program, complete the following steps.
In the root directory of your Alipay mini program project, run the following command to install the SDK.
npm install --save @alicloud/mpserverless-sdk@X.X.X // X.X.X is the version numberIn the `mini.project.json` configuration file of your Alipay mini program project, add the following configuration.
// mini.project.json "enableNodeModuleBabelTransform": true, "node_modules_es6_whitelist": [ "@alicloud/mpserverless-sdk"]In the `app.js` file of your miniapp project, add the following code to create an instance of the EMAS Serverless SDK.
// app.js import MPServerless from '@alicloud/mpserverless-sdk' const mpserverless = new MPServerless(my, { appId: <Your Alipay mini program appId>, spaceId: <Your Mini Program Cloud serverless service space spaceId> clientSecret: <Your Mini Program Cloud serverless service space clientSecret> endpoint: <Your Mini Program Cloud serverless service space API endpoint> });Where:
`my` is a global base library in the Alipay mini program developer tools and is directly available in the development environment.
`appId` is the ID of the miniapp. You can obtain the App ID directly from the settings in the Alipay mini program developer tools.
You can obtain the `spaceId`, `clientSecret`, and `endpoint` after you create a service space in the EMAS Serverless console. For more information, see Create your first service space.
In the `onLaunch` lifecycle of `app.js`, call the `init` method to complete the initialization.
NoteYou must call the `mpserverless.init` method to initialize the service before you use the Serverless service on the miniapp client. We recommend that you perform the initialization in the `onLaunch` lifecycle and mount the EMAS Serverless SDK instance to the global App object. This lets you call the instance from other files.
// app.js App({ mpserverless: mpserverless, onLaunch() { mpserverless.init(); }, });Use the SDK in other files.
NoteIn other files, retrieve the SDK instance from the global `App` object using `getApp`.
// Use the SDK in other files const { mpserverless } = getApp(); mpserverless.db.collection('test').find(); mpserverless.function.invoke('test');
Use the SDK in a WeChat mini program
To use the EMAS Serverless SDK in a WeChat mini program, complete the following steps.
In the root directory of your WeChat mini program project, run the following command to install the SDK.
npm install --save @alicloud/mpserverless-sdk@X.X.X // X.X.X is the version numberIn the WeChat mini program IDE, choose Tools > Build npm.
In the `app.js` file of your miniapp project, add the following code to create an instance of the EMAS Serverless SDK.
// app.js import MPServerless from '@alicloud/mpserverless-sdk'; const mpserverless = new MPServerless(wx, { appId: <Your WeChat mini program appId>, spaceId: <Your EMAS serverless service space spaceId> clientSecret: <Your EMAS serverless service space clientSecret> endpoint: <Your EMAS serverless service space API endpoint> });Where:
`wx` is a global base library in the WeChat mini program IDE and is directly available in the development environment.
`appId` is the ID of the miniapp. You can obtain the App ID directly from the settings in the WeChat developer tools.
You can obtain the `spaceId`, `clientSecret`, and `endpoint` after you create a service space in the EMAS Serverless console. For more information, see Create your first service space.
In the `onLaunch` lifecycle of `app.js`, call the `init` method to complete the initialization.
NoteYou must call the `mpserverless.init` method to initialize the service before you use the Serverless service on the miniapp client. We recommend that you perform the initialization in the `onLaunch` lifecycle and mount the EMAS Serverless SDK instance to the global App object. This lets you call the instance from other files.
App({ mpserverless: mpserverless, onLaunch(options) { mpserverless.init(); }, });Use the SDK in other files.
NoteIn other files, retrieve the SDK instance from the global `App` object using `getApp`.
// Use the SDK in other files const { mpserverless } = getApp(); mpserverless.db.collection('test').find(); mpserverless.function.invoke('test');
Use the SDK in a DingTalk mini program
To use the EMAS Serverless SDK in a third-party personal DingTalk miniapp, complete the following steps.
In the root directory of your DingTalk mini program project, run the following command to install the SDK.
npm install --save @alicloud/mpserverless-sdk@X.X.X // X.X.X is the version numberIn the `app.js` file of your miniapp project, add the following code to create an instance of the EMAS Serverless SDK.
// app.js import MPServerless from '@ali/mpserverless-sdk' const mpserverless = new MPServerless(dd, { appId: <Your DingTalk mini program appId>, spaceId: <Your EMAS Serverless service space spaceId> clientSecret: <Your EMAS Serverless service space clientSecret> endpoint: <Your EMAS Serverless service space API endpoint> });Where:
`dd` is a global base library in the DingTalk mini program developer tools and is directly available in the development environment.
`appId` is the ID of the miniapp. You can obtain it from the DingTalk Open Platform.
You can obtain the `spaceId`, `clientSecret`, and `endpoint` after you create a service space in the EMAS Serverless console. For more information, see Create your first service space.
In the `onLaunch` lifecycle of `app.js`, call the `init` method to complete the initialization.
NoteYou must call the `mpserverless.init` method to initialize the service before you use the Serverless service on the miniapp client. We recommend that you perform the initialization in the `onLaunch` lifecycle and mount the EMAS Serverless SDK instance to the global App object. This lets you call the instance from other files.
App({ mpserverless: mpserverless, onLaunch(options) { await mpserverless.init(); }, });Use the SDK in other files.
NoteIn other files, retrieve the SDK instance from the global `App` object using `getApp`.
// Use the SDK in other files const { mpserverless } = getApp(); mpserverless.db.collection('test').find(); mpserverless.function.invoke('test');