Forecasts and sessions over the API
How to upload sales, train and wait for the result, and read a session's forecasts, accuracy and results.
On this page
A session is one forecast run: some sales, a configuration and the result of training it. The app works with the active session, the one GET /planning returns in active_session_id. Sessions are never deleted: DELETE /sessions/{id} archives them and POST /sessions/{id}/restore brings them back.
The complete nightly cycle
This is the work most integrations need: replace the sales file, retrain and wait.
#!/usr/bin/env bash
set -euo pipefail
API=https://app.stockai.es/api/v1
AUTH="Authorization: Bearer $STOCKAI_KEY"
# 1. Replace the sales file in place (same source id, same column mapping)
curl -sf -X POST "$API/data-sources/$SOURCE_ID/file" -H "$AUTH" -F "file=@/exports/sales.csv"
# 2. Which session is active right now (ask every time, never hard-code it)
SESSION=$(curl -sf "$API/planning" -H "$AUTH" | jq -r '.data.active_session_id')
# 3. Retrain (skip this if the session has a schedule: it retrains when the file changes)
curl -sf -X POST "$API/sessions/$SESSION/train" -H "$AUTH" \
-H "Content-Type: application/json" -d '{}'
# 4. Wait while it is QUEUED or RUNNING. Watching only RUNNING exits on the first pass.
while :; do
ST=$(curl -sf "$API/sessions/$SESSION/train/status" -H "$AUTH" | jq -r '.data.status')
case "$ST" in QUEUED|RUNNING) sleep 10 ;; *) break ;; esac
done
echo "Training finished: $ST"- Look up your sales source's
idonce withGET /data-sources(orGET /datasets) and keep it in your configuration: that id does not change. - Every night replace the file with
POST /data-sources/{id}/file(multipart, fieldfile). The source keeps its id and its column mapping. It accepts.csv,.xlsx,.xls,.parquetand.json. - Ask for the active session with
GET /planning. - If the session has a schedule, StockAI retrains on its own when the file changes and you can skip this step. Otherwise queue training with
POST /sessions/{id}/train. - Poll
GET /sessions/{id}/train/statusevery few seconds while it saysQUEUEDorRUNNING. It ends inCOMPLETEDorFAILED.
{
"session_id": "sess_cc9ab9137953",
"status": "RUNNING",
"job_id": "job_fe6d1a76b4f2",
"job": {
"status": "RUNNING",
"created_by": "usr_97c71ad04a86",
"progress": { "step": "ml_training", "message": "Training ML models", "percent": 27 },
"error": null
}
}Data sources and datasets
| Method and route | What it does | Key |
|---|---|---|
GET /data-sources | Lists your sources (files and SQL databases). | read |
POST /data-sources/file | Creates a new source from a file. | write |
POST /data-sources/{id}/file | Replaces a source's file, keeping its id and mapping. The previous file is not deleted: it is kept aside. | write |
GET /data-sources/{id}/preview, /analyze | Preview and analysis of the source. | read |
POST /data-sources/sql, .../execute-query, .../materialize | SQL sources: create, query and save the result as a dataset. | write |
DELETE /data-sources/{id} | Deletes a source. Refused with data_source_in_use if any session (even archived) uses it. | write |
GET /datasets, POST /datasets, GET /datasets/{id} | Training datasets. | read / write |
Sessions and configuration
| Method and route | What it does |
|---|---|
GET /sessions, GET /sessions/summary, GET /sessions/{id} | List and read sessions. |
POST /sessions | Creates a draft session (name required; description, tags). |
POST /sessions/{id}/dataset, POST /sessions/{id}/upload | Attaches an existing dataset or uploads a new one. |
/sessions/{id}/configure/columns, /features, /models, /validation | The training wizard's steps (GET to read, POST to save). |
GET /sessions/{id}/data-gate, POST .../configure/remediations | The data-quality gate and your answers to it. |
POST /sessions/{id}/train | Queues training (optional: user_horizon_days, user_granularity). |
POST /sessions/{id}/reforecast | Creates a NEW session that forecasts with the stored models over newer sales, without retraining. |
POST /sessions/{id}/backtest | Starts a backtest. Backtest sessions never drive planning. |
GET /jobs/active, GET /jobs/{id}, GET /jobs/{id}/logs, DELETE /jobs/{id} | Queued jobs: see them, read their log and cancel them. |
Retraining on a schedule
curl -X POST https://app.stockai.es/api/v1/sessions/$SESSION/schedule \
-H "Authorization: Bearer $STOCKAI_KEY" -H "Content-Type: application/json" \
-d '{"cron_expr":"0 6 * * 1","enabled":true}'cron_expr is a cron expression (in the example: Mondays at 6:00). GET /schedules lists them all, GET /schedules/history their runs, and DELETE /sessions/{id}/schedule removes the schedule. It is the same as the "Schedules" tab under Automation.
Reading the forecast
Route (GET) | What it returns |
|---|---|
/sessions/{id}/forecast-series/{sku} | A SKU's history and forecast with its quantiles (p10, p50, p90…). Optional: model. |
/sessions/{id}/forecast-total | The whole catalogue summed as one series (granularity). |
/sessions/{id}/results | Forecasts and per-model results (level). |
/sessions/{id}/accuracy | Accuracy against the baseline models, per SKU and overall (threshold). |
/sessions/{id}/metrics, /routing, /warnings | Metrics, which model was chosen for each SKU, and the run's warnings. |
/sessions/{id}/sku-intelligence/{sku}, /decomposition/{sku} | A SKU's card and its decomposition (trend, seasonality). |
/sessions/{id}/forecast-vs-actual | What was forecast against what sold. |
/sessions/{id}/reports/{format} | Downloads a report (first POST .../reports/generate and GET .../reports/status). |
curl "https://app.stockai.es/api/v1/sessions/$SESSION/forecast-series/SKU-001" \
-H "Authorization: Bearer $STOCKAI_KEY"
# "data": {
# "sku": "SKU-001", "model": "ets", "available_models": ["ets"],
# "historical": [{ "date": "2024-01-01", "value": 25.0 }, ...],
# "forecast": [{ "date": "2025-04-01", "value": 40.67,
# "p10": 35.53, "p50": 40.67, "p90": 45.81,
# "lower": 34.07, "upper": 47.26 }, ...]
# }Datasets for BI
For Power BI, Excel or Looker there are flat tables meant to be read whole: /datasets/forecast-points, /datasets/inventory-status, /datasets/accuracy, /datasets/purchase-order-lines, /datasets/receptions and /datasets/committed-demand; /datasets/schema describes their columns. They accept format, page, limit and session_id. Since many BI tools cannot set Authorization, use X-API-Key.
curl "https://app.stockai.es/api/v1/datasets/forecast-points?format=csv&page=1" \
-H "X-API-Key: $STOCKAI_KEY" -o forecast-points.csv