Confirm an endpoint owner wants to receive Amazon SNS messages using an Amazon SDK
The following code examples show how to confirm the owner of an endpoint wants to receive Amazon SNS messages by validating the token sent to the endpoint by an earlier Subscribe action.
- Java
-
- SDK for Java 2.x
-
Note There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository
. public static void confirmSub(SnsClient snsClient, String subscriptionToken, String topicArn ) { try { ConfirmSubscriptionRequest request = ConfirmSubscriptionRequest.builder() .token(subscriptionToken) .topicArn(topicArn) .build(); ConfirmSubscriptionResponse result = snsClient.confirmSubscription(request); System.out.println("\n\nStatus was " + result.sdkHttpResponse().statusCode() + "\n\nSubscription Arn: \n\n" + result.subscriptionArn()); } catch (SnsException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
-
For API details, see ConfirmSubscription in Amazon SDK for Java 2.x API Reference.
-
- JavaScript
-
- SDK for JavaScript (v3)
-
Note There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository
. Create the client in a separate module and export it.
import { SNSClient } from "@aws-sdk/client-sns"; // Set the AWS Region. const REGION = "REGION"; //e.g. "us-east-1" // Create SNS service object. const snsClient = new SNSClient({ region: REGION }); export { snsClient };
Import the SDK and client modules and call the API.
// Import required AWS SDK clients and commands for Node.js import {ConfirmSubscriptionCommand } from "@aws-sdk/client-sns"; import {snsClient } from "./libs/snsClient.js"; // Set the parameters const params = { Token: "TOKEN", // Required. Token sent to the endpoint by an earlier Subscribe action. */ TopicArn: "TOPIC_ARN", // Required AuthenticateOnUnsubscribe: "true", // 'true' or 'false' }; const run = async () => { try { const data = await snsClient.send(new ConfirmSubscriptionCommand(params)); console.log("Success.", data); return data; // For unit tests. } catch (err) { console.log("Error", err.stack); } }; run();
-
For more information, see Amazon SDK for JavaScript Developer Guide.
-
For API details, see ConfirmSubscription in Amazon SDK for JavaScript API Reference.
-
- PHP
-
- SDK for PHP
-
Note There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository
. require 'vendor/autoload.php'; use Aws\Sns\SnsClient; use Aws\Exception\AwsException; /** * Verifies an endpoint owner's intent to receive messages by validating the token sent to the endpoint by an earlier Subscribe action. * * This code expects that you have AWS credentials set up per: * https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_credentials.html */ $SnSclient = new SnsClient([ 'profile' => 'default', 'region' => 'us-east-1', 'version' => '2010-03-31' ]); $subscription_token = 'arn:aws:sns:us-east-1:111122223333:MyTopic:123456-abcd-12ab-1234-12ba3dc1234a'; $topic = 'arn:aws:sns:us-east-1:111122223333:MyTopic'; try { $result = $SnSclient->confirmSubscription([ 'Token' => $subscription_token, 'TopicArn' => $topic, ]); var_dump($result); } catch (AwsException $e) { // output error message if fails error_log($e->getMessage()); }
-
For API details, see ConfirmSubscription in Amazon SDK for PHP API Reference.
-
For a complete list of Amazon SDK developer guides and code examples, see Using Amazon SNS with an Amazon SDK. This topic also includes information about getting started and details about previous SDK versions.