# Wire Formats & Streaming

SimHealth is engineered for high-throughput synthetic data generation. The API supports three primary wire formats via HTTP content negotiation, allowing clients to balance memory overhead and ingestion convenience.

> [!NOTE]
> All formats support the exact same seed determinism. The chosen wire format impacts network transport and parsing throughput, not the generated data values.

## Format comparison

| Format | Content-Type Header | Record Limit | Typical Use Case |
| --- | --- | --- | --- |
| **Avro OCF** | `application/avro-binary` | 10,000,000+ | Large ETL pipelines, Snowflake/BigQuery bulk loading, DuckDB |
| **NDJSON** | `application/x-ndjson` | 1,000,000 | Line-by-line stream ingestion, Kafka producers, CLI pipes |
| **JSON** | `application/json` | 10,000 | Interactive testing, UI previews, Postman queries |

## Streaming Avro Object Container Files (OCF)

Avro OCF is the recommended format for production workloads generating over 10,000 records. Avro bundles a compact binary payload with an embedded JSON schema in the file header:

```bash
# Stream 1,000,000 records directly into an Avro container file
simhealth sample person --count 1000000 --seed 9921 --format avro > persons.avro
```

### HTTP request with Avro negotiation

```tabs
=== cURL
```bash
curl -s -H "Authorization: Bearer $SIMHEALTH_API_KEY" \
  -H "Accept: application/avro-binary" \
  "https://api.preprod.simhealth.interoperabilitypro.com/v1/projects/{project_id}/random/person?count=50000&seed=1842" \
  -o export.avro
```
=== Python
```python
import fastavro
import requests

url = "https://api.simdata.interoperabilitypro.com/v1/projects/default/random/person?count=50000&seed=1842"
headers = {
    "Authorization": "Bearer sim_live_abcdef123456",
    "Accept": "application/avro-binary",
}

with requests.get(url, headers=headers, stream=True) as response:
    reader = fastavro.reader(response.raw)
    for record in reader:
        pass  # Process streaming record
```
=== TypeScript
```typescript
import avro from "avsc";

const response = await fetch(
  "https://api.simdata.interoperabilitypro.com/v1/projects/default/random/person?count=50000&seed=1842",
  {
    headers: {
      Authorization: "Bearer sim_live_abcdef123456",
      Accept: "application/avro-binary",
    },
  }
);
const arrayBuffer = await response.arrayBuffer();
// Parse binary buffer with avsc decoder
```
```

## Newline-Delimited JSON (NDJSON)

NDJSON streams each generated object as an individual JSON string terminated by a newline character `\n`. It requires no binary decoding tools and streams cleanly without buffering the entire dataset in memory:

```bash
curl -s -H "Authorization: Bearer $SIMHEALTH_API_KEY" \
  -H "Accept: application/x-ndjson" \
  "https://api.preprod.simhealth.interoperabilitypro.com/v1/projects/{project_id}/random/person?count=1000&seed=1842" \
  | jq -r '.first_name + " " + .last_name'
```

## Buffered JSON

Standard JSON is buffered in memory by the server and returned as a single JSON envelope with metadata:

```json
{
  "recordType": "person",
  "count": 2,
  "seed": 1842,
  "records": [
    { "person_id": "P-001", "name": "Alice" },
    { "person_id": "P-002", "name": "Bob" }
  ]
}
```

> [!WARNING]
> Standard buffered JSON is hard-capped at 10,000 records per request to prevent browser memory exhaustion. For larger datasets, request `application/avro-binary` or `application/x-ndjson`.
