- Retrieve all products
- Create a new product
- Retrieve a single product by its ID
- Delete a product
- Update a product
/products returns all products stored in the DynamoDB table, while a POST request to /products creates a new product. Other endpoints handle retrieval by ID, deletion, and updates.
Below is an initial Express setup with placeholders for each operation:

A scan operation retrieves all items from the table. However, it is less efficient than a query and consumes more read capacity.
/products endpoint. In the snippet below, we import the necessary DynamoDB classes and demonstrate a simple scan operation:
Retrieving All Products
Now, let’s add logic to the GET/products endpoint. We perform a scan on the “products” table and then return the resulting items as JSON:
npm start with nodemon), testing this endpoint with an API tester (like Postman) will return all products stored in your DynamoDB table. The response from DynamoDB includes metadata and primarily lists items under the property Items.
An example output might look like:
Creating a New Product
To allow users to create a product, we handle a POST request to/products. The application extracts product data from the request body, generates a unique ID using UUID, and uses DynamoDB’s PutCommand to add the new item.
Ensure you import
PutCommand from @aws-sdk/lib-dynamodb before using it.http://localhost:3000/products with a JSON body like:

Retrieving a Single Product
To fetch a product by its ID, we use the GET/products/:id endpoint. The following code extracts the id from the URL, then utilizes the GetCommand to retrieve the product from DynamoDB:
Deleting a Product
The DELETE/products/:id endpoint handles product deletion. It extracts the product ID from the request URL and uses the DeleteCommand to remove the corresponding item from the table:
Updating a Product
To update an existing product, use the PUT/products/:id endpoint. In this example, we overwrite the existing item using the PutCommand with new values provided in the request body. (Alternatively, you could use DynamoDB’s UpdateCommand for partial updates.)
Querying Products by Category
If users want to filter products by category, you have two primary options. Although scanning with client-side filtering is feasible, using a Global Secondary Index (GSI) is more efficient. In this example, we assume that a GSI named “category-index” exists with “category” as the partition key. Modify the GET/products endpoint to check for a query parameter and execute a query if a category is provided:
?category=electronics), it efficiently returns only the products within that category using the GSI.

This demonstration illustrates how to integrate various DynamoDB operations—scans, queries with a Global Secondary Index, and basic CRUD operations—within a Node.js Express application. By following these examples, you can seamlessly work with DynamoDB operations in your own projects. For more information, consider exploring additional resources: