Azure Cosmos DB SDKs
Access your Azure Cosmos DB data programmatically using our comprehensive suite of SDKs. These SDKs provide a rich and intuitive API for interacting with Azure Cosmos DB from your applications.
Azure Cosmos DB offers native SDKs for popular programming languages, allowing you to build robust applications with ease.
C# / .NET
// Install the NuGet package:
// dotnet add package Microsoft.Azure.Cosmos
using System;
using System.Threading.Tasks;
using Microsoft.Azure.Cosmos;
public class CosmosDbExample
{
// IMPORTANT: Replace with your actual endpoint and key
private static readonly string EndpointUri = "YOUR_COSMOS_DB_ENDPOINT";
private static readonly string PrimaryKey = "YOUR_COSMOS_DB_PRIMARY_KEY";
private static readonly string DatabaseId = "YOUR_DATABASE_ID";
private static readonly string ContainerId = "YOUR_CONTAINER_ID";
private CosmosClient client;
private Container container;
public async Task InitializeAsync()
{
client = new CosmosClient(EndpointUri, PrimaryKey);
Database database = await client.CreateDatabaseIfNotExistsAsync(DatabaseId);
container = await database.CreateContainerIfNotExistsAsync(ContainerId, "/partitionKey");
}
public async Task CreateItemAsync(object item)
{
try
{
ItemResponse<object> createdItem = await container.CreateItemAsync(item, new PartitionKey("yourPartitionKeyValue"));
Console.WriteLine($"Created item: {createdItem.Resource.Id}");
}
catch (CosmosException ex)
{
Console.WriteLine($"Error creating item: {ex.StatusCode}");
}
}
public async Task<object> GetItemAsync(string id, string partitionKey)
{
try
{
ItemResponse<object> itemResponse = await container.ReadItemAsync<object>(id, new PartitionKey(partitionKey));
Console.WriteLine($"Read item: {itemResponse.Resource.Id}");
return itemResponse.Resource;
}
catch (CosmosException ex)
{
Console.WriteLine($"Error reading item: {ex.StatusCode}");
return null;
}
}
public static async Task Main(string[] args)
{
CosmosDbExample example = new CosmosDbExample();
await example.InitializeAsync();
var newItem = new { id = Guid.NewGuid().ToString(), category = "widget", name = "Super Widget", quantity = 10, partitionKey = "widget" };
await example.CreateItemAsync(newItem);
var retrievedItem = await example.GetItemAsync(newItem.id, "widget");
if (retrievedItem != null)
{
Console.WriteLine("Successfully retrieved item.");
}
}
}
Important: Always manage your connection strings and keys securely. Avoid hardcoding them directly in client-side code. Consider using Azure Key Vault or environment variables. For browser-based JavaScript, it is highly recommended to use a backend proxy to handle authentication.
Key Features of Cosmos DB SDKs
- High Performance: Optimized for low latency and high throughput.
- Consistency Options: Support for various consistency levels (Strong, Bounded Staleness, Session, Consistent Prefix, Eventual).
- Querying: Powerful SQL query capabilities.
- Change Feed: Real-time data stream processing.
- Multi-Region Writes: Global distribution and low-latency writes.
- Cross-Platform: Available for major operating systems and platforms.
Choosing the Right SDK
Select the SDK that best matches your application's programming language and environment. Each SDK provides a consistent interface for interacting with Azure Cosmos DB, abstracting away the complexities of HTTP requests and responses.
For direct interaction with the database without an SDK, you can also use the Azure Cosmos DB REST API.