{"id":10016,"date":"2025-04-25T09:33:41","date_gmt":"2025-04-25T16:33:41","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/cosmosdb\/?p=10016"},"modified":"2025-05-09T11:11:23","modified_gmt":"2025-05-09T18:11:23","slug":"getting-started-with-azure-cosmos-db-using-the-python-sdk","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cosmosdb\/getting-started-with-azure-cosmos-db-using-the-python-sdk\/","title":{"rendered":"Getting Started with Azure Cosmos DB Using the Python SDK"},"content":{"rendered":"<p>If you&#8217;re new to Azure Cosmos DB and looking to build applications with Python, you&#8217;re in the right place. I\u2019ve created a four-part video series designed to take you from the basics to practical, real-world querying techniques\u2014no prior experience required. Whether you\u2019re setting up your first database, learning how to model your data, or optimizing your queries, this series will help you build confidence with each step.<\/p>\n<p>This blog post is your companion guide. It introduces key concepts, outlines what each video covers, and provides helpful context so you can follow along more effectively. Whether you&#8217;re a student, a data enthusiast, or a backend developer exploring NoSQL for the first time, you&#8217;ll find value here.<\/p>\n<h2>\ud83d\udd0d Key Concepts<\/h2>\n<p>Before diving into the code and videos, here are some terms you need to know:<\/p>\n<p><strong>\ud83c\udf10 Azue Cosmos DB Account<\/strong><\/p>\n<p>An Azure \u00a0Cosmos DB account is the top-level resource that represents your entry point into the service. You can think of it like your overall database service instance. In the video series, mine is named bookrecommender, but you can name yours anything.<\/p>\n<p><strong>\ud83d\udcc1 Database<\/strong><\/p>\n<p>Inside your account, you\u2019ll create a database. This is where you logically group containers. In my demo, it\u2019s called BookRecommendationsDB.<\/p>\n<p><strong>\ud83d\udce6 Container<\/strong><\/p>\n<p>A container holds your JSON documents (items). It\u2019s the equivalent of a collection in MongoDB or a table in a relational DB. I created one named books.<\/p>\n<p><strong>\ud83d\udd11 Partition Key<\/strong><\/p>\n<p>This is super important for performance and scalability. A partition key determines how data is distributed. For this demo, I used \/category, which means items are partitioned based on the category field in each document.<\/p>\n<h2>\ud83d\udee0\ufe0f Prerequisites<\/h2>\n<p>Make sure you have the following setup before following along:<\/p>\n<ul>\n<li>\u2705 Azure Cosmos DB account using the NoSQL API<\/li>\n<li>\u2705 One database (e.g., BookRecommendationsDB)<\/li>\n<li>\u2705 One container (e.g., books) with \/category as the partition key<\/li>\n<li>\u2705 <a href=\"https:\/\/learn.microsoft.com\/en-us\/cli\/azure\/install-azure-cli\">Azure CLI<\/a> installed and logged in<\/li>\n<li>\u2705 <a href=\"https:\/\/learn.microsoft.com\/azure\/cosmos-db\/nosql\/how-to-grant-data-plane-access?tabs=built-in-definition%2Ccsharp&amp;pivots=azure-interface-cli\">Azure Identity<\/a> (a managed identity or service principal with access to Azure Cosmos DB)<\/li>\n<li>\u2705 Python 3.11 or higher<\/li>\n<li>\u2705 Required Python packages installed:<\/li>\n<\/ul>\n<pre class=\"prettyprint language-py\"><code class=\"language-py\">pip install azure-cosmos azure-identity python-dotenv<\/code><\/pre>\n<ul>\n<li>\u2705 .env file with your configuration (use .env-sample as a template)<\/li>\n<li>Clone the series GitHub repository:<\/li>\n<\/ul>\n<pre class=\"prettyprint language-default\"><code class=\"language-default\">git clone https:\/\/github.com\/madebygps\/cosmosdb-python-101<\/code><\/pre>\n<h2>\ud83d\udcfc Series Overview<\/h2>\n<p>Each video in the series focuses on a key aspect of using Azure Cosmos DB with Python. Below is a quick summary, but make sure to watch the videos for the full walkthroughs and code demos!<\/p>\n<p>We\u2019ve put them all in a <a href=\"https:\/\/youtube.com\/playlist?list=PLmamF3YkHLoIVZeBceWlb5by6S_hhe85c&amp;si=Z6h9WEi0Pb3IPLzY\">playlist<\/a> for easier access.<\/p>\n<h3>1. 5-Minute Setup Guide<\/h3>\n<p><em><iframe title=\"YouTube video player\" src=\"\/\/www.youtube.com\/embed\/vLPng5wlTsc?si=MA_CQTfA_Rtj89AK\" width=\"560\" height=\"315\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/em><\/p>\n<p>In this intro video, I show you how to:<\/p>\n<ol>\n<li>Authenticate with Azure using Azure Identity<\/li>\n<li>Connect to your Azure Cosmos DB account<\/li>\n<li>List all databases and containers<\/li>\n<\/ol>\n<p>Run it with: <code class=\"language-py\">python setup.py<\/code><\/p>\n<h3>2. Creating and Reading Documents<\/h3>\n<p><iframe title=\"YouTube video player\" src=\"\/\/www.youtube.com\/embed\/R9jjCUCsCeQ?si=XySBw2YFt5lkjecP\" width=\"560\" height=\"315\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n<p>Here, we dig into:<\/p>\n<ol>\n<li>Creating new items with nested objects and arrays<\/li>\n<li>Reading all items from a container<\/li>\n<li>Retrieving an item by ID and partition key<\/li>\n<\/ol>\n<p>Run it with: <span style=\"font-family: terminal, monaco, monospace;\">python creating_reading.py<\/span><\/p>\n<h3>3. Updating and Deleting Items<\/h3>\n<p><iframe title=\"YouTube video player\" src=\"\/\/www.youtube.com\/embed\/7MyEUVHEUR0?si=6IGWkRS_0txjx61S\" width=\"560\" height=\"315\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n<p>Learn how to:<\/p>\n<ol>\n<li>When to use upsert<\/li>\n<li>Update an existing document using <span style=\"font-family: terminal, monaco, monospace;\">replace_item<\/span><\/li>\n<li>Delete items by their ID and partition key<\/li>\n<\/ol>\n<p>Run it with: <span style=\"font-family: terminal, monaco, monospace;\">python updating_deleting.py<\/span><\/p>\n<h3>4. Querying<\/h3>\n<p><iframe title=\"YouTube video player\" src=\"\/\/www.youtube.com\/embed\/1WR-cN3iJlQ?si=F6osUefGCfl4JKIc\" width=\"560\" height=\"315\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n<p>This video dives deep into querying:<\/p>\n<ol>\n<li>WHERE clause filters<\/li>\n<li>Parameterized queries (safer and cleaner)<\/li>\n<li>Projections to return specific fields<\/li>\n<li>Array contains operations (even case-insensitive!)<\/li>\n<li>Cross-partition queries<\/li>\n<\/ol>\n<p>Run it with: <span style=\"font-family: terminal, monaco, monospace;\">python querying.py<\/span><\/p>\n<h2>\ud83c\udfaf Wrap-Up<\/h2>\n<p>This series is designed to help you get up and running quickly with <strong>Azure Cosmos DB using Python<\/strong>\u2014from setting up your environment to writing real-world queries that bring your data to life.<\/p>\n<p>Whether you&#8217;re just starting out with NoSQL or looking to deepen your understanding of scalable cloud databases, these videos and scripts provide a solid foundation. The goal is not just to teach you <em>how<\/em> to use the SDK, but also <em>why<\/em> certain design and query decisions matter when building modern, cloud-native applications.<\/p>\n<p>Go watch the videos, follow along with the code, and experiment with your own data. And if you find the content helpful, don\u2019t forget to subscribe and share it with a fellow Python developer who might benefit, too.<\/p>\n<p>This is just the beginning\u2014we\u2019ll be sharing more advanced scenarios, performance tips, and integration examples in upcoming videos. Stay tuned and keep building with Azure Cosmos DB!<\/p>\n<h2 id=\"leave-a-review\">Leave a review<\/h2>\n<p>Tell us about your Azure Cosmos DB experience! Leave a review on PeerSpot and we\u2019ll gift you $50.\u00a0<a href=\"https:\/\/peerspotdotcom.my.site.com\/proReviews\/?SalesOpportunityProduct=00kPy000004TKXJIA4&amp;productPeerspotNumber=30881&amp;CalendlyAccount=peerspot&amp;CalendlyFormLink=peerspot-product-reviews-ps-gc-vi-sf-50&amp;giftCard=50\" target=\"_blank\" rel=\"noopener\">Get started here<\/a>.<\/p>\n<h2 id=\"about-azure-cosmos-db\">About Azure Cosmos DB<\/h2>\n<p>Azure Cosmos DB is a fully managed and serverless NoSQL and vector database for modern app development, including AI applications. With its SLA-backed speed and availability as well as instant dynamic scalability, it is ideal for real-time NoSQL and MongoDB applications that require high performance and distributed computing over massive volumes of NoSQL and vector data.<\/p>\n<p><a href=\"https:\/\/cosmos.azure.com\/try\/\" target=\"_blank\" rel=\"noopener\">Try Azure Cosmos DB for free here.<\/a>\u00a0To stay in the loop on Azure Cosmos DB updates, follow us on\u00a0<a href=\"https:\/\/twitter.com\/AzureCosmosDB\" target=\"_blank\" rel=\"noopener\">X<\/a>,\u00a0<a href=\"https:\/\/aka.ms\/AzureCosmosDBYouTube\" target=\"_blank\" rel=\"noopener\">YouTube<\/a>, and\u00a0<a href=\"https:\/\/www.linkedin.com\/company\/azure-cosmos-db\/\" target=\"_blank\" rel=\"noopener\">LinkedIn<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you&#8217;re new to Azure Cosmos DB and looking to build applications with Python, you&#8217;re in the right place. I\u2019ve created a four-part video series designed to take you from the basics to practical, real-world querying techniques\u2014no prior experience required. Whether you\u2019re setting up your first database, learning how to model your data, or optimizing [&hellip;]<\/p>\n","protected":false},"author":154186,"featured_media":10020,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[14,1217],"tags":[],"class_list":["post-10016","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-core-sql-api","category-python-sdk"],"acf":[],"blog_post_summary":"<p>If you&#8217;re new to Azure Cosmos DB and looking to build applications with Python, you&#8217;re in the right place. I\u2019ve created a four-part video series designed to take you from the basics to practical, real-world querying techniques\u2014no prior experience required. Whether you\u2019re setting up your first database, learning how to model your data, or optimizing [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/posts\/10016","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/users\/154186"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/comments?post=10016"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/posts\/10016\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/media\/10020"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/media?parent=10016"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/categories?post=10016"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/tags?post=10016"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}