All Products
Document Center

API sample code and endpoints

Last Updated: Sep 14, 2019

This topic describes the MQ API access procedure and precautions, including SDK obtaining, initialization parameter settings, and endpoint settings.

1. Obtain an SDK

Add the following configuration items to the pom.xml file to add an API-dependent SDK.

  1. <dependencies>
  2. <dependency>
  3. <groupId>com.aliyun</groupId>
  4. <artifactId>aliyun-java-sdk-core</artifactId>
  5. <optional>true</optional>
  6. <version>4.3.3</version>
  7. </dependency>
  8. <dependency>
  9. <groupId>com.aliyun</groupId>
  10. <artifactId>aliyun-java-sdk-ons</artifactId>
  11. <version>3.1.0</version> <! -- set to the latest version -->
  12. </dependency>
  13. </dependencies>

Note: For earlier API SDK versions, see API version.

2. API calling example


Before using an API, you need to start the API client. To start the client, you need to set parameters, such as regionId, accessKey, and secretKey. The following shows an example.

  1. /**
  2. *The endpoint of the API, which is set to the target region.
  3. */
  4. String regionId = "XXXXX";
  5. /**
  6. *The AccessKeyId you created in the Alibaba Cloud console for identity authentication.
  7. */
  8. String accessKey = "XXXXXXXXXXXXXXXXX";
  9. /**
  10. *The AccessKeySecret you created in the Alibaba Cloud console for identity authentication.
  11. */
  12. String secretKey = "XXXXXXXXXXXXXXXXX";
  13. IClientProfile profile= DefaultProfile.getProfile(regionIdaccessKeysecretKey);
  14. IAcsClient iAcsClient= new DefaultAcsClient(profile);
  15. //Construct request objects: Take TopicList as an example. Different request objects are constructed for different APIs.
  16. OnsTopicListRequest request = new OnsTopicListRequest();
  17. request.setPreventCache(System.currentTimeMillis()); //The current timestamp.
  18. OnsTopicListResponse response = iAcsClient.getAcsResponse(request);
  19. System.out.println(JSON.toJSONString(response));


Parameter Description
regionId The region where the API gateway resides.
accessKey The AccessKeyId obtained in the Alibaba Cloud console.
secretKey The AccessKeySecret obtained in the Alibaba Cloud console.
productName The name of the API. Enter Ons.
domain The domain of the API endpoint, which complies with ONS. ${RegionId} rule. The region corresponding to the domain must be consistent with the region corresponding to the region ID.

Regions and endpoints

The following table lists API regions supported by MQ as well as corresponding region IDs and domains.

Region name regionId domain
Internet mq-internet-access
China (Hangzhou) cn-hangzhou
China (Shanghai) cn-shanghai
China (Beijing) cn-beijing
China (Zhangjiakou) cn-zhangjiakou
China (Shenzhen) cn-shenzhen
Hong Kong cn-hongkong
Singapore ap-southeast-1
Australia (Sydney) ap-southeast-2
Malaysia (Kuala Lumpur) ap-southeast-3
Japan (Tokyo) ap-northeast-1
Germany (Frankfurt) eu-central-1
UK (London) eu-west-1
US (Virginia) us-east-1
US (Silicon Valley) us-west-1
India (Mumbai) ap-south-1
UAE (Dubai) me-east-1
East China 1 Finance cn-hangzhou-finance
South China 1 Finance cn-shenzhen-finance-1
China East 2 Finance cn-shanghai-finance-1