# Machine-Accessible Court Data - Documentation for AI Assistants

## Overview

This API provides machine-accessible access to public case information and decision documents from the Arizona Court of Appeals Division Two. All data is public record.

## Base URL

```
https://{HOSTNAME}/api
```

A trailing slash is accepted on all paths (`/api/cases/` is the same as `/api/cases`).

## Endpoints at a Glance

| Endpoint | Purpose | Response |
|---|---|---|
| `GET /api/case?case_id={case_id}...` | Look up a single case | JSON |
| `GET /api/cases` | Search cases (paginated) | JSON |
| `GET /api/cases?format=ndjson` | Bulk export of all matching cases | NDJSON stream |
| `GET /api/decisions/recent` | Recent cases/decisions | JSON |
| `GET /api/case/{case_id}/documents` | Documents for a case | JSON |
| `GET /api/decisions` | 100 most recent active cases | JSON |

All endpoints accept `GET` only.

## Case Object

Cases are returned in the same shape by every endpoint that lists or looks up cases.

```json
{
  "case_id": 12345,
  "case_number": "2 CA-CR 2025-0002",
  "case_title": "JOHNSON v. STATE",
  "county": "Maricopa",
  "filing_date": "2025-01-15T00:00:00+00:00",
  "decision_type": "Opinion",
  "published_date": "2025-02-01T00:00:00+00:00",
  "decision_document": "https://{HOSTNAME}/serveFile.php?type=DE&file=CR20250002r.pdf",
  "decision2_type": null,
  "decision2_document": null,
  "decision2_published_date": null,
  "current_status": "Active",
  "sealed": true,
  "issues_summary": "Whether the trial court erred in ...",
  "is_constitutional": false,
  "trial_court_case_numbers": ["CR2024-001234"]
}
```

**Field notes:**
- `case_id`: unique case identifier
- Dates are ISO 8601 (`date('c')` format). Any date or document field may be `null`.
- `decision_document` / `decision2_document`: absolute URLs to PDFs. `decision2_*` fields are populated only when a case has a second decision (for example, a bifurcated decision).
- `issues_summary`: free text; may be long or `null`.
- `is_constitutional`: Boolean flag indicating a constitutional issue.
- `trial_court_case_numbers`: array of trial court case numbers, or `null` if none are on file.

---

## 1. Get Case by CaseID

Retrieve detailed information for a single case.

**URL:** `GET /api/case?case_id={case_id}`

**Parameters:**
- `case_id` (required): case_id (e.g., "12345"). Leading zeros are stripped, and characters other than digits are removed.

**Example:**
```
curl "https://{HOSTNAME}/api/case?case_id=12354"
```

