

# Connecting to AWS IoT Core for Amazon Sidewalk
<a name="iot-sidewalk-onboard"></a>

This section shows you how to onboard your Sidewalk end device and then connect your device to the Sidewalk network. It describes the steps that you perform in the onboarding tutorial, as mentioned in [Introduction to onboarding your Sidewalk devices](sidewalk-getting-started.md#sidewalk-gs-workflow). You'll learn how to onboard devices by using the AWS IoT console and the AWS IoT Core for Amazon Sidewalk API operations. You'll also learn about the AWS CLI commands that perform these operations.

## Prerequisites
<a name="sidewalk-connect-prereq"></a>

To add your end device and destination to AWS IoT Core for Amazon Sidewalk, you must set up your AWS account. To perform these operations using the AWS IoT Wireless API or the AWS CLI commands, you must also set up the AWS CLI. For more information about the prerequisites and setting up, see [Installing Python and Python3-pip](getting-started.md#wireless-onboard-prereq).

**Note**  
To perform the entire onboarding workflow for provisioning and registering your end device, and connecting to your hardware development kit (HDK), you must also set up your Sidewalk gateway and HDK. For more information, see [Setting up the hardware development kit (HDK)](https://docs.sidewalk.amazon/getting-started/sidewalk-onboard-prereq-hdk.html) and [Setting up a Sidewalk gateway](https://docs.sidewalk.amazon/getting-started/sidewalk-onboard-prereq-gateway.html) in the *Amazon Sidewalk documentation*.

## Describing your Sidewalk resources
<a name="sidewalk-connect-resources"></a>

Before you get started and create the resources, we recommend that you consider the naming convention of your Sidewalk end devices, device profiles, and destinations. AWS IoT Core for Amazon Sidewalk assigns a unique identifier to the resources that you create. However, you can give them more descriptive names, add a description, or add optional tags to help identify and manage them. 

**Note**  
The destination name can't be changed after it's created. Use a name that's unique to your AWS account and AWS Region.

For more information, see [Describing your AWS IoT Wireless resources](getting-started.md#iotwireless-describe-resources).

**Topics**
+ [

## Prerequisites
](#sidewalk-connect-prereq)
+ [

## Describing your Sidewalk resources
](#sidewalk-connect-resources)
+ [

# Add your device to AWS IoT Core for Amazon Sidewalk
](iot-sidewalk-create-device.md)
+ [

# Add a destination for your Sidewalk end device
](iot-sidewalk-qsg-destination.md)
+ [

# Connect your Sidewalk device and view uplink metadata format
](iot-sidewalk-connect-uplink-metadata.md)
+ [

# Using AWS Location with Sidewalk Devices
](iot-device-location-feature-sidewalk-device.md)

# Add your device to AWS IoT Core for Amazon Sidewalk
<a name="iot-sidewalk-create-device"></a>

Before creating a wireless device, first create a device profile. Device profiles define the device capabilities and other parameters for your Sidewalk devices. A single device profile can be associated with multiple devices.

After you create a device profile, when you retrieve information about the profile, it returns a `DeviceTypeId`. When you provision your end device, you'll use this ID, the device certificates, application server public key, and the SMSN.

## How to create and add your device
<a name="iot-sidewalk-device-how"></a>

1. Create a device profile for your Sidewalk end devices. Specify a profile name to use for your Sidewalk devices as an alphanumeric string. The profile will help identify the devices to associate it with.
   + (Console) When adding your Sidewalk device, you can also create a new profile. This helps you quickly add your device to AWS IoT Core for Amazon Sidewalk and associate it with a profile.
   + (API) Use the `CreateDeviceProfile` API operation by specifying a profile name and the Sidewalk object, `sidewalk {}`. The API response will contain a profile ID and ARN (Amazon Resource Name).

1. Add your wireless device to AWS IoT Core for Amazon Sidewalk. Specify a destination name and choose the device profile that you created in the previous step. 
   + (Console) When adding your Sidewalk device, enter a destination name, and choose the profile that you created.
   + (API) Use the `CreateWirelessDevice` API operation. Specify a destination name and the ID of the device profile obtained previously.  
**Wireless device parameters**    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/iot-wireless/latest/developerguide/iot-sidewalk-create-device.html)

1. Obtain the JSON file that contains the required information for provisioning your end device.
   + (Console) Download this file from the details page of the Sidewalk device that you created.
   + (API) Use the `GetDeviceProfile` and `GetWirelessDevice` API operations to retrieve information about your device profile and wireless device. Store the API response information as JSON files, such as *`device_profile.json`* and *`wireless_device.json`*.

# Add your device profile and Sidewalk end device
<a name="iot-sidewalk-add-device"></a>

This section shows how you can create a device profile. It also shows how you can use the AWS IoT console and the AWS CLI to add your Sidewalk end device to AWS IoT Core for Amazon Sidewalk.

## Add your Sidewalk device (console)
<a name="iot-sidewalk-add-device-console"></a>

To add your Sidewalk device using the AWS IoT console, go to the [Sidewalk tab of the Devices hub](https://console.aws.amazon.com/iot/home#/wireless/devices?tab=sidewalk), choose **Provision device**, and then perform the following steps.

![\[Workflow for adding, provisioning, and registering your Sidewalk device to connect to the cloud.\]](http://docs.aws.amazon.com/iot-wireless/latest/developerguide/images/iot-sidewalk-provision-device.PNG)


1. 

**Specify device details**

   Specify the configuration information for your Sidewalk device. You can also create a new device profile, or choose an existing profile for your Sidewalk device.

   1. Specify a device name and optional description. The description can be up to 2,048 characters long. These fields can be edited after you create the device.

   1. Choose a device profile to associate with your Sidewalk device. If you have any existing device profiles, you can choose your profile. To create a new profile, choose **Create new profile**, and then enter a name for the profile.
**Note**  
To attach tags to your device profile, after you create your profile, go to the [Profiles hub](https://console.aws.amazon.com/iot/home#/wireless/profiles) and then edit your profile to add this information.

   1. Specify the name of your destination that will route messages from your device to other AWS services. If you haven't already created a destination, go to the [Destinations hub](https://console.aws.amazon.com/iot/home#/wireless/destinations) to create your destination. You can then choose that destination for your Sidewalk device. For more information, see [Add a destination for your Sidewalk end device](iot-sidewalk-qsg-destination.md).

   1. Choose **Next** to continue adding your Sidewalk device.

1. 

**Associate Sidewalk device with AWS IoT thing (Optional)**

   You can optionally associate your Sidewalk device to an AWS IoT thing. IoT things are entries in the AWS IoT device registry. Things make it easier to search and manage your devices. Associating a thing with your device lets your device access other AWS IoT Core features.

   To associate your device with a thing, choose **Automatic thing registration**.

   1. Enter a unique name for the IoT thing that you want to associate your Sidewalk device. Thing names are case sensitive and must be unique in your AWS account and AWS Region.

   1. Provide any additional configurations for your IoT thing, such as using a thing type, or searchable attributes that can be used to filter from a list of things.

   1. Choose **Next** and verify the information about your Sidewalk device, and then choose **Create**.

## Add your Sidewalk device (CLI)
<a name="iot-sidewalk-add-device-api"></a>

To add your Sidewalk device and download the JSON files that will be used to provision your Sidewalk device, perform the following API operations.

**Topics**
+ [

### Step 1: Create a device profile
](#iot-sidewalk-profile-create)
+ [

### Step 2: Add your Sidewalk device
](#iot-sidewalk-device-create)

### Step 1: Create a device profile
<a name="iot-sidewalk-profile-create"></a>

To create a device profile in your AWS account, use the [https://docs.aws.amazon.com/iot-wireless/latest/apireference/API_CreateDeviceProfile.html](https://docs.aws.amazon.com/iot-wireless/latest/apireference/API_CreateDeviceProfile.html) API operation or the [https://docs.aws.amazon.com/cli/latest/reference/create-device-profile.html](https://docs.aws.amazon.com/cli/latest/reference/create-device-profile.html) CLI command. When creating your device profile, specify the name and provide any optional tags as name-value pairs.

For example, the following command creates a device profile for your Sidewalk devices:

```
aws iotwireless create-device-profile \ 
    --name sidewalk_profile --sidewalk {}
```

Running this command returns the Amazon Resource Name (ARN) and the ID of the device profile as output.

```
{
    "DeviceProfileArn": "arn:aws:iotwireless:us-east-1:123456789012:DeviceProfile/12345678-a1b2-3c45-67d8-e90fa1b2c34d",
    "DeviceProfileId": "12345678-a1b2-3c45-67d8-e90fa1b2c34d"
}
```

### Step 2: Add your Sidewalk device
<a name="iot-sidewalk-device-create"></a>

To add your Sidewalk device to your account for AWS IoT Core for Amazon Sidewalk, use the [https://docs.aws.amazon.com/iot-wireless/latest/apireference/API_CreateWirelessDevice.html](https://docs.aws.amazon.com/iot-wireless/latest/apireference/API_CreateWirelessDevice.html) API operation or the [https://docs.aws.amazon.com/cli/latest/reference/create-wireless-device.html](https://docs.aws.amazon.com/cli/latest/reference/create-wireless-device.html) CLI command. When creating your device, specify the following parameters, in addition to an optional name and description for your Sidewalk device.

**Note**  
If you want to associate your Sidewalk device with an AWS IoT thing, use the [https://docs.aws.amazon.com/iot-wireless/latest/apireference/API_AssociateWirelessDeviceWithThing.html](https://docs.aws.amazon.com/iot-wireless/latest/apireference/API_AssociateWirelessDeviceWithThing.html) API operation or the [https://docs.aws.amazon.com/cli/latest/reference/associate-wireless-device-with-thing.html](https://docs.aws.amazon.com/cli/latest/reference/associate-wireless-device-with-thing.html) CLI command.

The following command shows an example of creating a Sidewalk device:

```
aws iotwireless create-wireless-device \ 
     --cli-input-json "file://device.json"
```

The following shows the contents of the file `device.json`.

**Contents of device.json**

```
{
  "Type": "Sidewalk",
  "Name": "SidewalkDevice",  
  "DestinationName": "SidewalkDestination",
  "Sidewalk": {
    "DeviceProfileId": "12345678-a1b2-3c45-67d8-e90fa1b2c34d"    
    }
}
```

Running this command returns the device ID and Amazon Resource Name (ARN) as output.

```
{
    "Arn": "arn:aws:iotwireless:us-east-1:123456789012:WirelessDevice/23456789-abcd-0123-bcde-fabc012345678",
    "Id": "23456789-abcd-0123-bcde-fabc012345678"
}
```

#### (Optional) Create your Sidewalk end device and get the Sidewalk device location
<a name="iot-sidewalk-device-create-devicelocation"></a>

If you want to enable location data when you create your Sidewalk end device with AWS IoT Core for Amazon Sidewalk, enable positioning. Replace *LocationDestination* with the destination name to which you want to send the device location data to.

**Note**  
You must enable positioning to use the device location feature. If you enable device location for the Sidewalk-enabled device, your raw uplink payload won't be propagated to the destination.

```
// Add your Sidewalk device by using the device profile ID. 
aws iotwireless create-wireless-device
  --type "Sidewalk"
  --name "sidewalk_device" \
  --description "My Sidewalk Device Description" \
  --destination-name "UplinkDestination" \
  --positioning "Enabled" \
  --sidewalk DeviceProfileId="12345678-234a-45bc-67de-e8901234f0a1",Positioning={DestinationName="LocationDestination"}
```

**Note**  
For Bluetooth Low Energy based location, AWS IoT returns location coordinates based on the approximate location of nearby Sidewalk Gateways that are connected to Amazon Sidewalk and have the Community Finding feature enabled. Gateway Location Data is AWS Content and is provided to you solely for the purpose of assisting you in locating your devices that are connected to Amazon Sidewalk, and you must only use the data for that purpose. You must only use and access location data via the interface and functionality that we generally make available to you, and you must not attempt to re-identify, reverse engineer, or re-map any Gateway location data provided by us to you.

# Obtain device JSON files for provisioning
<a name="sidewalk-json-get"></a>

After you've added your Sidewalk device to AWS IoT Core for Amazon Sidewalk, download the JSON file that contains the information required to provision your end device. You can retrieve this information using the AWS IoT console or the AWS CLI. For more information about how to provision the device, see [Provisioning and registering your end device](https://docs.sidewalk.amazon/provisioning/) in the *Amazon Sidewalk documentation*.

## Obtain JSON file (console)
<a name="iot-sidewalk-json-get-console"></a>

To obtain the JSON file for provisioning your Sidewalk device:

1. Go to the [Sidewalk devices hub](https://console.aws.amazon.com/iot/home#/wireless/devices?tab=sidewalk).

1. Choose the device that you added to AWS IoT Core for Amazon Sidewalk to view its details.

1. Obtain the JSON file by choosing **Download device JSON file** in the details page of the device that you added.

   A `certificate.json` file will be downloaded that contains the required information for provisioning your end device. The following shows a sample JSON file. It contains the device certificates, private keys, the Sidewalk manufacturing serial number (SMSN), and the `DeviceTypeID`.

   ```
   {
     "p256R1": "grg8izXoVvQ86cPVm0GMyWuZYHEBbbH ... DANKkOKoNT3bUGz+/f/pyTE+xMRdIUBZ1Bw==",
     "eD25519": "grg8izXoVvQ86cPVm0GMyWuZYHEBbbHD ... UiZmntHiUr1GfkTOFMYqRB+Aw==",
     "metadata": {    
       "devicetypeid": "fe98",
       "applicationDeviceArn": "arn:aws:iotwireless:us-east-1:123456789012:WirelessDevice/897ce68e-3ca2-4ed0-85a2-30b0666c4052",
       "applicationDeviceId": "897ce68e-3ca2-4ed0-85a2-30b0666c4052",
       "smsn": "82B83C8B35E856F43CE9C3D59B418CC96B996071016DB1C3BE5901F0F3071A4A",
       "devicePrivKeyP256R1": "3e704bf8d319b3a475179f1d68c60737b28c708f845d0198f2d00d00c88ee018",
       "devicePrivKeyEd25519": "17dacb3a46ad9a42d5c520ca5f47f0167f59ce54d740aa13918465faf533b8d0"
     },
     "applicationServerPublicKey": "5ce29b89c2e3ce6183b41e75fe54e45f61b8bb320efbdd2abd7aefa5957a316b"
   }
   ```

In the details page of your Sidewalk device, you'll also see information about:
+ The device ID, its Amazon Resource Name (ARN), and details about any AWS IoT thing that the device is associated with.
+ The device profile and destination details.
+ The time at which the last uplink message was received from the device.
+ The status that indicates whether your device has been provisioned or registered.

## Obtain JSON file (CLI)
<a name="iot-sidewalk-json-get-api"></a>

To obtain the JSON files for provisioning your Sidewalk end device using the AWS IoT Core for Amazon Sidewalk API or the AWS CLI, save the API response from retrieving information about your device profile and wireless device as JSON files, such as *`wireless_device.json`* and *`device_profile.json`* temporarily. You'll use them for provisioning your Sidewalk device.

The following shows how to retrieve the JSON files.

**Topics**
+ [

### Step 1: Get device profile information as JSON file
](#iot-sidewalk-profile-get)
+ [

### Step 2: Get Sidewalk device information as JSON file
](#iot-sidewalk-get-device)

### Step 1: Get device profile information as JSON file
<a name="iot-sidewalk-profile-get"></a>

Use the [https://docs.aws.amazon.com/iot-wireless/latest/apireference/API_GetDeviceProfile.html](https://docs.aws.amazon.com/iot-wireless/latest/apireference/API_GetDeviceProfile.html) API operation or the [https://docs.aws.amazon.com/cli/latest/reference/get-device-profile.html](https://docs.aws.amazon.com/cli/latest/reference/get-device-profile.html) CLI command to get information about your device profile that you added to your account for AWS IoT Core for Amazon Sidewalk. To retrieve information about your device profile, specify the profile ID.

The API will then return information about the device profile matching the specified identifier and the device ID. You save this response information as a file, and give it a name such as *`device_profile.json`*.

The following shows an example CLI command:

```
aws iotwireless get-device-profile \ 
    --id "12345678-a1b2-3c45-67d8-e90fa1b2c34d" > device_profile.json
```

Running this command returns the parameters of your device profile, the application server public key, and the `DeviceTypeID`. The following shows a JSON file that contains a sample response information from the API. For more information about the parameters in the API response, see [https://docs.aws.amazon.com/iot-wireless/latest/apireference/API_GetDeviceProfile.html](https://docs.aws.amazon.com/iot-wireless/latest/apireference/API_GetDeviceProfile.html).

**`GetDeviceProfile` API response (Contents of `device_profile.json`)**

```
{
    "Arn": "arn:aws:iotwireless:us-east-1:123456789012:DeviceProfile/12345678-a1b2-3c45-67d8-e90fa1b2c34d",
    "Id": "12345678-a1b2-3c45-67d8-e90fa1b2c34d",
    "Name": "Sidewalk_profile",
    "LoRaWAN": null,
    "Sidewalk":
    {        
        "ApplicationServerPublicKey": "a123b45c6d78e9f012a34cd5e6a7890b12c3d45e6f78a1b234c56d7e890a1234",
        "DAKCertificateMetadata": [
            {                
                "DeviceTypeId: fe98,
                "CertificateId": "43564A6D2D50524F544F54595045",
                "FactorySupport": false,
                "MaxAllowedSignature": 1000                
            }
        ],
        "QualificationStatus": false
    }
}
```

### Step 2: Get Sidewalk device information as JSON file
<a name="iot-sidewalk-get-device"></a>

Use the [https://docs.aws.amazon.com/iot-wireless/latest/apireference/API_GetWirelessDevice.html](https://docs.aws.amazon.com/iot-wireless/latest/apireference/API_GetWirelessDevice.html) API operation or the [https://docs.aws.amazon.com/cli/latest/reference/get-wireless-device.html](https://docs.aws.amazon.com/cli/latest/reference/get-wireless-device.html) CLI command to get information about your Sidewalk device that you added to your account for AWS IoT Core for Amazon Sidewalk. To get information about your end device, provide the identifier of the wireless device that you obtained when adding your device. 

The API will then return information about the device matching the specified identifier and the device ID. Save this response information as a JSON file. Give the file a meaningful name, such as *`wireless_device.json`*.

The following shows an example of running the command using the CLI:

```
aws iotwireless get-wireless-device --identifier-type WirelessDeviceId \ 
    --identifier "23456789-abcd-0123-bcde-fabc012345678" > wireless_device.json
```

Running this command returns the device details, device certificates, private keys, and the Sidewalk manufacturing serial number (SMSN). The following shows an example output of running this command. For more information about the parameters in the API response, see [https://docs.aws.amazon.com/iot-wireless/latest/apireference/API_GetWirelessDevice.html](https://docs.aws.amazon.com/iot-wireless/latest/apireference/API_GetWirelessDevice.html).

**`GetWirelessDevice` API response (Contents of `wireless_device.json`)**

```
{
    "Arn": "arn:aws:iotwireless:us-east-1:123456789012:WirelessDevice/23456789-abcd-0123-bcde-fabc012345678",
    "Description": "test-description",
    "DestinationName": "SidewalkDestination",
    "Id": "23456789-abcd-0123-bcde-fabc012345678",
    "LoRaWAN": "null",
    "Name" : "test-name",
    "Positioning": "Enabled"
    "Sidewalk": {        
        "CertificateId": "4C7438772D50524F544F54595045",
        "DeviceCertificates": [
            {
                "SigningAlg": "Ed25519",
                "Value": "WUqB/E50VP7oZpHtYoBgpzJXYvhv51y/DBIfzNhrleo4UzOWCsIzbaJwft
                            +IPBSUQthDifJDYik0DuU1jLvuR8cpK7YyI7cUD/oZG+4Pro/s3nAIhy
                            XmhUlepmbveVxM8boiTiaUlL4iB9DoKrB41pWdHeg7hR8BDrE1m4sf5Q
                            9ZUwDy5BqMafeW0RUKZMVunpChji0dwC5VoSSb8IT7V+bKTJXXdZ8lP1
                            1jsiuJfwF64Eq1NCe2qKb7gql5u+qBE7vatDOSonwN56I6Ah8HWYRSyJ
                            Tk7DSJKknSY7KGyLjs0qMI8L8AeJ++UIO/jOsGnhC6Ku1ba62bEPmIBr
                            +889NhOngiIt1+1DrSOO59a1PLYqfVa5ejKq0tzzbyNG/m/oW72kkGGH
                            Ruec2zOXEO86kf4X0qzFfTKoo/6lt67XfXIQkO4wApCgJ8AHwHa3xz+d
                            h+W6mFwYFRqrQQT8s0SjSuDtLCaqZhnch47MZk7E/itqP4JnJ7RsJHWx
                            XsG2gTNlRghfG+zhpKzVVdvVVZeZ22f2WZ2QoGlzXxrW0/b7mqpO2l+8
                            fzRYYdqAp1AAADGz+gFBeX/ZNN8VJwnsNfgzj4me1HgVJdUo4W9kvx9c
                            r2jHWkC3Oj/bdBTh1+yBjOC53yHlQK/l1GHrEWiWPPnE434LRxnWkwr8
                            EHD4oieJxC8fkIxkQfj+gHh"
            },
            {
                "SigningAlg": "P256r1",
                "Value": "hDdkJw9L2uMCORjImjMHqzNR6nYYh6QKncSl5GthQNmHmGU8a+SOqDXWwDNt3
                            VSntpbTTQl7cMIusqweQo+JPXXWElbGh7eaxPGz4ZeF5yM2cqVNUrQr1l
                            X/6lZ+OLuycrFrLzzB9APi0NIMLqV/Rt7XJssHQs2RPcT1ul/2XVpa6zt
                            ULJeQi2JwhTb/k48wbh/EvafG/ibrIp4O3ZDa4f+5SVWvbY5eyDDXcohv
                            z/OcCtuRjAkzKBCvIjBDnCv1McjVdCO3+utizGntfhAo1RZstnOoRkgVF
                            2WuMT9IrUmzYximuTXUmWtjyFSTqgNBZwHWUTlMmjlpLCVzZQWM4zOisX
                            UAAALPsP34BS6EzJO5AsS5pC7QTpjBtAbLN9SdXOT9w4H1x8Nkp0ujLxW
                            RN37IEy0V9DrPK2w1g74uqWPfUPnSBjtvM55JnQpmm23WQNvHa1Vr6zmW
                            DjzjHpcNirPbzXyBlKEhkX4xylaSMnm4UrVXtAMaAJ/csC4HPTKr3dazd
                            vEkhwGAAAIFByCjSp/5WHc4AhsyjMvKCsZQiKgiI8ECwjfXBaSZdY4zYs
                            RlO3FC428H1atrFChFCZT0Bqt5LPXD38bMSB+vAUJiP8XqiEdXeqf2mYM
                            J5ykoDpwkve/cUQfPpjzFQlQfvwjBwiJDANKkOKoNT3bUGz+/f/pyTE+x
                            MRdIUBZ1Bw=="
            }
        ],
        "DeviceProfileId":"0ff5b0c6-f149-4498-af34-21993acd52a7",
        "Positioning": {
                        "DestinationName": SidewalkLocationDestination,
                        },
        "PrivateKeys": [
            {
                "SigningAlg": "Ed25519",
                "Value": "2c24d4572327f23b9bef38097137c29224a9e979081b3d90124ac9dfa477934e"
            },
            {
                "SigningAlg": "P256r1",
                "Value": "38d526f29cfaf142f596deca187bd809ef71bc13435eedc885b63bb825d63def"
            }
        ],        
        "SidewalkManufacturingSn": "843764270F4BDAE3023918C89A3307AB3351EA761887A40A9DC
                            4A5E46B6140D9",
        "Status": "PROVISIONED"
    },
    "Type": "Sidewalk",
    
    ...
    
}
```

## Next steps
<a name="iot-sidewalk-json-next"></a>

Store the JSON files, *`wireless_device.json`* and *`device_profile.json`* temporarily, as you'll use them in the next step to provision and register your end device for connecting to the hardware platform. For more information, see [Provisioning and registering your end device](https://docs.sidewalk.amazon/provisioning/) in the *Amazon Sidewalk documentation*.

# Add a destination for your Sidewalk end device
<a name="iot-sidewalk-qsg-destination"></a>

**Note**  
For Bluetooth Low Energy based location, AWS IoT returns location coordinates based on the approximate location of nearby Sidewalk Gateways that are connected to Amazon Sidewalk and have the Community Finding feature enabled. Gateway Location Data is AWS Content and is provided to you solely for the purpose of assisting you in locating your devices that are connected to Amazon Sidewalk, and you must only use the data for that purpose. You must only use and access location data via the interface and functionality that we generally make available to you, and you must not attempt to re-identify, reverse engineer, or re-map any Gateway location data provided by us to you.

AWS IoT Core for Amazon Sidewalk destinations describe the AWS IoT rule or MQTT topic that processes a device's data for use by other AWS services and applications. Use AWS IoT rules or MQTT topics to process the data and device messages and route it to other services.

Because most Sidewalk devices don't send data to AWS IoT Core for Amazon Sidewalk in a format that can be used by AWS services, an AWS IoT rule must process it first. The AWS IoT rule contains the SQL statement that interprets the device's data and the topic rule actions that send the result of the SQL statement to the services that will use it.

The AWS IoT rule processes binary messages received from a device and converts the messages to other formats that make other services easy to use them. Destinations associate your Sidewalk end device with the rule that processes the device data to send to other AWS services. For more information about rules, see [Rules for AWS IoT](https://docs.aws.amazon.com/iot/latest/developerguide/iot-rules.html) in the *AWS IoT Core documentation*.

## How to create and use a destination
<a name="iot-sidewalk-destination-how"></a>

1. Create an AWS IoT rule and an IAM role for the destination. The AWS IoT rule specifies the rules that will process the device's data and routes it for use by other AWS services and your applications. The IAM role grants permission to access the rule.

1. Create a destination for your Sidewalk devices using the `CreateDestination` API operation. Specify the destination name, rule name, role name, and any optional parameters. The API will return a unique identifier for the destination, which you can specify when adding your end device to AWS IoT Core for Amazon Sidewalk.

The following shows how to create a destination, and an AWS IoT rule and IAM role for the destination.

**Topics**
+ [

## How to create and use a destination
](#iot-sidewalk-destination-how)
+ [

# Create a destination for your Sidewalk device
](iot-sidewalk-destination-create.md)
+ [

# Create an IAM role and IoT rule for your destination
](sidewalk-destination-rule-role.md)

# Create a destination for your Sidewalk device
<a name="iot-sidewalk-destination-create"></a>

You can add a destination to your account for AWS IoT Core for Amazon Sidewalk either from the using the [Destinations hub](https://console.aws.amazon.com/iot/home#/wireless/destinations) or using the `CreateDestination`. When creating your destination, specify:
+ A unique name for the destination to use for your Sidewalk end device.
**Note**  
If you already add your device using a destination name, you must use that name when creating your destination. For more information, see [Step 2: Add your Sidewalk device](iot-sidewalk-add-device.md#iot-sidewalk-device-create). 
+ The name of the AWS IoT rule that will process the device's data, and the topic to which messages are published.
+ An IAM role that grants the device's data permission to access the rule.

The following sections describe how to create the AWS IoT rule and IAM role for your destination.

## Create a destination (console)
<a name="iot-sidewalk-destination-create-console"></a>

To create a destination using the AWS IoT console, go to the [Destinations hub](https://console.aws.amazon.com/iot/home#/wireless/destinations) and choose **Add destination**.

![\[Add a Sidewalk destination using the AWS IoT console.\]](http://docs.aws.amazon.com/iot-wireless/latest/developerguide/images/iot-sidewalk-add-destination.PNG)


To process a device's data, specify the following fields when creating a destination, and then choose **Add destination**.
+ 

**Destination details**  
Enter a **Destination name** and an optional description for your destination.
+ 

**Rule name**  
The AWS IoT rule that is configured to evaluate messages sent by your device and process the device's data. The rule name will be mapped to your destination. The destination requires the rule to process the messages that it receives. You can choose for the messages to be processed by either invoking an AWS IoT rule or by publishing to the AWS IoT message broker.
  + If you choose **Enter a rule name**, enter a name, and then choose **Copy** to copy the rule name that you'll enter when creating the AWS IoT rule. You can either choose **Create rule** to create the rule now or navigate to the [Rules](https://console.aws.amazon.com/iot/home#/create/rule) Hub of the AWS IoT console and create a rule with that name.

    You can also enter a rule and use the **Advanced** setting to specify a topic name. The topic name is provided during rule invocation and is accessed by using the `topic` expression inside the rule. For more information about AWS IoT rules, see [AWS IoT rules](https://docs.aws.amazon.com/iot/latest/developerguide/iot-rules.html).
  + If you choose **Publish to AWS IoT message broker**, enter a topic name. You can then copy the MQTT topic name and multiple subscribers can subscribe to this topic to receive messages published to that topic. For more information, see [MQTT topics](https://docs.aws.amazon.com/iot/latest/developerguide/topics.html).

  For more information about AWS IoT rules for destinations, see [Create rules to process LoRaWAN device messages](https://docs.aws.amazon.com/iot-wireless/latest/developerguide/lorawan-destination-rules.html).
+ 

**Role name**  
The IAM role that grants the device's data permission to access the rule named in **Rule name**. In the console, you can create a new service role or select an existing service role. If you're creating a new service role, you can either enter a role name (for example, **SidewalkDestinationRole**), or leave it blank for AWS IoT Core for LoRaWAN to generate a new role name. AWS IoT Core for LoRaWAN will then automatically create the IAM role with the appropriate permissions on your behalf.

## Create a destination (CLI)
<a name="iot-sidewalk-destination-create-cli"></a>

To create a destination, use the [https://docs.aws.amazon.com/iot-wireless/latest/apireference/API_CreateDestination.html](https://docs.aws.amazon.com/iot-wireless/latest/apireference/API_CreateDestination.html) API operation or the [https://docs.aws.amazon.com/cli/latest/reference/iotwireless/create-destination.html](https://docs.aws.amazon.com/cli/latest/reference/iotwireless/create-destination.html) CLI command. For example, the following command creates a destination for your Sidewalk end device:

```
aws iotwireless create-destination --name SidewalkDestination \
    --expression-type RuleName --expression SidewalkRule \
    --role-arn arn:aws:iam::123456789012:role/SidewalkRole
```

Running this command returns the destination details, which include the Amazon Resource Name (ARN) and the destination name.

```
{
    "Arn": "arn:aws:iotwireless:us-east-1:123456789012:Destination/SidewalkDestination",
    "Name": "SidewalkDestination"
}
```

For more information about creating a destination, see [Create rules to process LoRaWAN device messages](https://docs.aws.amazon.com/iot/latest/developerguide/connect-iot-lorawan-destination-rules.html).

# Create an IAM role and IoT rule for your destination
<a name="sidewalk-destination-rule-role"></a>

Your destination can be an MQTT topic or an AWS IoT rule. In this example, you configure an AWS IoT rule as a destination.

AWS IoT rules send device messages to other services. AWS IoT rules can also process the binary messages received from a Sidewalk end device for other services to use. AWS IoT Core for Amazon Sidewalk destinations associate a wireless device with the rule that processes the device's message data to send to other services. The rule acts on the device's data as soon as AWS IoT Core for Amazon Sidewalk receives it. For all devices that send their data to the same service, you can create a destination that can be shared by all devices. You must also create an IAM role that grants permission to send data to the rule.

## Create an IAM role for your destination
<a name="iot-sidewalk-destination-role"></a>

Create an IAM role that grants AWS IoT Core for Amazon Sidewalk permission to send data to the AWS IoT rule. To create the role, use the [https://docs.aws.amazon.com/IAM/latest/APIReference/API_CreateRole.html](https://docs.aws.amazon.com/IAM/latest/APIReference/API_CreateRole.html) API operation or [https://docs.aws.amazon.com/cli/latest/reference/iam/create-role](https://docs.aws.amazon.com/cli/latest/reference/iam/create-role) CLI command. You can name the role as *`SidewalkRole`*.

```
aws iam create-role --role-name SidewalkRole \ 
    --assume-role-policy-document '{"Version": "2012-10-17",		 	 	 "Statement": [{ "Effect": "Allow", "Principal": {"Service": "lambda.amazonaws.com"}, "Action": "sts:AssumeRole"}]}'
```

You can also define the trust policy for the role using a JSON file.

```
aws iam create-role --role-name SidewalkRole \ 
    --assume-role-policy-document file://trust-policy.json
```

The following shows the contents of the JSON file.

**Contents of trust-policy.json**

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Service": "lambda.amazonaws.com"
      },
      "Action": "sts:AssumeRole"
    }
  ]
}
```

## Create a rule for your destination
<a name="iot-sidewalk-destination-rule"></a>

Use the AWS IoT Core API operation, [https://docs.aws.amazon.com/iot/latest/apireference/API_CreateTopicRule.html](https://docs.aws.amazon.com/iot/latest/apireference/API_CreateTopicRule.html), or the AWS CLI command, [https://docs.aws.amazon.com/cli/latest/reference/iot/create-topic-rule.html](https://docs.aws.amazon.com/cli/latest/reference/iot/create-topic-rule.html), to create a rule. The topic rule will be used by your destination to route the data received from your Sidewalk end device to other AWS services. For example, you can create a rule action that sends a message to a Lambda function. You can define the Lambda function such that it receives the application data from your device and uses base64 to decode the payload data so that it can be used by other applications.

The following steps show how you create the Lambda function and then a topic rule that sends a message to this function.

1. 

**Create execution role and policy**

   Create an IAM role that grants your function permission to access AWS resources. You can also define the trust policy for the role using a JSON file.

   ```
   aws iam create-role --role-name lambda-ex \ 
       --assume-role-policy-document file://lambda-trust-policy.json
   ```

   The following shows the contents of the JSON file.

   **Contents of lambda-trust-policy.json**  
****  

   ```
   {
     "Version":"2012-10-17",		 	 	 
     "Statement": [
       {
         "Effect": "Allow",
         "Principal": {
           "Service": "lambda.amazonaws.com"
         },
         "Action": "sts:AssumeRole"
       }
     ]
   }
   ```

1. 

**Create and test Lambda function**

   Perform the following steps to create a AWS Lambda function that base64 decodes the payload data.

   1. Write the code for decoding the payload data. For example, you can use the following sample Python code. Specify a name for the script, such as *`base64_decode.py`*.

      **Contents of base64\$1decode.py**

      ```
      // -----------------------------------------------------------
      // ----- Python script to decode incoming binary payload -----
      // -----------------------------------------------------------
      import json
      import base64
      
      def lambda_handler(event, context):
      
          message = json.dumps(event)
          print (message)
      
          payload_data = base64.b64decode(event["PayloadData"])
          print(payload_data)
          print(int(payload_data,16))
      ```

   1. Create a deployment package as a zip file that contains the Python file and name it as `base64_decode.zip`. Use the `CreateFunction` API or the `create-function` CLI command to create a Lambda function for the sample code, *`base64_decode.py`*.

   1. 

      ```
      aws lambda create-function --function-name my-function \
      --zip-file fileb://base64_decode.zip --handler index.handler \ 
      --runtime python3.9 --role arn:aws:iam::123456789012:role/lambda-ex
      ```

      You should see the following output. You'll use the Amazon Resource Name (ARN) value from the output, `FunctionArn`, when creating the topic rule.

      ```
      {
          "FunctionName": "my-function",
          "FunctionArn": "arn:aws:lambda:us-east-1:123456789012:function:my-function",
          "Runtime": "python3.9",
          "Role": "arn:aws:iam::123456789012:role/lambda-ex",
          "Handler": "index.handler",
          "CodeSha256": "FpFMvUhayLkOoVBpNuNiIVML/tuGv2iJQ7t0yWVTU8c=",
          "Version": "$LATEST",
          "TracingConfig": {
              "Mode": "PassThrough"
          },
          "RevisionId": "88ebe1e1-bfdf-4dc3-84de-3017268fa1ff",
          ...
      }
      ```

   1. To get logs for an invocation from the command line, use the `--log-type` option with the `invoke` command. The response includes a LogResult field that contains up to 4 KB of base64-encoded logs from the invocation. 

      ```
      aws lambda invoke --function-name my-function out --log-type Tail
      ```

      You should receive a response with a `StatusCode` of 200. For more information about creating and using Lambda functions from the AWS CLI, see [Using Lambda with the AWS CLI](https://docs.aws.amazon.com/lambda/latest/dg/gettingstarted-awscli.html).

1. Create a topic rule

   Use the `CreateTopicRule` API or the `create-topic-rule` CLI command to create a topic rule that sends a message to this Lambda function. You can also add a second rule action that republishes to an AWS IoT topic. Name this topic rule as `Sidewalkrule`.

   ```
   aws iot create-topic-rule --rule-name Sidewalkrule \ 
       --topic-rule-payload file://myrule.json
   ```

   You can use the `myrule.json` file to specify more details about the rule. For example, the following JSON file shows how to republish to an AWS IoT topic and send a message to a Lambda function.

   ```
   {
       "sql": "SELECT * ",
       "actions": [
          {
               // You obtained this functionArn when creating the Lambda function using the 
               // create-function command.
               "lambda": {
                   "functionArn": "arn:aws:lambda:us-east-1:123456789012:function:my-function"
                }
           },
           {
               // This topic can be used to observe messages exchanged between the device and 
               // AWS IoT Core for Amazon Sidewalk after the device is connected.
                "republish": {
                    "roleArn": "arn:aws:iam::123456789012:role/service-role/SidewalkRepublishRole",
                    "topic": "project/sensor/observed"
                }
           }
       ],
   }
   ```

# Connect your Sidewalk device and view uplink metadata format
<a name="iot-sidewalk-connect-uplink-metadata"></a>

In this tutorial, you'll use the MQTT test client to test the connectivity and see messages exhanged between your end device and the AWS Cloud. To receive messages, in the MQTT test client, subscribe to the topic specified when creating the IoT rule for your destination. You can also send a downlink message from AWS IoT Core for Amazon Sidewalk to your device using the `SendDataToWirelessDevice` API operation. You can verify that the message was delivered by enabling the message delivery status event notification.

**Note**  
For information about connecting your hardware platform and setting it up, see [Provisioning and registering your end device](https://docs.sidewalk.amazon/provisioning/) and [Setting up the hardware development kit (HDK)](https://docs.sidewalk.amazon/getting-started/sidewalk-onboard-prereq-hdk.html) in the *Amazon Sidewalk documentation*.

## Send downlink messages to your end device
<a name="iot-sidewalk-downlink-message"></a>

Use the [https://docs.aws.amazon.com/iot-wireless/latest/apireference/API_SendDataToWirelessDevice.html](https://docs.aws.amazon.com/iot-wireless/latest/apireference/API_SendDataToWirelessDevice.html) API operation or the [https://docs.aws.amazon.com/cli/latest/reference/iotwireless/send-data-to-wireless-device.html](https://docs.aws.amazon.com/cli/latest/reference/iotwireless/send-data-to-wireless-device.html) CLI command to send downlink messages from AWS IoT Core for Amazon Sidewalk to your Sidewalk end device. Following shows an example of how to run this command. The payload data is the binary to be sent, encoded in base64.

```
aws iotwireless send-data-to-wireless-device \
    --id "<Wireless_Device_ID>" \
    --payload-data "SGVsbG8gVG8gRGV2c2lt" \
    --wireless-metadata Sidewalk={Seq=1,AckModeRetryDurationSecs=10}
```

Following shows a sample output of running this command, which is an ID of the downlink message sent to the device.

```
{
    MessageId: "6011dd36-0043d6eb-0072-0008"
}
```

**Note**  
The `SendDataToWirelessDevice` API can return a message ID but the message might not be successfully delivered. To check the status of the message that was sent to the device, you can enable message delivery status events for your Sidewalk accounts and devices. For information about how to enable this event, see [Event notifications for Sidewalk resources](iot-wireless-events-notifications.md#iot-sidewalk-events). For more information about this event type, see [Message delivery events](https://docs.aws.amazon.com/iot/latest/developerguide/iot-sidewalk-message-delivery-events.html).

## View format of uplink messages from the device
<a name="iot-sidewalk-uplink-metadata"></a>

After you've connected your device, you can subscribe to the topic (for example, *`project/sensor/observed`*) that you specified when creating the destination rule, and observe uplink messages from the device. 

If you specified a topic name when creating your destination, you can subscribe to the topic to monitor uplink messages from your end device. Go to the [MQTT test client](https://console.aws.amazon.com/iot/home#/test) on the **Test** page of the AWS IoT console, enter the topic name (for example, *`project/sensor/observed`*), and then choose **Subscribe**.

The following example shows the format of the uplink messages that are sent from Sidewalk devices to AWS IoT. The `WirelessMetadata` contains metadata about the message request.

```
{
   "PayloadData":"ZjRlNjY1ZWNlNw==",
   "WirelessDeviceId":"wireless_device_id",   
   "WirelessMetadata":{
      "Sidewalk":{
         "CmdExStatus":"Cmd",
         "SidewalkId":"device_id",
         "Seq":0,
         "MessageType":"messageType",
         "Timestamp": "2024-10-21T19:33:01.295912052Z",
         "Rssi": -22,
         "LinkType": "LoRa"
      }
    }
}
```

The following table shows a definition of the different parameters in the uplink metadata. The `device-id` is the ID of the wireless device, such as `ABCDEF1234` and the `messageType` is the type of uplink message that's received from the device.


**Sidewalk uplink metadata parameters**  

| Parameter | Description | Type | Required | 
| --- | --- | --- | --- | 
| PayloadData |  The message payload that is sent from the wireless device.   | String | Yes | 
| WirelessDeviceID | The identifier of the wireless device that's sending the data | String | Yes | 
| Sidewalk.CmdExStatus |  Command runtime status. Response-type messages shall include the status code, `COMMAND_EXEC_STATUS_SUCCESS`. However, notifications might not include the status code.  | Enumeration | No | 
| Seq |  The message sequence number.  | Integer | Yes | 
| Sidewalk.NackExStatus |  Response nack status, which can be `RADIO_TX_ERROR` or `MEMORY_ERROR`.   | Array of strings | No | 
| Timestamp |  The time when the Sidewalk device sent an uplink request.  | Timestamp | Yes | 
| Rssi |  Received Signal Strength Indicator, which is a measurement of a wireless network's signal strength  | Integer | No | 
| LinkType |  The type of link that was used to perform the uplink which can be one of `LoRa`, `BLE`, or `FSK`  | String | No | 

# Using AWS Location with Sidewalk Devices
<a name="iot-device-location-feature-sidewalk-device"></a>

AWS IoT Core for Amazon Sidewalk provides location capabilities for your Sidewalk devices, enabling you to track and monitor device positions. Geolocation is handled by the Amazon Sidewalk stack on the end device and triggered by the end device itself. The device collects location data and sends it to AWS IoT Core for Amazon Sidewalk, where you can retrieve it using AWS services.

**Note**  
To use location capabilities with Sidewalk devices, ensure you are using Amazon Sidewalk SDK version 1.19 or later on your end device. For details on implementing geolocation on the device side, see the [Amazon Sidewalk Location Library Developer Guide](https://docs.sidewalk.amazon/assets/pdf/Amazon_Sidewalk_Location_Library_Developer_Guide-1.0-rev-A.pdf).

## Location resolution methods
<a name="sidewalk-location-methods"></a>

Sidewalk devices support three methods for resolving device location:

WiFi-based location  
The device scans nearby WiFi access points and sends their BSSID information and signal strengths. AWS uses this information to estimate the device's location based on known WiFi access point locations.

GNSS (Global Navigation Satellite System)  
The device captures raw GNSS signals from GPS or other satellite navigation systems and sends this data to the cloud, where AWS solves for the device's position.

BLE (Bluetooth Low Energy) based location  
AWS IoT returns location coordinates based on the approximate location of nearby Amazon Sidewalk Gateways that are connected to Amazon Sidewalk and have the Community Finding feature enabled. Gateway Location Data is AWS Content and is provided to you solely for the purpose of assisting you in locating your devices that are connected to Amazon Sidewalk, and you must only use the data for that purpose. You must only use and access location data via the interface and functionality that we generally make available to you, and you must not attempt to re-identify, reverse engineer, or re-map any Gateway location data provided by us to you.

The location resolution method used depends on your device's capabilities and configuration. Your end device determines which method to use based on its hardware capabilities and the Amazon Sidewalk SDK configuration.

## Enable location for Sidewalk devices
<a name="sidewalk-location-enable"></a>

You can enable location capabilities when creating a new Sidewalk device or update an existing device to enable positioning. When you enable positioning, you must also specify a destination for the location data.

**Important**  
If you enable device location for the Sidewalk-enabled device, your raw uplink payload won't be propagated to the uplink destination. Location data will be sent to the location destination instead.

### Enable location (console)
<a name="sidewalk-location-enable-console"></a>

You can enable location capabilities for your Sidewalk device using the AWS IoT console when creating a new device or updating an existing device.

#### Create a new device with location enabled
<a name="sidewalk-location-enable-console-new"></a>

To create a new Sidewalk device with location capabilities:

1. Go to the [Sidewalk devices hub](https://console.aws.amazon.com/iot/home#/wireless/devices?tab=sidewalk) and choose **Provision device**.

1. In the **Specify device details** section, enter the device name, choose a device profile, and specify the uplink destination name.

1. In the **Geolocation** section, select **Activate positioning** to enable location capabilities for the device.  
![\[alt text not found\]](http://docs.aws.amazon.com/iot-wireless/latest/developerguide/images/iot-sidewalk-enable-positioning-console.png)

1. In the **Position data destination** field, select the name of the destination where location data will be sent. This destination must have an AWS IoT rule configured to process location data.  
![\[alt text not found\]](http://docs.aws.amazon.com/iot-wireless/latest/developerguide/images/iot-sidewalk-location-destination.png)

1. Complete the remaining steps to create your device, and then choose **Create**.

#### Update an existing device to enable location
<a name="sidewalk-location-enable-console-existing"></a>

To enable location capabilities for an existing Sidewalk device:

1. Go to the [Sidewalk devices hub](https://console.aws.amazon.com/iot/home#/wireless/devices?tab=sidewalk).

1. Choose the device that you want to update to view its details.  
![\[alt text not found\]](http://docs.aws.amazon.com/iot-wireless/latest/developerguide/images/iot-sidewalk-device-edit.png)

1. In the device details page, choose select **Activate positioning** to enable location capabilities for the device.  
![\[alt text not found\]](http://docs.aws.amazon.com/iot-wireless/latest/developerguide/images/iot-sidewalk-enable-positioning-console.png)

1. In the **Position data destination** field, select the name of the destination where location data will be sent.  
![\[alt text not found\]](http://docs.aws.amazon.com/iot-wireless/latest/developerguide/images/iot-sidewalk-location-destination.png)

1. Choose **Save** to apply the changes.

### Enable location (CLI)
<a name="sidewalk-location-enable-cli"></a>

You can enable location capabilities using the AWS CLI when creating a new device or updating an existing device.

#### Create a new device with location enabled
<a name="sidewalk-location-enable-cli-new"></a>

To create a Sidewalk device with location enabled, use the [https://docs.aws.amazon.com/iot-wireless/latest/apireference/API_CreateWirelessDevice.html](https://docs.aws.amazon.com/iot-wireless/latest/apireference/API_CreateWirelessDevice.html) API operation or the [https://docs.aws.amazon.com/cli/latest/reference/iotwireless/create-wireless-device.html](https://docs.aws.amazon.com/cli/latest/reference/iotwireless/create-wireless-device.html) CLI command with the `--positioning` parameter set to `Enabled`.

```
aws iotwireless create-wireless-device \
    --type "Sidewalk" \
    --name "sidewalk_device" \
    --destination-name "UplinkDestination" \
    --positioning "Enabled" \
    --sidewalk DeviceProfileId="12345678-a1b2-3c45-67d8-e90fa1b2c34d",Positioning={DestinationName="LocationDestination"}
```

#### Update an existing device to enable location
<a name="sidewalk-location-enable-cli-existing"></a>

To enable location for an existing Sidewalk device, use the [https://docs.aws.amazon.com/iot-wireless/latest/apireference/API_UpdateWirelessDevice.html](https://docs.aws.amazon.com/iot-wireless/latest/apireference/API_UpdateWirelessDevice.html) API operation or the [https://docs.aws.amazon.com/cli/latest/reference/iotwireless/update-wireless-device.html](https://docs.aws.amazon.com/cli/latest/reference/iotwireless/update-wireless-device.html) CLI command.

```
aws iotwireless update-wireless-device \
    --id "23456789-abcd-0123-bcde-fabc012345678" \
    --positioning "Enabled" \
    --sidewalk Positioning={DestinationName="LocationDestination"}
```

## Retrieve location data
<a name="sidewalk-location-retrieve"></a>

After enabling location capabilities for your Sidewalk device, you can retrieve location data using two methods: the `GetResourcePosition` API or by subscribing to an MQTT topic.

### Retrieve location using GetResourcePosition API
<a name="sidewalk-location-retrieve-api"></a>

Use the [https://docs.aws.amazon.com/iot-wireless/latest/apireference/API_GetResourcePosition.html](https://docs.aws.amazon.com/iot-wireless/latest/apireference/API_GetResourcePosition.html) API operation or the [https://docs.aws.amazon.com/cli/latest/reference/iotwireless/get-resource-position.html](https://docs.aws.amazon.com/cli/latest/reference/iotwireless/get-resource-position.html) CLI command to retrieve the most recent location data for your device.

The following example shows how to retrieve location data for a Sidewalk device:

```
aws iotwireless get-resource-position \
    --resource-identifier "23456789-abcd-0123-bcde-fabc012345678" \
    --resource-type "WirelessDevice"
```

The API returns the device's location coordinates and additional metadata. The following shows an example response:

```
{
    "GeoJsonPayload": blob,
    "Timestamp": "2024-10-21T19:33:01.295912052Z"
}
```

The `GeoJsonPayload` contains the location data in GeoJSON format, which includes the device's latitude, longitude, and accuracy information.

### Retrieve location using MQTT
<a name="sidewalk-location-retrieve-mqtt"></a>

You can subscribe to an MQTT topic to receive location data as it becomes available from your Sidewalk device. This method allows you to process location data in real-time using AWS IoT rules.

1. 

**Create a destination for location data**

   Create a destination with an AWS IoT rule that processes location data. For information about creating destinations, see [Add a destination for your Sidewalk end device](iot-sidewalk-qsg-destination.md).

1. 

**Subscribe to the MQTT topic**

   Go to the [MQTT test client](https://console.aws.amazon.com/iot/home#/test) on the **Test** page of the AWS IoT console. Enter the topic name that you specified in your location destination rule (for example, *`project/sensor/location`*), and then choose **Subscribe**.

1. 

**View location messages**

   When your device sends location data, you'll see messages published to the topic. The following shows an example of a location message:

   ```
   {
       "type": Point,
       "coordinates": [
           11.11,
           22.22,
           33.33
       ],
       "properties": {
           "timestamp": 2026-00-00T00:00:00Z
       }
   }
   ```