This topic describes how to use a software development kit (SDK) to call an OpenAPI to send a text message to a specified mobile phone number.
Step 1: View the OpenAPI documentation
Before you call an OpenAPI, we recommend that you read the API documentation for or SendMessageToGlobe to understand the required parameters and permissions. For more information, see API overview.
Step 2: Create a RAM user and grant permissions
An Alibaba Cloud account has a high level of permissions. We recommend that you use a Resource Access Management (RAM) user to call API operations and perform daily operations and maintenance (O&M). For more information about RAM users, see What is a RAM user?.
Create a RAM user
Grant permissions to the RAM user
Step 3: Call an OpenAPI operation
SDKs can be easily integrated into your application and support a wide range of operations. We recommend that you use SDKs to call API operations. This topic uses the Java SDK as an example to show how to call an API operation. You can use SDKs for other languages in a similar manner. For more information, see Short Message Service SDK.
Prerequisites
Ensure that you are using Java 8 or a later version. For more information about how to configure the Java environment, see Build a Java development environment on Windows.
Configure Maven dependencies to install the SDK.
<dependency> <groupId>com.aliyun</groupId> <artifactId>dysmsapi20180501</artifactId> <!-- Replace 'the-latest-version' with the latest version number. You can find it at https://mvnrepository.com/artifact/com.aliyun/dysmsapi20180501 --> <version>the-latest-version</version> </dependency>Configure environment variables to read the AccessKey pair. For more information about how to configure environment variables, see Configure environment variables on Linux, macOS, and Windows.
NoteTo prevent your AccessKey pair from being hard-coded into your code and being leaked, configure environment variables to retrieve the AccessKey pair.
This topic uses the
ALIBABA_CLOUD_ACCESS_KEY_IDandALIBABA_CLOUD_ACCESS_KEY_SECRETenvironment variables as an example.
Sample code
The following sample code shows how to use the SDK to call the API operation to send text messages. Replace the placeholder values of the parameters with your actual values based on the comments.
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 ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is set in your runtime environment.
.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
// Configure the AccessKey secret. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is set in your runtime environment.
.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();
// Create a request object and specify the parameter values.
SendMessageToGlobeRequest sendSmsRequest = new SendMessageToGlobeRequest()
.setTo("<YOUR_VALUE>")
.setMessage("<YOUR_VALUE>");
// Obtain the response object.
SendMessageToGlobeResponse sendSmsResponse = client.sendMessageToGlobe(sendSmsRequest);
// The response includes the body and headers from the server.
System.out.println(toJSONString(sendSmsResponse));
}
}You can also download the sample code and run it directly.
Download the sample code
Run the project
After you run the project, you can view the output. The following sample output is returned:
{
"headers": {
"date": "Tue, 24 Oct 2023 07:47:17 GMT",
"content-type": "application/json;charset=utf-8",
"content-length": "263",
"connection": "keep-alive",
"keep-alive": "timeout=25",
"access-control-allow-origin": "*",
"access-control-expose-headers": "*",
"x-acs-request-id": "97B1D7B6-F2F6-3A50-97BC-A90B43EC962F",
"x-acs-trace-id": "29c11fe4c778b74774d5f5602f0e7975",
"etag": "2a+mcDRTDkXqx9VF7b6U57Q3"
},
"statusCode": 200,
"body": {
"ResponseCode": "OK",
"NumberDetail": {
"Region": "Taiwan",
"Country": "Taiwan, Province of China",
"Carrier": "FarEasTone"
},
"RequestId": "97B1D7B6-F2F6-3A50-97BC-A90B43EC962F",
"Segments": "1",
"ResponseDescription": "OK",
"To": "88691567****",
"MessageId": "191921698133637273"
}
}