Azure Cosmos DB SDK for Rust
Client library for Azure Cosmos DB NoSQL API — globally distributed, multi-model database.
Installation
cargo add azure_data_cosmos azure_identity
Environment Variables
COSMOS_ENDPOINT=https://<account>.documents.azure.com:443/
COSMOS_DATABASE=mydb
COSMOS_CONTAINER=mycontainer
Authentication
use azure_identity::DeveloperToolsCredential;
use azure_data_cosmos::CosmosClient;
let credential = DeveloperToolsCredential::new(None)?;
let client = CosmosClient::new(
"https://<account>.documents.azure.com:443/",
credential.clone(),
None,
)?;
Client Hierarchy
| Client | Purpose | Get From |
CosmosClient | Account-level operations | Direct instantiation |
DatabaseClient | Database operations | client.database_client() |
ContainerClient | Container/item operations | database.container_client() |
Core Workflow
Get Database and Container Clients
let database = client.database_client("myDatabase");
let container = database.container_client("myContainer");
Create Item
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize)]
struct Item {
pub id: String,
pub partition_key: String,
pub value: String,
}
let item = Item {
id: "1".into(),
partition_key: "partition1".into(),
value: "hello".into(),
};
container.create_item("partition1", item, None).await?;
Read Item
let response = container.read_item("partition1", "1", None).await?;
let item: Item = response.into_model()?;
Replace Item
let mut item: Item = container.read_item("partition1", "1", None).await?.into_model()?;
item.value = "updated".into();
container.replace_item("partition1", "1", item, None).await?;
Patch Item
use azure_data_cosmos::models::PatchDocument;
let patch = PatchDocument::default()
.with_add("/newField", "newValue")?
.with_remove("/oldField")?;
container.patch_item("partition1", "1", patch, None).await?;
Delete Item
container.delete_item("partition1", "1", None).await?;
Key Auth (Optional)
Enable key-based authentication with feature flag:
cargo add azure_data_cosmos --features key_auth
Best Practices
- Always specify partition key — required for point reads and writes
- Use
into_model()?— to deserialize responses into your types - Derive
SerializeandDeserialize— for all document types - Use Entra ID auth — prefer
DeveloperToolsCredentialover key auth - Reuse client instances — clients are thread-safe and reusable
Reference Links
| Resource | Link |
| API Reference | https://docs.rs/azure_data_cosmos |
| Source Code | https://github.com/Azure/azure-sdk-for-rust/tree/main/sdk/cosmos/azure_data_cosmos |
| crates.io | https://crates.io/crates/azure_data_cosmos |
Skill Information
- Source
- Microsoft
- Category
- Cloud & Azure
- Repository
- View on GitHub
Related Skills
agent-framework-azure-ai-py
Build Azure AI Foundry agents using the Microsoft Agent Framework Python SDK (agent-framework-azure-ai). Use when creating persistent agents with AzureAIAgentsProvider, using hosted tools (code interpreter, file search, web search), integrating MCP servers, managing conversation threads, or implementing streaming responses. Covers function tools, structured outputs, and multi-tool agents.
Microsoftazd-deployment
Deploy containerized applications to Azure Container Apps using Azure Developer CLI (azd). Use when setting up azd projects, writing azure.yaml configuration, creating Bicep infrastructure for Container Apps, configuring remote builds with ACR, implementing idempotent deployments, managing environment variables across local/.azure/Bicep, or troubleshooting azd up failures. Triggers on requests for azd configuration, Container Apps deployment, multi-service deployments, and infrastructure-as-code with Bicep.
Microsoftazure-ai-agents-persistent-dotnet
Azure AI Agents Persistent SDK for .NET. Low-level SDK for creating and managing AI agents with threads, messages, runs, and tools. Use for agent CRUD, conversation threads, streaming responses, function calling, file search, and code interpreter. Triggers: "PersistentAgentsClient", "persistent agents", "agent threads", "agent runs", "streaming agents", "function calling agents .NET".
Microsoftazure-ai-agents-persistent-java
Azure AI Agents Persistent SDK for Java. Low-level SDK for creating and managing AI agents with threads, messages, runs, and tools. Triggers: "PersistentAgentsClient", "persistent agents java", "agent threads java", "agent runs java", "streaming agents java".
Microsoftazure-ai-anomalydetector-java
Build anomaly detection applications with Azure AI Anomaly Detector SDK for Java. Use when implementing univariate/multivariate anomaly detection, time-series analysis, or AI-powered monitoring.
Microsoft