Cloudsmith API Client
The `cloudsmith-api` library is an auto-generated Python client for interacting with the Cloudsmith package management API (version 1). It provides programmatic access to manage repositories, packages, users, and more. As of May 2024, the latest version is 2.0.25, with frequent releases reflecting updates to the underlying OpenAPI specification.
Common errors
-
cloudsmith_api.rest.ApiException: (401) Reason: Unauthorized
cause The provided API key is either missing, invalid, or has insufficient permissions for the requested operation.fixVerify that the `CLOUDSMITH_API_KEY` environment variable (or wherever you get your key) is correctly set and contains a valid Cloudsmith API key. Ensure the key has the necessary permissions for the API calls being made. -
cloudsmith_api.rest.ApiException: (404) Reason: Not Found
cause The resource (e.g., repository, package) specified in the API call does not exist, or the owner/repository slugs are incorrect.fixCheck the `owner`, `repo` (and any other identifier slugs) parameters in your API call. Ensure they are correctly spelled and correspond to existing resources in your Cloudsmith account. Remember slugs are case-sensitive. -
AttributeError: module 'cloudsmith_api' has no attribute '...' (e.g., 'RepositoriesApi')
cause Attempting to access an API client class or function directly from the top-level `cloudsmith_api` module instead of through an instantiated API client.fixAfter importing `cloudsmith_api`, you must first create a `cloudsmith_api.ApiClient` instance (potentially with a `cloudsmith_api.Configuration`). Then, create an API-specific client like `repositories_api = cloudsmith_api.RepositoriesApi(api_client)` before calling methods on it.
Warnings
- breaking Major version `2.x.x` of the `cloudsmith-api` client library (released August 2023) introduced potential breaking changes compared to `1.x.x` versions. This was due to an upgrade of the underlying OpenAPI Generator, which can alter method signatures, object structures, and error handling patterns.
- gotcha API key authentication requires the key to be passed within a dictionary, mapping 'X-Api-Key' to your actual key. Incorrectly formatted authentication can lead to `401 Unauthorized` errors.
- gotcha Many API endpoints require both an `owner` slug (your organization or user slug) and a `repo` slug (the repository identifier). Misunderstanding these parameters or providing incorrect slugs often results in `404 Not Found` errors.
Install
-
pip install cloudsmith-api
Imports
- cloudsmith_api
import cloudsmith_api
- ApiException
from cloudsmith_api import ApiException
from cloudsmith_api.rest import ApiException
- Configuration
import cloudsmith_api
- RepositoriesApi
import cloudsmith_api
Quickstart
import os
import cloudsmith_api
from cloudsmith_api.rest import ApiException
# Configure API key authorization:
X-Api-Key = os.environ.get('CLOUDSMITH_API_KEY', '')
if not X_Api_Key:
print("Error: CLOUDSMITH_API_KEY environment variable not set.")
exit(1)
configuration = cloudsmith_api.Configuration(api_key={'X-Api-Key': X_Api_Key})
# Enter a context with an instance of the API client
with cloudsmith_api.ApiClient(configuration) as api_client:
# Create an instance of the API class
repositories_api = cloudsmith_api.RepositoriesApi(api_client)
try:
# List repositories
print("Listing Cloudsmith repositories...")
api_response = repositories_api.repos_list()
for repo in api_response.data:
print(f" - {repo.name} (Slug: {repo.slug}) by {repo.owner}")
except ApiException as e:
print(f"Exception when calling RepositoriesApi->repos_list: {e}")
except Exception as e:
print(f"An unexpected error occurred: {e}")