Alibaba Cloud SMS provides SDKs to help you use the SMS API.
Background information
The following table lists the background information about the SMS API.
API version | 2018-05-01 |
Style | RPC |
Endpoint | Global public endpoint: dysmsapi.ap-southeast-1.aliyuncs.com . The endpoint is the URL of the SMS API. Select an endpoint based on the network type and region. For more information, see Endpoints.
|
Note 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.
This topic takes the SendMessageToGlobe API operation as an example to walk you through the SMS API. Workflow:
Configure the account
Grant permissions to a RAM user
Important We recommend that you grant permissions to a RAM user and use the RAM user to make API calls and perform daily O&M. For information about RAM users, see Overview of RAM users.
Create a RAM user
Go to the Create User page of the Resource Access Management (RAM) console, specify a RAM username, set the Access Mode parameter to OpenAPI Access, and then click OK. Record the AccessKey information.
Grant permissions to the RAM user
Go to the Users page of the RAM console, find the RAM user, and click Add Permissions in the Actions column. In the Policy section of the panel that appears, search for and select the AliyunDysmsFullAccess policy, and then click Grant permissions.
Note For more information about how to create a custom policy, see RAM authorization.
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
The Java version must be later than 8. Check the Java version by running the following command in the terminal:
java -version
If you have not deployed a Java environment or the Java version is earlier than 8, see Build a Java development environment on Windows.
Configure the SDK
You can install the SDK through Maven dependencies.
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>dysmsapi20180501</artifactId>
<version>1.0.7</version>
</dependency>
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.
Note 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. You can only view and record an AccessKey secret when creating it. Otherwise, you must create a new AccessKey pair. For more information, see Create an AccessKey pair.
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"));
Configure the endpoint
Sample code:
config.endpoint = "dysmsapi.ap-southeast-1.aliyuncs.com";
Use an endpoint based on the network type and region. For more information, see Endpoints.
Call an API operation
Construct an API request
Construct an API request and specify parameters based on your business requirements.
Note 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.
Note 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);
Sample code
Sample code:
package com.aliyun.sample;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.dysmsapi20180501.Client;
import com.aliyun.dysmsapi20180501.models.SendMessageToGlobeRequest;
import com.aliyun.dysmsapi20180501.models.SendMessageToGlobeResponse;
import static com.aliyun.teautil.Common.toJSONString;
public class Sample {
public static Client createClient() throws Exception {
Config config = new Config()
// Configure the AccessKey ID. Make sure that the environment variable ALIBABA_CLOUD_ACCESS_KEY_ID is configured.
.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
// Configure the AccessKey secret. Make sure that the environment variable ALIBABA_CLOUD_ACCESS_KEY_SECRET is configured.
.setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
Configure the endpoint
config.endpoint = "dysmsapi.ap-southeast-1.aliyuncs.com";
return new Client(config);
}
public static void main(String[] args) throws Exception {
// Initialize the client
Client client = Sample.createClient();
// Construct the request object and specify the request parameters
SendMessageToGlobeRequest sendSmsRequest = new SendMessageToGlobeRequest()
.setTo("<YOUR_VALUE>")
.setMessage("<YOUR_VALUE>");
// Obtain the response object
SendMessageToGlobeResponse sendSmsResponse = client.sendMessageToGlobe(sendSmsRequest);
// Response body and headers
System.out.println(toJSONString(sendSmsResponse));
}
}
FAQ
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