> ## Documentation Index
> Fetch the complete documentation index at: https://docs.strandai.com/llms.txt
> Use this file to discover all available pages before exploring further.

# SDK quickstart

> Create a Strand API key and predict spatially resolved protein-marker maps from H&E with Python or R.

Lattice predicts spatially resolved protein-marker maps from routine H\&E,
delivered as virtual mIF channels. This guide covers the programmatic workflow:
upload, side-effect-free pricing, job submission, waiting, and result download.
For the standard browser workflow,
start with the [Platform quickstart](https://docs.strandai.com/quickstart.md).

> [!WARNING]
>
> **For research use only.** Strand AI predictions are model outputs intended
> for research and hypothesis generation. They are not validated for clinical
> use. Do not use them for diagnosis, treatment selection, or patient care
> decisions.

## 1. Create an API key

Sign in to [app.strandai.com](https://app.strandai.com), select the organization
your integration should use, and open [Settings → API
keys](https://app.strandai.com/settings/api-keys). Choose “Create key” and copy
the `sk-strand-…` value when it appears. The full value is shown only once.

```bash theme={"dark"}
export STRAND_API_KEY=sk-strand-XXXXXXXXXXXXXXXXXXXXXXXX
```

> [!TIP]
>
> Treat the API key like a password. It is scoped to the selected organization
> and uses that organization's sample access and credit balance.

## 2. Install a client

```bash Python theme={"dark"}
pip install "strand-sdk[anndata]"
```

```r R theme={"dark"}
install.packages(
  "strandai",
  repos = c("https://strand-ai.r-universe.dev", "https://cloud.r-project.org")
)
BiocManager::install("SpatialExperiment")
```

## 3. Upload and price the run

**Slides must be de-identified before upload.** Automated slide de-identification
is off by default and runs only when enabled for the organization. It does not
determine whether a slide satisfies HIPAA or any other legal de-identification
standard, and it does not inspect or remove identifiers in tissue pixels or
filenames. See [Security and data handling](https://docs.strandai.com/security-data-handling.md).

```python Python theme={"dark"}
import time

from strand import Client, StrandError

client = Client()  # reads STRAND_API_KEY
upload = client.uploads.upload_file("biopsy.svs", mpp=0.26)
markers = ["CD4", "PanCK", "Ki67"]

# Automatic ingest preprocessing has started. Wait for sample metadata.
deadline = time.monotonic() + 600
poll_seconds = 2.0
while True:
    sample = client.samples.get(upload.id)
    if sample.ownership != "mine":
        raise RuntimeError(f"uploaded sample resolved as {sample.ownership!r}")
    if sample.status == "ready":
        if not sample.width_px or not sample.height_px or not sample.mpp:
            raise RuntimeError("sample is ready but lacks prediction dimensions or MPP")
        break
    if sample.status.endswith("failed"):
        raise RuntimeError(f"ingest failed: {sample.status}")
    if sample.status not in {"uploading", "preprocessing"}:
        raise RuntimeError(f"unexpected ingest status: {sample.status}")
    if time.monotonic() >= deadline:
        raise TimeoutError(f"sample metadata was not ready: {sample.status}")
    time.sleep(poll_seconds)
    poll_seconds = min(poll_seconds * 1.5, 15.0)

# Tissue-tile counting can finish later. A successful dry run is final readiness.
deadline = time.monotonic() + 600
poll_seconds = 2.0
while True:
    try:
        estimate = client.predict.submit(upload.id, markers, dry_run=True)
        break
    except StrandError as err:
        if (err.status_code, err.error_code) != (409, "sample_not_ready"):
            raise
        if time.monotonic() >= deadline:
            raise TimeoutError("sample was not priceable after 10 minutes") from err
        time.sleep(poll_seconds)
        poll_seconds = min(poll_seconds * 1.5, 15.0)

print(estimate.estimated_credits, estimate.org_balance)
```

```r R theme={"dark"}
library(strandai)

client <- strand_client()  # reads STRAND_API_KEY
upload <- strand_upload_file(client, "biopsy.svs", progress = TRUE, mpp = 0.26)
markers <- c("CD4", "PanCK", "Ki67")

# Automatic ingest has started. Wait for sample metadata.
deadline <- Sys.time() + 10 * 60
poll_seconds <- 2
repeat {
  sample <- strand_samples_get(client, upload$id)
  if (!identical(sample$ownership, "mine")) {
    stop("uploaded sample resolved as ", sample$ownership)
  }
  if (identical(sample$status, "ready")) {
    geometry <- c(sample$width_px, sample$height_px, sample$mpp)
    if (length(geometry) != 3L || any(!is.finite(geometry)) || any(geometry <= 0)) {
      stop("sample is ready but lacks prediction dimensions or MPP")
    }
    break
  }
  if (grepl("failed$", sample$status)) stop("ingest failed: ", sample$status)
  if (!sample$status %in% c("uploading", "preprocessing")) {
    stop("unexpected ingest status: ", sample$status)
  }
  if (Sys.time() >= deadline) stop("sample metadata was not ready: ", sample$status)
  Sys.sleep(poll_seconds)
  poll_seconds <- min(poll_seconds * 1.5, 15)
}

# Tissue-tile counting can finish later. A successful dry run is final readiness.
deadline <- Sys.time() + 10 * 60
poll_seconds <- 2
repeat {
  estimate <- tryCatch(
    strand_predict(client, upload$id, markers, dry_run = TRUE),
    strand_api_error = function(e) e
  )
  if (!inherits(estimate, "strand_api_error")) break
  if (!identical(estimate$status_code, 409L) ||
      !identical(estimate$error_code, "sample_not_ready")) {
    stop(estimate)
  }
  if (Sys.time() >= deadline) stop("sample was not priceable after 10 minutes")
  Sys.sleep(poll_seconds)
  poll_seconds <- min(poll_seconds * 1.5, 15)
}

message(estimate$estimated_credits, " credits; balance ", estimate$org_balance)
```

`strand_upload_file()` returns after the storage event starts ingest. Sample
metadata and tissue-tile pricing become ready asynchronously, so both bounded
checks are required before an immediate prediction. Omit `mpp` when the slide's
embedded calibration is authoritative. The dry run reports the exact price
without creating a job or reserving credits.

## 4. Submit and wait

```python Python theme={"dark"}
job = client.predict.submit(upload.id, markers)
status = job.wait(timeout=1800)
print(status.status)
```

```r R theme={"dark"}
job <- strand_predict(client, upload$id, markers)
status <- strand_job_wait(job, timeout = 1800, progress = TRUE)
message(status$status)
```

Submission reserves credits atomically. The job continues server-side if the
client disconnects.

## 5. Download results

```python Python theme={"dark"}
adata = job.download_results()
job.download_export("ome-tiff", "result.ome.tiff", timeout=1800)
```

```r R theme={"dark"}
spe <- strand_download_results(job)
strand_export_download(job, "ome-tiff", "result.ome.tiff")
```

The in-memory Python result is an `AnnData`. The R result is a
`SpatialExperiment`. Both contain the requested marker channels aligned to the
slide coordinate system.

## Next steps

> [card] [Python SDK](https://docs.strandai.com/sdks/python.md)
>
> Typed samples, jobs, events, exports, and selective result reads.

> [card] [R SDK](https://docs.strandai.com/sdks/r.md)
>
> `strand_*` functions and Bioconductor result handling.

> [card] [Command line](https://docs.strandai.com/sdks/cli.md)
>
> Run the workflow from a shell without writing a script.

> [card] [REST API](https://docs.strandai.com/api/overview.md)
>
> Use the canonical HTTP contract directly.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.