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

# Add patients to cohort

> Adds one or more patients to a cohort.

```python theme={null}
olira.add_patients_to_cohort(
    cohort_id: str,
    patient_ids: list[str],
) -> CohortPatientMutationResult
```

**Requires scope:** `api:manage-patients`

Idempotent — patients already in the cohort are skipped. Max 500 per call. If the cohort has template assignments, snapshot documents are seeded for newly added patients in the background.

## Parameters

<ParamField body="cohort_id" type="str" required>
  Olira-assigned cohort id.
</ParamField>

<ParamField body="patient_ids" type="list[str]" required>
  Olira patient ids to add (max 500).
</ParamField>

## Returns

`CohortPatientMutationResult`

<ResponseField name="cohort_id" type="str">
  Cohort id.
</ResponseField>

<ResponseField name="patient_count" type="int">
  Total enrolled patients after the operation.
</ResponseField>

## Raises

| Exception         | When                                                                                                                  |
| ----------------- | --------------------------------------------------------------------------------------------------------------------- |
| `AuthError`       | HTTP 401 or 403 — invalid API key or insufficient OAuth scope for this endpoint.                                      |
| `ValidationError` | HTTP 400, 404, or 422 — malformed JSON, unknown patient, or validation failure (message includes response excerpt).   |
| `ServerError`     | HTTP 409 (e.g. conflicting external identifier) or repeated 5xx after retries; includes status\_code when applicable. |
| `RateLimitError`  | HTTP 429 — rate limited; check retry\_after (seconds).                                                                |
| `NetworkError`    | Connection timeout, DNS failure, or read error after retries.                                                         |

<RequestExample>
  ```python Python theme={null}
  import olira

  olira.init(api_key="YOUR_API_KEY")
  result = olira.add_patients_to_cohort(
      cohort_id="68582e4a0a0b3c1234567890",
      patient_ids=["8a4fde23-0f1b-4c2a-9d7e-b36c1a5f0e82"],
  )
  print(result.patient_count)
  ```

  ```http HTTP theme={null}
  POST /v1/cohorts/68582e4a0a0b3c1234567890/patients
  Authorization: Bearer YOUR_API_KEY
  Content-Type: application/json

  {
    "patient_ids": ["8a4fde23-0f1b-4c2a-9d7e-b36c1a5f0e82"]
  }
  ```
</RequestExample>

<ResponseExample>
  ```python 200 theme={null}
  CohortPatientMutationResult(cohort_id="68582e4a0a0b3c1234567890", patient_count=1)
  ```

  ```json 401 theme={null}
  {
    "error": true,
    "status_code": 401,
    "error_type": "authentication_error",
    "message": "Could not validate credentials",
    "details": [
      {
        "type": "authentication_error",
        "message": "Could not validate credentials"
      }
    ],
    "timestamp": "2026-05-06T12:00:00+00:00"
  }
  ```

  ```json 403 theme={null}
  {
    "error": true,
    "status_code": 403,
    "error_type": "authorization_error",
    "message": "Insufficient OAuth scope for this endpoint",
    "details": [
      {
        "type": "authorization_error",
        "message": "Insufficient OAuth scope for this endpoint"
      }
    ],
    "timestamp": "2026-05-06T12:00:00+00:00"
  }
  ```

  ```json 404 theme={null}
  {
    "error": true,
    "status_code": 404,
    "error_type": "not_found_error",
    "message": "Patient not found",
    "details": [
      {
        "type": "not_found_error",
        "message": "Patient not found"
      }
    ],
    "timestamp": "2026-05-06T12:00:00+00:00"
  }
  ```

  ```json 422 theme={null}
  {
    "error": true,
    "status_code": 422,
    "error_type": "validation_error",
    "message": "Request validation failed (1 error)",
    "details": [
      {
        "type": "missing",
        "message": "Field required",
        "field": "patient_id",
        "location": ["body", "patient_id"],
        "input_value": null
      }
    ],
    "timestamp": "2026-05-06T12:00:00+00:00"
  }
  ```

  ```json 429 theme={null}
  {
    "error": true,
    "status_code": 429,
    "error_type": "server_error",
    "message": "Rate limit exceeded",
    "details": [
      {
        "type": "rate_limit",
        "message": "Too many requests; retry after backoff"
      }
    ],
    "timestamp": "2026-05-06T12:00:00+00:00"
  }
  ```

  ```json 500 theme={null}
  {
    "error": true,
    "status_code": 500,
    "error_type": "internal_server_error",
    "message": "An internal server error occurred",
    "details": [
      {
        "type": "internal_server_error",
        "message": "An unexpected error occurred while processing your request"
      }
    ],
    "timestamp": "2026-05-06T12:00:00+00:00"
  }
  ```
</ResponseExample>
