Use ListThings with an AWS SDK or CLI - AWS IoT Core

Use ListThings with an AWS SDK or CLI

The following code examples show how to use ListThings.

.NET
SDK for .NET (v4)
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

/// <summary> /// Lists IoT Things with pagination support. /// </summary> /// <returns>List of Things, or empty list if listing failed.</returns> public async Task<List<ThingAttribute>> ListThingsAsync() { try { // Use pages of 10. var request = new ListThingsRequest() { MaxResults = 10 }; var response = await _amazonIoT.ListThingsAsync(request); // Since there is not a built-in paginator, use the NextMarker to paginate. bool hasMoreResults = true; var things = new List<ThingAttribute>(); while (hasMoreResults) { things.AddRange(response.Things); // If NextMarker is not null, there are more results. Get the next page of results. if (!String.IsNullOrEmpty(response.NextMarker)) { request.Marker = response.NextMarker; response = await _amazonIoT.ListThingsAsync(request); } else hasMoreResults = false; } _logger.LogInformation($"Retrieved {things.Count} Things"); return things; } catch (Amazon.IoT.Model.ThrottlingException ex) { _logger.LogWarning($"Request throttled, please try again later: {ex.Message}"); return new List<ThingAttribute>(); } catch (Exception ex) { _logger.LogError($"Couldn't list Things. Here's why: {ex.Message}"); return new List<ThingAttribute>(); } }
  • For API details, see ListThings in AWS SDK for .NET API Reference.

CLI
AWS CLI

Example 1: To list all things in the registry

The following list-things example lists the things (devices) that are defined in the AWS IoT registry for your AWS account.

aws iot list-things

Output:

{ "things": [ { "thingName": "ThirdBulb", "thingTypeName": "LightBulb", "thingArn": "arn:aws:iot:us-west-2:123456789012:thing/ThirdBulb", "attributes": { "model": "123", "wattage": "75" }, "version": 2 }, { "thingName": "MyOtherLightBulb", "thingTypeName": "LightBulb", "thingArn": "arn:aws:iot:us-west-2:123456789012:thing/MyOtherLightBulb", "attributes": { "model": "123", "wattage": "75" }, "version": 3 }, { "thingName": "MyLightBulb", "thingTypeName": "LightBulb", "thingArn": "arn:aws:iot:us-west-2:123456789012:thing/MyLightBulb", "attributes": { "model": "123", "wattage": "75" }, "version": 1 }, { "thingName": "SampleIoTThing", "thingArn": "arn:aws:iot:us-west-2:123456789012:thing/SampleIoTThing", "attributes": {}, "version": 1 } ] }

Example 2: To list the defined things that have a specific attribute

The following list-things example displays a list of things that have an attribute named wattage.

aws iot list-things \ --attribute-name wattage

Output:

{ "things": [ { "thingName": "MyLightBulb", "thingTypeName": "LightBulb", "thingArn": "arn:aws:iot:us-west-2:123456789012:thing/MyLightBulb", "attributes": { "model": "123", "wattage": "75" }, "version": 1 }, { "thingName": "MyOtherLightBulb", "thingTypeName": "LightBulb", "thingArn": "arn:aws:iot:us-west-2:123456789012:thing/MyOtherLightBulb", "attributes": { "model": "123", "wattage": "75" }, "version": 3 } ] }

For more information, see How to Manage Things with the Registry in the AWS IoT Developers Guide.

  • For API details, see ListThings in AWS CLI Command Reference.

Rust
SDK for Rust
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

async fn show_things(client: &Client) -> Result<(), Error> { let resp = client.list_things().send().await?; println!("Things:"); for thing in resp.things.unwrap() { println!( " Name: {}", thing.thing_name.as_deref().unwrap_or_default() ); println!( " Type: {}", thing.thing_type_name.as_deref().unwrap_or_default() ); println!( " ARN: {}", thing.thing_arn.as_deref().unwrap_or_default() ); println!(); } println!(); Ok(()) }
  • For API details, see ListThings in AWS SDK for Rust API reference.

For a complete list of AWS SDK developer guides and code examples, see Using AWS IoT with an AWS SDK. This topic also includes information about getting started and details about previous SDK versions.