# API overview

> Base URL, authentication and the conventions every endpoint follows.

The REST API serves both models. The Python library and the command line are built on it, and any language can call it
directly.

```
https://api.yhatlabs.com
```

## Authentication

Send your key in the `Authorization` header. This reads the API's request limits, a quick way to check the key works:

```bash
curl -s https://api.yhatlabs.com/v1/limits -H "Authorization: Bearer $YHAT_API_KEY" | python3 -m json.tool
```

Output:

```
{
"forecast": {
    "max_series": 256,
    "max_points_per_series": 32768,
    "max_total_points": 8000000,
    "max_horizon": 2048,
    "max_columns": 256,
    "native_quantile_levels": [
        0.1,
        0.2,
        0.3,
        0.4,
        0.5,
        0.6,
        0.7,
        0.8,
        0.9
    ]
},
"tabular": {
    "max_rows": 200000,
    "max_bytes": 67108864,
    "presets": [
        "fast",
        "medium",
        "full"
    ],
    "max_quantiles": 32,
    "fit_ttl_hours": {
        "default": 24,
        "max": 168
    },
    "max_upload_bytes": 1073741824,
    "max_table_storage_bytes": 10737418240,
    "max_tables": 200
},
"pricing": {
    "forecast": {
        "unit": "series",
        "counts": "one per target column of each series"
    },
    "tabular": {
        "unit": "credits",
        "fit_cells_per_credit": 1000000,
        "preset_multiplier": {
            "fast": 1,
            "medium": 3,
            "full": 8
        },
        "predict_rows_per_credit": 10000,
        "importance_rows_per_credit": 10000,
        "min_credits": 1
    }
},
"rate": {
    "requests_per_minute": 120,
    "concurrent": {
        "forecast": 8,
        "tabular": 2,
        "upload": 4
    }
},
"capabilities": {
    "idempotency_keys": true,
    "gzip_requests": true,
    "forecast_quantile_levels": true,
    "forecast_samples": true,
    "forecast_history": true,
    "tabular_distribution": true,
    "tabular_prediction_sets": true,
    "fit_groups": true,
    "table_uploads": true
}
}
```

## Endpoints

| Method | Path | What it does |
|---|---|---|
| `POST` | [/v1/series/forecast](https://yhatlabs.com/models/docs/api/forecast) | forecasts a batch of series |
| `POST` | [/v1/table/predict](https://yhatlabs.com/models/docs/api/tabular-predict) | fits on a table and predicts rows, in one call |
| `POST` | [/v1/table/fits](https://yhatlabs.com/models/docs/api/fits) | fits a table once and keeps the fit |
| `POST` | [/v1/table/fits/\{id\}/predict](https://yhatlabs.com/models/docs/api/fits#predict-from-a-fit) | predicts rows from a stored fit |
| `POST` | [/v1/table/fits/\{id\}/importance](https://yhatlabs.com/models/docs/api/fits#importance) | column importance of a stored fit |
| `GET` | [/v1/table/fits](https://yhatlabs.com/models/docs/api/fits#list-get-and-delete), `/v1/table/fits/{id}` | lists fits, reads one |
| `DELETE` | `/v1/table/fits/{id}` | deletes a fit |
| `GET` | [/v1/models](https://yhatlabs.com/models/docs/api/account#models), [/v1/usage](https://yhatlabs.com/models/docs/api/account#usage), [/v1/limits](https://yhatlabs.com/models/docs/api/account#limits) | the key's models, its quota, and the request caps |
| `POST` | [/v1/uploads](https://yhatlabs.com/models/docs/api/uploads) | uploads a large table once, to use by reference |
| `GET` | [/v1/openapi.json](https://api.yhatlabs.com/v1/openapi.json) | the OpenAPI 3.1 description of every endpoint and field, for client generators and tools |

## Conventions

- Requests and responses are JSON (`Content-Type: application/json`).
- Every response has an `X-Request-Id` header. Quote it when you contact us.
- Errors have one shape: `{"error": {"type": ..., "message": ..., "request_id": ...}}`; see [Errors](https://yhatlabs.com/models/docs/api/errors).
- **Retries without duplicates:** `POST /v1/table/fits` and `POST /v1/table/predict` accept an `Idempotency-Key` header (8 to 128
  letters, digits, `-` or `_`). Send the same key when you retry: a call that already succeeded returns its first reply,
  marked `Idempotent-Replayed: true`, with no second fit and no second charge; a retry that arrives while the first call still
  runs gets `409 request_in_progress` with `Retry-After`. Failed calls are not kept, so their retry runs again. Keys are kept
  for 24 hours, per API key.

```bash
BODY='{"train": [{"x": 1, "y": 0}, {"x": 2, "y": 0}, {"x": 3, "y": 1}, {"x": 4, "y": 1}, {"x": 5, "y": 1}, {"x": 6, "y": 0}],
   "target": "y", "test": [{"x": 2.5}]}'
KEY="retry-$(date +%s)"
for attempt in 1 2; do   # the second call is what a retry after a dropped connection sends
  curl -s -D - -o /dev/null https://api.yhatlabs.com/v1/table/predict \
-H "Authorization: Bearer $YHAT_API_KEY" -H "Idempotency-Key: $KEY" -H "Content-Type: application/json" -d "$BODY" \
| grep -iE "^HTTP|^idempotent-replayed" | tr -d '\r'
done
```

Output:

```
HTTP/2 200 
HTTP/2 409
```

- **Capabilities:** `GET /v1/limits` lists, under `capabilities`, the features this API has switched on (`idempotency_keys`,
  `gzip_requests`, ...). A missing flag means off.
- **Long calls:** a call that runs longer than 30 seconds (a large tabular fit, for example) switches to a `200` response that
  sends a space every 20 seconds and then the JSON body. Leading spaces are valid JSON, so ordinary clients parse it unchanged,
  and the connection never idles long enough for a proxy to drop it. An error after the switch arrives in the body as
  `{"error": ...}` with status `200`, so check for an `error` key.
