Python & Azure Cosmos DB Integration: Create an E-Commerce DB Management Tool
Published Jan 15 2024 12:00 AM 1,440 Views
Brass Contributor

Creating an effective Python command-line tool to manage an e-commerce product catalog with Azure Cosmos DB can streamline operations significantly. This tutorial will guide you through creating a Python command-line tool that interfaces with Azure Cosmos DB to manage an e-commerce product catalog. The tool simplifies CRUD operations, making it accessible for non-technical staff to maintain up-to-date product listings.




- Python 3.x installed

- Azure account and Azure Cosmos DB setup

- Basic knowledge of Python and command-line operations

- Git and GitHub account for version control


Step 1: Setting Up Azure Cosmos DB


  1. Log in to Azure Portal: Access your Azure account and navigate to the Azure portal.
  2. Create a New Cosmos DB Account:

   - Click on "Create a resource".




   - Search for and select "Azure Cosmos DB".




   - Select an API, such as Core (SQL) for document storage. 




   - Click "Create" and fill in the details like subscription, resource group, and account name.





   - Review and create your Cosmos DB account.





  1. Acquire Connection Strings:

   - Once the account is set up, go to the "Keys" section under "Settings".





   - Copy the Primary Connection String, which will be used in your tool to access the database.


Step 2: Preparing the Database and Container


     1. Create a Database:

   - Inside your Cosmos DB account, click on "Data Explorer".



   - Click on the "New Container" button.



   - You will be prompted to create a new database. Enter a unique name for your database. (Include the relevant image here)


  1. Create a Container:

   - After specifying the database name, you will be prompted to create a container.

   - Enter a unique name for your container.



   - Choose a partition key. For simplicity, you can use `/id` as the partition key for this guide.


Step 3: Designing the Command-Line Tool


  1. Initialize Your Project:

   - Create a new directory for your project on your local machine.

   - Initialize a Git repository and connect it to a new GitHub repository.


  1. Write Your Python Script:

   - Create a `` script where you'll write functions to interact with Cosmos DB using the `azure-cosmos` Python package.

   - Define functions like `add_product`, `update_product`, `get_product`, and `delete_product`.


Step 4: Implementing CRUD Operations


  1. Create Product:

   - Implement a function that takes product details in JSON format and adds them to the Cosmos DB.


  1. Read Product:

   - Create a function to retrieve product details by their unique ID.


  1. Update Product:

   - Write code to modify existing product details, such as price or stock levels.


  1. Delete Product:

   - Allow for the removal of product listings from the database.


Step 5: Interfacing with Cosmos DB


- Use the `azure-cosmos` Python package to establish a connection to your Cosmos DB instance using the connection string.

- Implement your CRUD operations using the Cosmos DB SDK methods like `upsert_item` for adding or updating and `delete_item` for removing entries.


Step 6: Packaging and Distribution


  1. Create a `requirements.txt` File:

   - List all the dependencies your tool requires, including `azure-cosmos`.



   - Write a `` file for packaging your tool. Include metadata like name, version, and entry points for your command-line interface.


Step 7: Testing Your Tool


- Test each function individually to ensure it interacts with Cosmos DB correctly.

- Perform end-to-end tests simulating real-world scenarios, like adding a new product catalog or updating stock levels.


Step 8: Deploying Your Tool


- Push your final code to your GitHub repository.

- Use `pip` to install your command-line tool on the machines of end-users.

- Provide documentation on how to use the tool, including example commands and expected outputs.


Step 9: Verifying Deployment in Azure


   - Navigate to your resource group in the Azure portal.

   - Click on the "Deployments" section.

   - Look for the deployment named 'Microsoft.Azure.CosmosDB' followed by a timestamp.



   - Confirm that the status is "Succeeded" which indicates your Cosmos DB setup was successful.




By following this guide, you can create a Python command-line tool that enables efficient management of an e-commerce product catalog using Azure Cosmos DB. This tool will allow your product management team to perform database operations with ease, improving productivity and ensuring data accuracy. Start building your Python command-line tool today to take advantage of the powerful features offered by Azure Cosmos DB, and revolutionize the way your e-commerce platform handles product management.

Version history
Last update:
‎Jan 15 2024 05:43 AM
Updated by: