Alibaba Cloud SMS provides SDKs to help you use the SMS API.
This topic takes the SendMessageToGlobe API operation as an example to walk you through the SMS API. Workflow:
If you are familiar with OpenAPI Explorer, you can refer to the API documentation and call API operations.
We recommend that you call API operations through SMS SDKs. For information about how to use custom encapsulated requests to call API operations, see Request syntax and signature method V3.
Preparations
Item | Description | References |
Signature | Submit the signature for approval and obtain the signature name. | |
Message template | Submit the message template for approval and obtain the template code. | |
User permissions | Note We recommend that you grant permissions to a RAM user and use the RAM user to make API calls and perform daily O&M. Make sure that the RAM user that you want to use to call the API has the permissions on SMS:
For more information about how to create a custom policy, see RAM authorization. | |
AccessKey ID | To view the AccessKey ID of the RAM user, go to the Users page of the RAM console and click the name of the RAM user. | |
AccessKey secret | You can only view and record an AccessKey secret when creating it. Otherwise, you must create a new AccessKey pair. |
Create a RAM user and grant permissions to it
Deploy a development environment
This topic uses Java as an example. For information about how to use SMS SDKs for other programming languages, see SDK reference.
Deploy a Java environment
Initialize the client
Alibaba Cloud SDKs support multiple access credentials, such as AccessKey pairs and Security Token Service (STS) tokens, to initialize clients. For more information, see Manage access credentials. In this example, an AccessKey pair is used to initialize the client.
Configure an AccessKey pair
To prevent the AccessKey pair from being leaked due to hard-coding, you must configure environment variables to obtain an AccessKey pair. For more information about how to configure environment variables, see Configure environment variables in Linux, macOS, and Windows.
In this example, the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID
and ALIBABA_CLOUD_ACCESS_KEY_SECRET
are used. Sample code:
Config config = new Config()
// Make sure that the environment variables are configured for code execution.
.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
.setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
The System.getenv()
method obtains system environment variables. You need to enter the variable names. Do not directly enter the AccessKey information.
Configure the endpoint
The following code configures the endpoint. Replace the endpoint based on the region. For more information, see Endpoints.
config.endpoint = "dysmsapi.ap-southeast-1.aliyuncs.com";
Call an API operation
Construct an API request
Construct an API request and specify parameters based on your business requirements.
The request object is named in the {API name}Request
format. For example, the request object of the SendSms
operation is SendSmsRequest
.
SendMessageToGlobeRequest sendSmsRequest = new SendMessageToGlobeRequest()
.setTo("<YOUR_VALUE>")
.setMessage("<YOUR_VALUE>");
Send an API request
Use the SendMessageToGlobe operation to send an API request.
The response object is named in the {API name}Response
format. For example, the response object of the SendSms
operation is SendSmsResponse
.
SendMessageToGlobeResponse sendSmsResponse = client.sendMessageToGlobe(sendSmsRequest);
For information about how to configure request parameters, see Integrate Alibaba Cloud SDK V2.0 for Java.
For information about the timeout and retry settings, see Configure a timeout period and Configure a retry mechanism.
For information about SDK exceptions and how to fix them, see Exception handling.
FAQ
How do I view the AccessKey ID and AccessKey secret?
You can only view and record an AccessKey secret when creating it. Otherwise, you must create a new AccessKey pair.
How do I encapsulate an HTTP request and use the curl
command or Postman to call API operations?
Alibaba Cloud SDKs have encapsulated the signature, timeout, and retry configurations. We recommend that you use Alibaba Cloud SDKs to reduce development costs. For information about how to encapsulate a request for API testing, see Request syntax and signature method V3.
What to do next
Debug in OpenAPI Explorer.
To send multiple messages at a time, call the BatchSendMessageToGlobe operation.
For information about other use scenarios of SMS, visit Sample Code Center.
For information about the statistics, such as the message volume, go to the Summary page of the SMS console.