IBM Cloudant Python SDK
The IBM Cloudant Python SDK is an actively maintained client library for interacting with IBM Cloudant APIs. Currently at version 0.11.5, it offers a unified IBM Cloud SDK experience, handles various authentication types, and provides a thread-safe client. While considered production-ready, it is still in a 0.x release series, meaning API changes may occur before its 1.0 release. It receives frequent patch releases.
Warnings
- breaking The behavior of the `AUTH_DISABLE_SSL` environment variable was fixed in v0.11.0, specifically impacting the `AUTH_TYPE=COUCHDB_SESSION` authenticator configuration. If you relied on the previous, incorrect behavior with this specific combination, your application might break.
- gotcha The SDK is still in a 0.x release series (e.g., 0.11.5). While considered production-ready, IBM explicitly states that APIs may be subject to change before the 1.0 release. It is highly recommended to pin your dependency version to avoid unexpected breaking changes.
- deprecated The previous Python client library, `python-cloudant` (module name `cloudant`), is End-of-Life (EOL) and no longer supported. Users should migrate to `ibmcloudant`.
- gotcha There is a known issue regarding `application/json` attachments documented in v0.11.4 release notes. If working with attachments, consult the official documentation for details and potential workarounds.
- gotcha Authentication requires proper configuration of a service URL and Cloudant service credentials (e.g., API key, username/password). Incorrect or missing credentials are a common source of connection issues.
Install
-
pip install ibmcloudant
Imports
- CloudantV1
from ibmcloudant.cloudant_v1 import CloudantV1
- IAMAuthenticator
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
- Document
from ibmcloudant.cloudant_v1 import Document
Quickstart
import os
from ibmcloudant.cloudant_v1 import CloudantV1
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
# Retrieve credentials from environment variables (recommended)
service_url = os.environ.get('CLOUDANT_URL', 'YOUR_CLOUDANT_URL')
api_key = os.environ.get('CLOUDANT_APIKEY', 'YOUR_IAM_API_KEY')
# Ensure credentials are provided
if not service_url or not api_key:
print("Error: CLOUDANT_URL and CLOUDANT_APIKEY environment variables must be set.")
exit(1)
# Authenticate and create client
authenticator = IAMAuthenticator(api_key)
service = CloudantV1(authenticator=authenticator)
service.set_service_url(service_url)
# Example: List all databases
try:
all_dbs = service.get_all_dbs().get_result()
print("Databases:")
for db in all_dbs:
print(f"- {db}")
except Exception as e:
print(f"An error occurred: {e}")