[Yandex Cloud documentation](../../index.md) > [Yandex Cloud Notification Service](../index.md) > [Tools](index.md) > AWS SDK for JavaScript

# How to get started with the AWS SDK for JavaScript in Yandex Cloud Notification Service

{% note info %}

The service is at the [preview stage](../../overview/concepts/launch-stages.md).

{% endnote %}

To get started with the AWS SDK for JavaScript:

1. [Get your cloud ready](#before-you-begin).
1. [Get a static access key](#static-key).
1. [Configure the AWS SDK](#aws-sdk).
1. [Create a notification channel](#create-channel).
1. [Get a list of channels](#list-channel).
1. [Create an endpoint](#create-endpoint).
1. [Send a notification](#publish).


## Get your cloud ready {#before-you-begin}

Sign up for Yandex Cloud and create a [billing account](../../billing/concepts/billing-account.md):
1. Navigate to the [management console](https://console.yandex.cloud) and log in to Yandex Cloud or create a new account.
1. On the **[Yandex Cloud Billing](https://center.yandex.cloud/billing/accounts)** page, make sure you have a billing account linked and it has the `ACTIVE` or `TRIAL_ACTIVE` [status](../../billing/concepts/billing-account-statuses.md). If you do not have a billing account, [create one](../../billing/quickstart/index.md) and [link](../../billing/operations/pin-cloud.md) a cloud to it.

If you have an active billing account, you can create or select a [folder](../../resource-manager/concepts/resources-hierarchy.md#folder) for your infrastructure on the [cloud page](https://console.yandex.cloud/cloud).

[Learn more about clouds and folders here](../../resource-manager/concepts/resources-hierarchy.md).


## Get a static access key {#static-key}

For authentication in Cloud Notification Service, use a [static access key](../../iam/concepts/authorization/access-key.md). The key is issued for the [service account](../../iam/concepts/users/service-accounts.md), and all actions are performed on behalf of that service account.

To get a static access key:
1. [Create](../../iam/operations/sa/create.md) a service account.
1. [Assign](../../iam/operations/sa/assign-role-for-sa.md) the `editor` [role](../../iam/roles-reference.md#editor) for the folder to the service account.
1. [Create](../../iam/operations/authentication/manage-access-keys.md#create-access-key) a static access key for the service account.

    Save the ID and secret key.


## Configure the AWS SDK {#aws-sdk}

You can find the prerequisites and an AWS SDK for JavaScript installation guide in the relevant [AWS documentation](https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/setting-up.html).

1. [Install](https://nodejs.org/en/download) Node.js.
1. Initialize your project in the selected directory:

    ```bash
    npm init -y
    ```

1. Install the package with the AWS SNS client:

    ```bash
    npm i @aws-sdk/client-sns
    ```

1. Add `"type": "module"` to the `package.json` file to use the ESM syntax.
1. Create a client in the `index.js` file:

    ```javascript
    import * as AWS from "@aws-sdk/client-sns";
    
    const snsClient = new AWS.SNSClient({
        endpoint: "https://notifications.yandexcloud.net/",
        region: "ru-central1",
        credentials: {
            accessKeyId: "<static_key_ID>",
            secretAccessKey: "<secret_key>",
        },
    })
    ```

1. Command to run the program:

    ```bash
    node index.js
    ```


## Create a notification channel {#create-channel}

```javascript
const response = await client.send(
    new AWS.CreatePlatformApplicationCommand({
        Name: "<channel_name>",
        Platform: "<platform_type>",
        Attributes: { <authentication_type>: "<key>" },
    })
);
console.log(`PlatformApplication ARN: ${response.PlatformApplicationArn}`);
```

Where:

* `Name`: Notification channel name, user-defined.
  
    The name must be unique throughout CNS. Once the channel is created, you will not be able to change the name. The name may contain lowercase and uppercase Latin letters, numbers, underscores, hyphens, and periods. It must be from 1 to 256 characters long. For APNs channels, we recommend specifying the bundle ID in the name; for FCM and HMS, the full package name; for RuStore, `packageName`.
    
* `Platform`: Mobile platform type:

    * `APNS` and `APNS_SANDBOX`: Apple Push Notification service (APNs). Use `APNS_SANDBOX` to test the application.
    * `GCM`: Firebase Cloud Messaging (FCM).
    * `HMS`: Huawei Mobile Services (HMS).
    * `RUSTORE`: RuStore Android.

* `Attributes`: Mobile platform authentication parameters in `key=value` format. The values depend on the platform:

    * APNs:
    
        * Token-based authentication:
    
            * `PlatformPrincipal`: Path to the token signature key file from Apple
            * `PlatformCredential`: Key ID
            * `ApplePlatformTeamID`: Team ID
            * `ApplePlatformBundleID`: Bundle ID
    
        * Certificate-based authentication:
    
            * `PlatformPrincipal`: SSL certificate in `.pem` format
            * `PlatformCredential`: Certificate private key in `.pem` format
    
                To save the certificate and the private key in individual `.pem` files, use the [openssl](https://docs.openssl.org/1.1.1/man1/pkcs12) Linux utility:
                
                ```bash
                openssl pkcs12 -in Certificates.p12 -nokeys -nodes -out certificate.pem
                openssl pkcs12 -in Certificates.p12 -nocerts -nodes -out privatekey.pem
                ```
    
        Token-based authentication: The more modern and secure method.
    
    * FCM: `PlatformCredential` is the Google Cloud service account key in JSON format for authentication with the HTTP v1 API or API key (server key) for authentication with the legacy API.
    
        Use the HTTP v1 API because the [FCM legacy API is no longer supported](https://firebase.google.com/docs/cloud-messaging/migrate-v1) starting July 2024.
    
    * HMS:
    
        * `PlatformPrincipal`: Key ID
        * `PlatformCredential`: API key


## Get a list of notification channels {#list-channel}

```javascript
const response = await client.send(new AWS.ListPlatformApplicationsCommand({}));
console.log(
    response.PlatformApplications.map(
        (t) => `Application ARN: ${t.PlatformApplicationArn}`
    ).join("\n")
);
```

You will get the list of notification channels located in the same folder as the service account.


## Create an endpoint {#create-endpoint}

```javascript
const response = await client.send(
    new AWS.CreatePlatformEndpointCommand({
        PlatformApplicationArn: "<notification_channel_ARN>",
        Token: "<push_token>",
    })
);
console.log(`Endpoint ARN: ${response.EndpointArn}`);
```

Where:

* `PlatformApplicationArn`: Notification channel ID (ARN).
* `Token`: Unique push token for the application on the user’s device.


## Send a notification {#publish}

### Explicit notifications (Bright Push) {#bright-push}

{% list tabs %}

- Apple iOS (APNs)

  ```javascript
  const message = {
      default: "<notification_text>",
      APNS: JSON.stringify({
          aps: {
              alert: "<notification_text>",
          },
      }),
  };

  const response = await client.send(
      new AWS.PublishCommand({
          Message: JSON.stringify(message),
          TargetArn: "<endpoint_ARN>",
          MessageStructure: "json",
      })
  );
  console.log(`Message id: ${response.MessageId}`);
  ```

- Google Android (GCM)

  ```javascript
  const message = {
      default: "<notification_text>",
      GCM: JSON.stringify({
          notification: {
              body: "<notification_text>",
          },
      }),
  };
  const response = await client.send(
      new AWS.PublishCommand({
          Message: JSON.stringify(message),
          TargetArn: "<endpoint_ARN>",
          MessageStructure: "json",
      })
  );
  console.log(`Message id: ${response.MessageId}`);
  ```

{% endlist %}

Where:

* `message`: Message.
* `TargetArn`: Mobile endpoint ARN.
* `MessageStructure`: Message format.


### Silent notifications (Silent Push) {#silent-push}

{% list tabs %}

- Apple iOS (APNs)

  ```javascript
  const message = {
      default: "<notification_text>",
      APNS: JSON.stringify({
          key: "value",
      }),
  };
  const response = await client.send(
      new AWS.PublishCommand({
          Message: JSON.stringify(message),
          TargetArn: "<endpoint_ARN>",
          MessageStructure: "json",
      })
  );
  console.log(`Message id: ${response.MessageId}`);
  ```

- Google Android (GCM)

  ```javascript
  const message = {
      default: "<notification_text>",
      GCM: JSON.stringify({
          data: {
              key: "value",
          },
      }),
  };

  const response = await client.send(
      new AWS.PublishCommand({
          Message: JSON.stringify(message),
          TargetArn: "<endpoint_ARN>",
          MessageStructure: "json",
      })
  );
  console.log(`Message id: ${response.MessageId}`);
  ```

{% endlist %}

Where:

* `message`: Message.
* `TargetArn`: Mobile endpoint ARN.
* `MessageStructure`: Message format.


### Text message {#sms-messages}

```javascript
const response = await client.send(
    new AWS.PublishCommand({
        PhoneNumber: "<phone_number>",
        Message: "<notification_text>",
        MessageAttributes: {
            "AWS.SNS.SMS.SenderID": {
                DataType: "String",
                StringValue: "<sender's_text_name>"
            }
        }
    })
);
console.log(`Message id: ${response.MessageId}`);
```

Where:

* `PhoneNumber`: Recipient's phone number
* `Message`: Notification text
* `StringValue`: Sender's text name


## Useful links {#see-also}

* [Getting started](../quickstart.md)
* [AWS CLI](aws-cli.md)
* [Concepts](../concepts/index.md)
* [AWS developer guide](https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/getting-started-nodejs.html)