Skip to main content
Working with the Microsoft .NET SDK for Azure Cosmos DB This guide demonstrates common patterns for working with Azure Cosmos DB using the Microsoft .NET SDK: initializing a client, accessing a container, creating/upserting items, reading a single item, and querying multiple items. Each code snippet shows the typical sequence of operations and is accompanied by notes and portal screenshots for context. Quick overview — common steps:
  • Initialize a CosmosClient with your account endpoint and key.
  • Get a Container reference for the target database and container.
  • Create or upsert items (CreateItemAsync, UpsertItemAsync).
  • Read a single item by id and partition key (ReadItemAsync<T>).
  • Query items with SQL-like queries via GetItemQueryIterator<T> (returns a FeedIterator<T>).
Table: common SDK operations References:

Create items

Start by creating a CosmosClient and obtaining the Container object for the database and container where you’ll store documents. The following example shows:
  • Initializing the client and container.
  • Defining a simple Employee POCO.
  • Creating an item with CreateItemAsync.
  • Demonstrating UpsertItemAsync (insert-or-update).
If your container uses a partition key other than id, include it when creating or upserting items so the SDK routes the write correctly. Example: await container.CreateItemAsync(employeeRecord, new PartitionKey(employeeRecord.department));
Notes:
  • UpsertItemAsync is useful for idempotent writes — it inserts if the id is new, or replaces the existing item if the id exists.
  • Monitor RequestCharge from responses to understand RU consumption and tune throughput.

Read a single item

Reading an item requires the item’s id and its partition key value (if the container uses one). Use ReadItemAsync<T> to fetch a single document and access the resource via ItemResponse<T>.Resource.

Query multiple items (paged queries with FeedIterator)

To query many documents, use SQL-like syntax with GetItemQueryIterator<T>. The method returns a FeedIterator<T> that yields FeedResponse<T> batches — which enables efficient paging through large result sets.
This approach scales because the SDK retrieves pages of results from the service and you can process each page independently.

Creating a database and container in the Azure portal

If you don’t yet have a database and container, use the Azure portal Data Explorer to create them. The portal’s Overview page shows account essentials and quick access to Data Explorer.
A screenshot of the Microsoft Azure portal showing the Overview page for an Azure Cosmos DB account named "az204cosmosdb01." The page displays account essentials (status, read/write locations, throughput), monitoring charts, and a left-hand navigation menu with options like Activity log, Tags, and Data Explorer.
When creating a new container in the portal you configure:
  • Database id (create new or use existing).
  • Provisioned throughput (RU/s) or serverless/free options.
  • Container id and partition key path (for scale and routing).
  • Indexing policy and any unique keys.
Below is the New Container dialog in the Data Explorer.
A screenshot of the Azure Cosmos DB web portal showing the "Welcome to Azure Cosmos" home page with tiles like "Launch quick start" and "New Container." A right-side pane is open to the "New Container" creation form, showing fields for Container id, indexing, and partition key.

Bulk loading sample data (Data Migration Tool)

For test datasets or bulk imports, use the Cosmos DB Data Migration Tool (DMT). It supports sources such as JSON files, Amazon S3, and Azure Blob Storage and sinks including Cosmos DB (NoSQL). See the GitHub repository: https://github.com/Azure/azure-documentdb-datamigrationtool. Sample migration settings for a JSON source to a Cosmos DB sink:
Example terminal steps to prepare and run the tool:
Sample logs from a successful import (example output):
A sample document ingested into the container might look like:
After import, validate and query documents from Data Explorer using SQL queries.

Visual Studio: adding, reading, and querying items from .NET

Create a Console application and add the Microsoft.Azure.Cosmos NuGet package. Use the account URI and primary key from the Azure portal Keys blade to configure CosmosClient. Do not hard-code secrets in production—use managed identities and Azure Key Vault.
A screenshot of the Microsoft Azure portal showing the Keys page for an Azure Cosmos DB account named "az204cosmosdb01," with the account URI, primary/secondary keys, and connection strings displayed (keys masked). The portal's left navigation and account settings are visible.
Do not store keys or secrets in source code for production. Use managed identities with Key Vault or other secure secret storage.
Example: inserting an airport record using an anonymous object that matches your JSON schema:
Validate the inserted record in Data Explorer with a SQL query such as:

Querying by country_code (example)

If your documents use a country_code property, filter by that property in your SQL query. The sample below shows querying airports by country_code and printing each airport name.
Sample console output (truncated):

This lesson covered the essential CRUD and query patterns for using the Microsoft .NET SDK with Azure Cosmos DB. From here you can expand into partition key design, throughput tuning (RU/s), bulk imports, change feed processing, and server-side programming (stored procedures, triggers, and UDFs). Further reading and resources:

Watch Video