Guide mini programs to call customized JSAPIs

The procedures for mini programs to call customized JSAPIs are slightly different from the ones used for original built-in JSAPIs. While Mini Program Platform provides standard JSAPI specifications for the latter, super apps need to create their own guidance for new JSAPIs or edit the provided specifications for customized existing JSAPIs. This topic helps super apps with the documentation for mini programs by highlighting the differences in calling the two types of JSAPIs and providing a JSAPI specification template.

Note: To publish your documentation, contact your Solution Architect for assistance.

Instructions for calling customized JSAPIs

In contrast to directly calling original built-in APIs, mini programs are required to perform an additional step prior to utilizing customized APIs. See Add the JSAPI in Mini Program Platform for details.

Besides, the code to call customized JSAPIs is also different. See Call the customized JSAPI for details.

Add the JSAPI in Mini Program Platform

To access the customized JSAPIs, mini programs must add them in Mini Program Platform. Refer to the different guides according to your workspace type:

For tenant workspace

For tenant workspace, only Workspace Admin can perform this action by completing the following steps:

  1. Log into Mini Program Platform and navigate to Mini Programs > Wallet.
  2. Find and click the name of the mini program in which you need to utilize the customized JSAPI.
  3. Click the Features tab and then + Add Feature. Select the feature that the customized JSAPI belongs to and click Add. Then, the added feature is shown in the feature list and you can access all the associated JSAPIs.

For developer workspaces

For developer workspaces, only Owner and Master can perform this action by completing the following steps:

  1. Log into Mini Program Platform and navigate to Mini Programs > Manage Mini Programs.
  2. Find and click the specific mini program in which you need to utilize the customized JSAPI.
  3. Click the Features tab and then + Add Feature. Select the feature that the customized JSAPI belongs to and click Add. Then, the added feature is shown in the feature list and you can access all the associated JSAPIs.

Notes for your documentation:

  • In your JSAPI specification, clearly state the name of the feature where the customized JSAPI belongs, which is necessary for mini programs to complete step 3.
  • It is recommended that you mention the different calling approaches of the original built-in JSAPIs and customized JSAPIs and include the Add the JSAPI in Mini Program Platform instructions in JSAPI overview. Then, refer to it in the Before you begin or Prerequisites section in each specification for the customized JSAPIs.

Call the customized JSAPI

Mini programs can use the following two statements to call the customized JSAPIs depending on the JSAPI type:

Statement

Description

my.apiName

Applies to customized existing JSAPIs only.

This is a common approach to call all built-in JSAPIs because their names already exist in the Mini Program Framework.

my.call

Applies to both customized existing JSAPIs and new JSAPIs.

Mini programs can call new JSAPIs with this statement only because the names of these JSAPIs are only known to the iOS client of the super app and cannot be recognized by Mini Program Framework.

For more information about this statement, refer to my.call.

my.call

Statement and parameters

The following code shows how to use the my.call statement:

copy
my.call(apiName, params, callback);

Here are the details of the parameters used in the above statement:

Field

Type

Required

Description

apiName

String

M

The name of the JSAPI.

params

JSONObject

O

The request parameters of the JSAPI call. Mini programs must specify this parameter if your JSAPI requires any request parameter.

callback

function

O

The callback function of the JSAPI call. Mini programs must specify this parameter if the callback of your JSAPI requires further actions from them.

Sample code

With the above fields specified, a sample code is created as follows:

copy
my.call("testAPI", {
  intParam1 : 1,
  stringParam2 : "ABC",
  success: (res) => {
    },
  fail: (res) => {
    }
});

Notes for your documentation:

  • For customized existing JSAPIs, make sure to edit the parameter details and sample codes in the provided specifications.
  • If you have multiple super apps and not all of them support the customized JSAPIs, make sure to remind mini programs that they need to use the my.call statement prudently in case of any error in a super app that does not implement the corresponding JSAPI.

JSAPI specification template

You can write the specification for the customized JSAPIs according to the following structure:

  • An opening paragraph that briefly describes the function of the JSAPI.
  • (Optional) Before you begin (or Prerequisites): The requirements or other important information that mini programs should know before they call the JSAPI.
  • Request parameters: The details of the request parameters of the JSAPI. You can provide the details with the template parameter table below.
  • Response parameters: The details of the response parameters of the JSAPI. If there is no response parameter for your JSAPI, write "N/A" in this section. Otherwise, you can provide the details with the template parameter table below.
  • (Optional) Error codes: The details of the errors that are returned to mini programs by the JSAPI. You can provide the details with the template error code table below.
  • Sample: A sample code of the JSAPI.

Template parameter table

Name

Type

Description

Required

The specified parameter name.

e.g., String

The description of the parameter.

M/O

Template error codes table

Error code

Error message

Description

Further action

The specified error.

The specified errorMessage.

The description of the error.

The actions that mini programs need to perform to solve the error.