Skip to main content

Azure Functions Extensions Bindings Cosmos DB library for Python

This library allows Cosmos DB Input bindings in Python Function Apps to recognize and bind to client types from the Azure Cosmos DB SDK.

Cosmos DB client types can be generated from:

  • Cosmos DB Input

Source code Package (PyPi) | Samples

Getting started

Prerequisites

Install the package

Install the Azure Functions Extensions Bindings Cosmos DB library for Python with pip:

pip install azurefunctions-extensions-bindings-cosmosdb

Create a storage account

If you wish to create a new storage account, you can use the Azure Portal, Azure PowerShell, or Azure CLI:

# Create a new resource group to hold the storage account -
# if using an existing resource group, skip this step
az group create --name my-resource-group --location westus2

# Create the storage account
az storage account create -n my-storage-account-name -g my-resource-group

Bind to the SDK-type

The Azure Functions Extensions Bindings Cosmos DB library for Python allows you to create a function app with Cosmos DB Input and define the type as a CosmosClient, DatabaseProxy, or ContainerProxy. Instead of receiving a DocumentList, when the function is executed, the type returned will be the defined SDK-type and have all of the properties and methods available as seen in the Azure Storage Cosmos DB library for Python.

import logging
import azure.functions as func
import azurefunctions.extensions.bindings.cosmosdb as cosmos

app = func.FunctionApp(http_auth_level=func.AuthLevel.FUNCTION)

@app.route(route="cosmos")
@app.cosmos_db_input(arg_name="container",
                     connection="CosmosDBConnection",
                     database_name="db_name",
                     container_name="container_name")
def get_docs(req: func.HttpRequest, client: cosmos.CosmosClient):
    databases = client.list_databases()
    for db in databases:
        logging.info(f"Found database with ID: {db.get('id')}")

    return "ok"

Troubleshooting

General

The SDK-types raise exceptions defined in Azure Core.

This list can be used for reference to catch thrown exceptions. To get the specific error code of the exception, use the error_code attribute, i.e, exception.error_code.

Next steps

More sample code

Get started with our Cosmos DB samples.

Several samples are available in this GitHub repository. These samples provide example code for additional scenarios commonly encountered while working with Cosmos DB:

Additional documentation

For more information on the Azure Cosmos DB SDK, see the Azure Cosmos DB documentation on learn.microsoft.com and the Azure Cosmos DB README.

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com.

When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

Release files for azurefunctions-extensions-bindings-cosmosdb 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for azurefunctions-extensions-bindings-cosmosdb 1.0.0
File Size Uploaded
azurefunctions_extensions_bindings_cosmosdb-1.0.0.tar.gz 12.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for azurefunctions-extensions-bindings-cosmosdb 1.0.0
File Interpreter ABI Platform
azurefunctions_extensions_bindings_cosmosdb-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 24.6 kB

Release files / azurefunctions_extensions_bindings_cosmosdb-1.0.0.tar.gz

Download URL azurefunctions_extensions_bindings_cosmosdb-1.0.0.tar.gz
Size 12.6 kB
Tags Source
SHA-256 checksum
How to use checksums
c475cf139594d9979bc201c21cf54fcfd401ec339500931cdbb9cd73e08a975d
BLAKE2b-256 checksum
How to use checksums
f7f1986eb2dd864dc7ac8653d83559cd3aa6ded0618e0359fad87f54dfdbc1da
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via RestSharp/106.13.0.0

Release files / azurefunctions_extensions_bindings_cosmosdb-1.0.0-py3-none-any.whl

Download URL azurefunctions_extensions_bindings_cosmosdb-1.0.0-py3-none-any.whl
Size 12.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
372d17f18c611b405819c05d0afdff6a427fc6557b04f0531ab77b5d29d084ae
BLAKE2b-256 checksum
How to use checksums
66158824d7262f5ed4ee28e95f3050a4a3a889b2670017ae9badd96d97bbe125
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via RestSharp/106.13.0.0

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page