**Response:** a single [Case Object](#case-object) (not wrapped in an envelope).

**Errors:** `400` if `case_id` is missing or empty, `404` if no case matches.

---

## 2. Search Cases

Search for cases using filters. Results are **paginated JSON** by default. To retrieve every matching case, use the [bulk export mode](#bulk-export-ndjson) below.

**URL:** `GET /api/cases`

**Filter parameters (all optional, combine with AND):**
- `party`: partial match against the case title
- `from_date`: earliest filing date (YYYY-MM-DD)
- `to_date`: latest filing date (YYYY-MM-DD)
- `county`: exact county name
- `case_type`: case type code (CR, CV, PT, etc.)
- `status`: exact status (Active, Closed, Mandate)

**Pagination parameters:**
- `page` (default `1`): page number, starting at 1
- `per_page` (default `50`, maximum `100`): results per page. Values above 100 are reduced to 100.

**Example:**
```
curl "https://{HOSTNAME}/api/cases?county=Maricopa&case_type=CR&from_date=2025-01-01&page=1&per_page=50"
```

**Response:**
```json
{
  "cases": [
    {
      "case_id": 12345,
      "case_number": "2 CA-CR 2025-0002",
      "case_number_raw": "CA0000020250002",
      "case_title": "JOHNSON v. STATE",
      "county": "Maricopa",
      "filing_date": "2025-01-15T00:00:00+00:00",
      "current_status": "Active"
    }
  ],
  "pagination": {
    "page": 1,
    "per_page": 50,
    "total": 1,
    "total_pages": 1,
    "has_more": false
  },
  "export": "Add format=ndjson to stream all matching cases."
}
```

(Each entry in `cases` is a full [Case Object](#case-object); fields are abbreviated here.)

**Pagination notes:**
- `total` is the number of cases matching the filters across all pages, not just the current page.
- Keep requesting `page + 1` while `has_more` is `true`.
- Results are ordered by case type, then case year (newest first), then case number (highest first).
- For retrieving large result sets, prefer bulk export. Very deep pages are slower than early ones.

### Bulk Export (NDJSON)

Add `format=ndjson` to stream **all** cases matching the filters in a single response. Pagination parameters are ignored in this mode.

**URL:** `GET /api/cases?format=ndjson`

**Example:**
```
curl "https://{HOSTNAME}/api/cases?format=ndjson" > all_cases.ndjson
curl "https://{HOSTNAME}/api/cases?format=ndjson&county=Pima&status=Active"
```

**Response:** `Content-Type: application/x-ndjson`. The body is one complete [Case Object](#case-object) per line, with no enclosing array or envelope:

```
{"case_number":"2 CA-CR 2025-0002","case_title":"JOHNSON v. STATE", ...}
{"case_number":"2 CA-CV 2025-0010","case_title":"SMITH v. JONES", ...}
```

**Usage notes:**
- Parse the response line by line; each line is independently valid JSON. Do not try to parse the whole body as a single JSON document.
- Results are ordered by internal case ID, not by the ordering used in paginated mode.
- The stream may be large. Process it incrementally rather than loading it all into memory.
- The `pagination` and `total` fields are not included in NDJSON mode.
- If an error occurs after streaming has started, the HTTP status cannot change; the stream will simply end early. Validate that you received the number of records you expect (for example, by comparing against `pagination.total` from a paginated request with the same filters).
- Please do not run bulk exports repeatedly. Download once, then use `/api/decisions/recent` to stay current.

---

## 3. Get Recent Decisions

Retrieve recently filed cases that are still in progress. Cases with status `Mandate` or `Closed` are excluded.

**URL:** `GET /api/decisions/recent`

**Parameters:**
- `since` (optional): YYYY-MM-DD. Returns cases with a filing date on or after this date. Defaults to 30 days ago. An invalid format returns `400`.

**Example:**
```
curl "https://{HOSTNAME}/api/decisions/recent?since=2025-10-01"
```

**Response:**
```json
{
  "decisions": [
    {
      "case_number": "2 CA-CR 2025-0002",
      "case_title": "JOHNSON v. STATE",
      "decision_type": "Opinion",
      "published_date": "2025-02-01T00:00:00+00:00",
      "decision_document": "https://{HOSTNAME}/serveFile.php?type=DE&file=CR20250002r.pdf"
    }
  ],
  "total": 1,
  "since": "2025-10-01"
}
```

(Each entry in `decisions` is a full [Case Object](#case-object); fields are abbreviated here.)

**Notes:**
- Returns at most 100 results, newest filing date first.
- The `since` date is compared against the case **filing date**, not the decision publication date. A case may appear here before it has a published decision; check that `decision_document` is non-null.

---

## 4. Get Case Documents

Retrieve documents associated with a case.

**URL:** `GET /api/case/{case_id}/documents`

**Parameters:**
- `case_id` (required): Internal numeric database case ID. This is not the case number. It is not currently returned by the other endpoints, so this endpoint is mainly useful when you already know the ID.

**Example:**
```
curl "https://{HOSTNAME}/api/case/90001/documents"
```

**Response:**
```json
{
  "case_id": 90001,
  "case_number": "2 CA-CR 2025-0002",
  "documents": [
    {
      "type": "decision",
      "filename": "CR20250002r.pdf",
      "url": "https://{HOSTNAME}/serveFile.php?type=DE&file=CR20250002r.pdf",
      "published_date": "2025-02-01T00:00:00+00:00",
      "decision_type": "Opinion"
    }
  ],
  "total": 1
}
```

**Document types:**
- `decision`: a published decision (includes `published_date` and `decision_type`). A second decision, if any, is listed as another `decision` entry.
- `upload`: a file uploaded by an attorney or firm (includes `uploaded_by`, with no dates). Uploads are listed only for cases that have a published decision.

**Errors:** `404` if the case ID does not exist.

---

## 5. List Recent Active Decisions

Get the 100 most recently filed cases that are still in progress (status other than `Mandate` or `Closed`). This is not a complete list of all decisions. For that, use [bulk export](#bulk-export-ndjson).

**URL:** `GET /api/decisions`

**Parameters:** none

**Response:**
```json
{
  "decisions": [
    {
      "case_number": "2 CA-CR 2025-0002",
      "case_title": "JOHNSON v. STATE",
      "decision_type": "Opinion",
      "published_date": "2025-02-01T00:00:00+00:00",
      "decision_document": "https://{HOSTNAME}/serveFile.php?type=DE&file=CR20250002r.pdf"
    }
  ],
  "total": 1
}
```

(Each entry in `decisions` is a full [Case Object](#case-object); fields are abbreviated here.)

---

## Error Responses

All errors are JSON with an `error` message and, usually, a `details` object.

### 400 Bad Request
```json
{
  "error": "Missing required parameter",
  "details": {
    "parameter": "case_number"
  }
}
```

```json
{
  "error": "Invalid date",
  "details": {
    "parameter": "since",
    "expected": "YYYY-MM-DD"
  }
}
```

### 404 Not Found
```json
{
  "error": "Case not found",
  "details": {
    "case_number": "INVALID123"
  }
}
```

Unknown API paths also return `404`, with `details.path` and `details.message`.

### 405 Method Not Allowed
```json
{
  "error": "Method not allowed",
  "details": {
    "allowed": "GET"
  }
}
```

### 429 Too Many Requests

Returned by the web server when the rate limit is exceeded (see below).

---

## Security

- All data is public record
- No authentication required
- Rate limited: 10 requests per minute (burst: 20)
- Cached responses: 1 hour for successful responses
- Path traversal protection on file serving
- Security headers on all responses
- User-Agent logged for abuse detection

### Rate Limiting

- **Standard limit**: 10 requests per minute per IP
- **Burst allowance**: 20 requests
- **Exceeded limit**: HTTP 429 Too Many Requests
- **Header**: `X-RateLimit-Limit: 100`
- A bulk export (`format=ndjson`) counts as a single request, no matter how many records it returns.

### Caching

- API responses cached for 1 hour (successful responses)
- 404 errors cached for 1 minute
- File serving (`/serveFile.php`) has no caching
- Static assets cached for 1 year
- Because of caching, data may be up to an hour stale.

### User-Agent Policy

- All requests logged with User-Agent header
- AI assistants should identify themselves clearly
- See [user-agent-policy.md](user-agent-policy.md) for details

---

## Guidance for AI Assistants

- To find a specific case, use `/api/case`. To search by party, county, date, type, or status, use `/api/cases`.
- To enumerate everything, use `/api/cases?format=ndjson` once, rather than paging through hundreds of pages.
- To check for new activity, use `/api/decisions/recent?since=YYYY-MM-DD`.
- Case documents are PDFs at the URLs given in `decision_document`, `decision2_document`, and the documents endpoint. Use the URLs exactly as returned.
- Treat `null` values as "not available", not as errors.

---

## Related Files

- OpenAPI Specification: `openapi.json`
- Sitemap: `sitemap.xml`
- LLM Guide: `llms.txt`
- User-Agent Policy: `user-agent-policy.md`
- Nginx Configuration: `nginx.conf`

---
