All Products
Search
Document Center

ApsaraMQ for MQTT:ApplyToken

Last Updated:Oct 26, 2026

When token-based authentication is used, the ApsaraMQ for MQTT broker issues a token (temporary credential) to the client, and the client must include the token when accessing the ApsaraMQ for MQTT broker to authenticate permissions. This API operation is used to apply for a temporary token from ApsaraMQ for MQTT.

Operation description

  • The request frequency limit for a single user is 100 calls per second. If you have special requirements, contact the technical support team of ApsaraMQ for MQTT. DingTalk group ID: 35228338.

  • Each time you successfully call the ApplyToken API operation, it is counted as one message TPS, which affects your billing. For billing details, see Billing overview.

Try it now

Try this API in OpenAPI Explorer, no manual signing needed. Successful calls auto-generate SDK code matching your parameters. Download it with built-in credential security for local usage.

Test

RAM authorization

The table below describes the authorization required to call this API. You can define it in a Resource Access Management (RAM) policy. The table's columns are detailed below:

  • Action: The actions can be used in the Action element of RAM permission policy statements to grant permissions to perform the operation.

  • API: The API that you can call to perform the action.

  • Access level: The predefined level of access granted for each API. Valid values: create, list, get, update, and delete.

  • Resource type: The type of the resource that supports authorization to perform the action. It indicates if the action supports resource-level permission. The specified resource must be compatible with the action. Otherwise, the policy will be ineffective.

    • For APIs with resource-level permissions, required resource types are marked with an asterisk (*). Specify the corresponding Alibaba Cloud Resource Name (ARN) in the Resource element of the policy.

    • For APIs without resource-level permissions, it is shown as All Resources. Use an asterisk (*) in the Resource element of the policy.

  • Condition key: The condition keys defined by the service. The key allows for granular control, applying to either actions alone or actions associated with specific resources. In addition to service-specific condition keys, Alibaba Cloud provides a set of common condition keys applicable across all RAM-supported services.

  • Dependent action: The dependent actions required to run the action. To complete the action, the RAM user or the RAM role must have the permissions to perform all dependent actions.

Action

Access level

Resource type

Condition key

Dependent action

mq:ApplyToken

create

*Instance

acs:mq:{#regionId}:{#accountId}:{#InstanceId}

None None

Request parameters

Parameter

Type

Required

Description

Example

Resources

string

Yes

The resource name, which is the topic of ApsaraMQ for MQTT. Separate multiple topics with commas (,). Each token can be used to perform operations on up to 100 resources. If multiple topics are specified, they must be sorted in lexicographical order. The resource parameter registered when you apply for a token supports the MQTT wildcard syntax, including the plus sign (+) for single-level wildcards and the number sign (#) for multi-level wildcards.

For example, if you set Resources to "Topic1/+" when you apply for a token, the client can perform operations on any topic in the format of "Topic1/xxx". If you set Resources to "Topic1/#", the client can perform operations on any multi-level topic in the format of "Topic1/xxx/xxx/xxx".

Note

ApsaraMQ for MQTT supports matching child topics. You do not need to create child topics in the console. You can directly configure the settings in the message sending and receiving code. The forward slash (/) in a topic name is used to separate the parent topic and child topics. For more information, see Terms.

TopicA/+

InstanceId

string

Yes

The ID of the ApsaraMQ for MQTT instance. The instance ID must match the actual instance ID used by the client. You can obtain the instance ID on the Instance Details page in the console.

post-cn-0pp12gl****

ExpireTime

integer

Yes

The expiration time of the token. The value is a millisecond timestamp. The minimum effective period is 60 seconds, and the maximum effective period is 30 days. If the specified value exceeds 30 days, the API operation does not return an error, but the actual effective period is 30 days.

Example: If the effective period of the token is 60 seconds and the current system time in milliseconds is 1609434061000, set this parameter to 1609434121000, which is the sum of 1609434061000 and 60 × 1000.

1609434121000

Actions

string

Yes

The permission type of the token. Valid values:

  • R: read-only permissions. The client can only subscribe to the specified topics and cannot publish messages to the topics.

  • W: write-only permissions. The client can only publish messages to the specified topics and cannot subscribe to the topics.

  • R,W: read and write permissions. The client can publish messages to and subscribe to the specified topics. Separate R and W with a comma (,).

R

Note

For more parameter descriptions, see Common parameters and Endpoints.

Response elements

Element

Type

Description

Example

object

Token

string

The token returned by the broker.

Note

Do not make any assumptions about the length, format, or rules of the returned token. The actual returned value prevails.

LzMT+XLFl5s/YWJ/MlDz4t/Lq5HC1iGU1P28HAMaxYxn8aQbALNtml7QZKl9L9kPe6LqUb95tEVo+zUqOogs9+jZwDUSzsd4X4qaD3n2TrBEuMOqKkk1Xdrvu9VBQQvIYbz7MJWZDYC3DlW7gLEr33Cuj54iIhagtBi3epStJitsssWs7otY9zhKOSZxhr49G3d0bh35mwyP18EMvDas8UlzeSozsSrujNUqZXOGK0PEBSd+rWMGDJlCt6GFmJgm2JFY7PJwf/7OOSmUYIYFs5o/PuPpoTMF+hcVXMs+0yDukIMTOzG9m3t8k36PVrghFmnK6pC3Rt3mibjW****ng==

RequestId

string

The common parameter. The ID of each request is unique.

31782AAF-D0CC-44C3-ABFD-1B500276****

Examples

Success response

JSON format

{
  "Token": "LzMT+XLFl5s/YWJ/MlDz4t/Lq5HC1iGU1P28HAMaxYxn8aQbALNtml7QZKl9L9kPe6LqUb95tEVo+zUqOogs9+jZwDUSzsd4X4qaD3n2TrBEuMOqKkk1Xdrvu9VBQQvIYbz7MJWZDYC3DlW7gLEr33Cuj54iIhagtBi3epStJitsssWs7otY9zhKOSZxhr49G3d0bh35mwyP18EMvDas8UlzeSozsSrujNUqZXOGK0PEBSd+rWMGDJlCt6GFmJgm2JFY7PJwf/7OOSmUYIYFs5o/PuPpoTMF+hcVXMs+0yDukIMTOzG9m3t8k36PVrghFmnK6pC3Rt3mibjW****ng==",
  "RequestId": "31782AAF-D0CC-44C3-ABFD-1B500276****"
}

Error codes

HTTP status code

Error code

Error message

Description

400 ApplyTokenOverFlow You have applied for tokens too many times. Please try again later.
400 CheckAccountInfoFailed An error occurred while checking the account information by the STS token.
400 InstancePermissionCheckFailed An error occurred while validating the permissions of the instance. Please verify the account that created the instance and its permissions settings.
400 ParameterCheckFailed An error occurred while validating the parameters. The parameters may be missing or invalid.
400 PermissionCheckFailed An error occurred while validating the resource permissions. Please check the account that created the instance, topic, and GroupId, and check their permission settings.
400 InvalidParameter.%s An error occurred while validating the parameter. The parameter may be missing or invalid.
500 InternalError An error occurred while processing your request. Try again later.
500 SystemOverFlow An error occurred while processing your request. Please try again.
404 ApiNotSupport The specified API is not supported.

See Error Codes for a complete list.

Release notes

See Release Notes for a complete list.