# Python SDK

Use the same ledger operations in your application, locally or through a named connection.

Canonical page: /docs/sdk

## Write and read

This example starts a local ledger. A new ledger’s permissive contract accepts this payload; inspect existing ledger rules before writing.

```python
from op_sys import OpSys

client = OpSys(connection="local")
client.start()
client.ledgers.inspect()
result = client.records.append(
    operation_id="first-record", payload={"message": "Hello"}
)
if result["state"] == "accepted":
    record = client.records.get(record_id=result["acceptance"]["record_id"])
    print(record["record"]["payload"])
```


## Target a hosted ledger

Use an authenticated named connection and an authorized ledger ID. The methods and domain results stay the same. Direct hosted use needs no local replica.

Constructors do not make a network request. Existing SDK clients retain their initialized selection until explicitly reconfigured.

```python
client = OpSys(connection="work", ledger="<ledger-id>")
ledger = client.ledgers.inspect()
page = client.records.list(limit=20)
```

- [Configure a connection](/docs/sharing#connect)

## Handle outcomes and retries

Accepted, pending and uncertain outcomes have different meanings. Save an operation ID before dispatch and preserve both it and the intended input for retries. records.outcome checks acceptance without submitting again.

OpSysError.core_error contains the domain code, operation, retry classification and recovery guidance. Follow pagination continuations and coverage; a first page is not the full ledger.

- [SDK contract and error handling](/docs/raw/manuals/sdk.md)
- [Contribution recovery](/docs/raw/manuals/ledger-contributions.md)

## Related guides

- [Your first record](/docs/raw/quickstart.md)
- [Connections & sharing](/docs/raw/sharing.md)
- [API conventions](/docs/raw/api-conventions.md)
