Skip to content

Repository files navigation

Codesphere

Python SDK

PyPIPythonLicense

The official Python client for the Codesphere API.


Installation

uv add codesphere

Configuration

Every option can be passed directly to the constructor:

fromcodesphereimportCodesphereSDKsdk=CodesphereSDK(token="your-api-token")
# or, e.g. against a different deployment:sdk=CodesphereSDK(token="your-api-token", base_url="https://my-instance.example.com/api")

Alternatively, configure via environment variables (or a .env file in your project root). Explicit constructor arguments always win over the environment:

VariableDescriptionDefault
CS_TOKENAPI token (required)
CS_BASE_URLAPI base URLhttps://codesphere.com/api

If no token is provided at all, CodesphereSDK() raises an AuthenticationError at construction (importing the package never requires any environment variable).

Retries

Transient failures are retried automatically (2 retries by default). Requests using an idempotent method (GET, HEAD, PUT, DELETE) are retried on 429, 502, 503, and 504 responses as well as on connect/timeout errors. The delay honors the server's Retry-After header (seconds or HTTP-date) when present, otherwise exponential backoff with jitter (backoff_factor * 2**attempt).

Tune or disable the behavior with a RetryConfig:

fromcodesphereimportCodesphereSDK, RetryConfigsdk=CodesphereSDK(
token="your-api-token",
retry=RetryConfig(max_retries=0), # disable retries entirely
)

POST is never retried unless you add it to retry_methods explicitly (only do this if your endpoints tolerate duplicate submissions):

RetryConfig(max_retries=3, retry_methods=frozenset({"GET", "POST"}))

Feature flags

Platform instances enable different features. Inspect them via sdk.flags:

snapshot=awaitsdk.flags.get() # fetched once, then cachedifawaitsdk.flags.is_enabled("workspace-ssh"):
...
awaitsdk.flags.refresh() # re-fetch after platform changes

SDK features that depend on a platform flag raise FeatureNotEnabledError (or FeatureNotAvailableError if the instance doesn't have the feature at all) before sending any request when the flag is off.

Quick Start

importasynciofromcodesphereimportCodesphereSDKasyncdefmain():
asyncwithCodesphereSDK() assdk:
teams=awaitsdk.teams.list()
forteaminteams:
print(f"{team.name} (ID: {team.id})")
asyncio.run(main())

Synchronous client

The same API is available without async/await — ideal for scripts and tools that don't run an event loop:

fromcodesphere.syncimportCodesphereSDKwithCodesphereSDK() assdk:
teams=sdk.teams.list()
forteaminteams:
print(f"{team.name} (ID: {team.id})")

Both flavors share the same request/response models, configuration, and error types; only the entities with API methods (Workspace, Team, …) exist per flavor.

Usage

Teams

teams=awaitsdk.teams.list()
team=awaitsdk.teams.get(team_id=123)
awaitteam.delete()

Workspaces

workspaces=awaitsdk.workspaces.list(team_id=123)
workspace=awaitsdk.workspaces.get(workspace_id=456)
result=awaitworkspace.execute_command("ls -la")
print(result.output)
awaitworkspace.env_vars.set([{"name": "API_KEY", "value": "secret"}])
env_vars=awaitworkspace.env_vars.get()

Domains

team=awaitsdk.teams.get(team_id=123)
domains=awaitteam.domains.list()
domain=awaitteam.domains.create(name="api.example.com")

Metadata

datacenters=awaitsdk.metadata.list_datacenters()
plans=awaitsdk.metadata.list_plans()
images=awaitsdk.metadata.list_images()

Development

git clone https://github.com/Datata1/codesphere-python.git
cd codesphere-python
uv sync --all-extras
uv run pytest

License

MIT – see LICENSE for details.

About

A Codesphere Public API wrapper

Topics

Resources

Code of conduct

Contributing

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages