# Use the Python SDK

Install from PyPI in a virtual environment with `python -m pip install nestor-cloud`.
[CLI setup](/cli) includes release verification and the minimum version for Compute.
The import is `nestor`; verify it with `python -c "from nestor import Nestor"`.

## Management client

Set `NESTOR_API_URL=https://api.nestor.software`, `NESTOR_ORGANIZATION` and
`NESTOR_PROJECT` to your public scope IDs. Provide `NESTOR_API_KEY` through a hidden
prompt or your application's secret manager, never a command argument or source.
Use the full one-time management secret; its public key ID or prefix cannot
authenticate requests.

```python
from nestor import Nestor

client = Nestor()
endpoint = client.inference.endpoints.create(
    model="Qwen/Qwen2.5-0.5B-Instruct", name="chat", wait=True
)
print(endpoint.id, endpoint.state)
```

Managed Endpoints use Auto selection unless you explicitly request supported
hardware. Wait for **READY**. The returned document keeps its public fields under
`.raw`, including immutable revision intent and resolved facts.

## Invoke with a separate key

A human creates an invocation key bound to the READY Endpoint in Console Access.
Load it privately as `NESTOR_INFERENCE_KEY`; the management Service account cannot
issue it. Use your Endpoint's returned ID:

```python
import os
from nestor import Nestor

client = Nestor()
endpoint = client.inference.endpoints.get(os.environ["NESTOR_ENDPOINT_ID"])
reply = client.inference.endpoints.chat(
    endpoint, "Give one tip for keeping API keys private.",
    key=os.environ["NESTOR_INFERENCE_KEY"],
)
print(reply.text)
```

The supplied walkthrough demonstrates both plain and streamed chat. Complete
stream consumption matters: an interrupted stream is not a successful full reply.
Custom HTTP and Comfy Endpoints instead submit asynchronous invocations with durable
Operations and result Data; they do not accept the chat interface.

## Data and finite work

```python
item = client.data.upload("./input", name="input", permitted_uses=["training", "serving"])
print(item.state, item.reference)
client.data.download(item, "./download")
```

Verify downloaded files against their published sizes and digests. Reuse the
returned canonical reference and permitted use in [Training or Run](/training/runs).
Keep accepted resource IDs, Operations and stable mutation keys across failures.
Never blindly repeat an upload or workload after an uncertain response.

Read committed Run checkpoints with the SDK:

```python
import os

run = client.runs.get(os.environ["NESTOR_RUN_ID"])
for checkpoint in run.checkpoints():  # client.runs.checkpoints(run) is equivalent
    print(checkpoint.id, checkpoint.path, checkpoint.size, checkpoint.digest)
    print(checkpoint.data_reference or checkpoint.reference)
    run.download(checkpoint, f"./checkpoints/{checkpoint.id}.pt")
```

Iteration follows opaque page tokens; downloads verify size and SHA-256
before replacing local files. Only committed versions are listed, including
while the run is active. Older wheels use `client.training.runs.checkpoints(run)`
and `client.training.runs.download(run, checkpoint, destination)`.
Reading checkpoints does not resume a program; [continuation](/guides/fine-tune)
needs an explicit immutable input and a command that loads it.

Delete an Endpoint when finished and wait for cleanup; revoke its invocation key
through the human Console. Deleting an Endpoint does not delete your reusable Data.
Data deletion remains logical before hold and retention rules permit physical purge.
