Skip to main content

Serving & APIs

6.1 Serving & APIs​

REST API​

The SDK provides a production-grade REST API built on FastAPI with automatic Swagger UI documentation. The API enables programmatic model submission, run management, parameter sweeps, and live streaming --- making it possible to embed ABM simulations in larger enterprise workflows without requiring Python client code.

Starting the server:

abm-lab serve --model examples/models/gai_kapadia/model.py --port 8080
# Swagger UI: http://localhost:8080/docs

The API exposes 11 endpoints:

MethodPathDescription
GET/api/v1/healthHealth check: status, version, models discovered
GET/api/v1/modelsList all available models with metadata and parameter descriptions
POST/api/v1/models/{name}/runStart a simulation run with config overrides, step count, and seed
GET/api/v1/models/{name}/statusPoll run status (pending, running, completed, failed) with progress
GET/api/v1/models/{name}/resultsRetrieve completed run results: accumulators, agent counts, config
POST/api/v1/models/{name}/stopStop a running simulation gracefully
POST/api/v1/sweepExecute a parameter sweep (grid, random, or Sobol strategy)
GET/api/v1/models/{name}/sourceRetrieve model source code
GET/api/v1/models/{name}/configRetrieve parsed config.json
GET/api/v1/models/{name}/architectureModel architecture: agent types, link types, accumulators, mechanisms
WS/api/v1/models/{name}/streamWebSocket: live step-by-step data streaming

The run endpoint accepts a JSON body:

{
"config_overrides": {"num_banks": 200, "cash_buffer": 0.08},
"num_steps": 100,
"seed": 42
}

And returns a run ID that can be used to poll status and retrieve results. The sweep endpoint accepts parameter ranges and a strategy, fans out into multiple runs, and returns a sweep ID with all associated run IDs.

WebSocket Streaming​

The SimulationStreamer provides real-time step-by-step data to connected WebSocket clients. It supports five message types:

TypeContentUse Case
stepTick number, accumulator values, eventsLive dashboard time series
agentAgent ID, state snapshot, tickIndividual agent monitoring
eventEvent type, details, tickAlerts (defaults, violations)
space2dAgent positions, field layer data, markersContinuous space visualisation
completeFinal tick, summary statisticsRun completion notification

The streamer is thread-safe (broadcast() can be called from any thread) and supports background mode (start_background() / stop_background()) for integration with the simulation runtime. The MockStreamer captures all messages in a list for testing.

from simudyne.engine.streaming import SimulationStreamer

streamer = SimulationStreamer(host="localhost", port=8765)
streamer.start_background()
model.set_streamer(streamer)
# Run simulation... clients receive live updates
streamer.stop_background()

Web Dashboard​

The Dashboard class provides a browser-based monitoring interface built on FastAPI with Chart.js for live visualisation. It connects to the REST API and WebSocket streaming endpoints to provide a unified view of model execution.

from simudyne.engine.dashboard import Dashboard

dashboard = Dashboard(api_url="http://localhost:8000", port=8050)
dashboard.run()
# Dashboard available at http://localhost:8050

Dashboard pages:

  • Home: Lists all discovered models with descriptions and parameter metadata
  • Run: Auto-generated parameter forms from Input descriptors, start/stop controls, live progress charts via WebSocket
  • Results: Browse completed runs, view accumulator time series, download results as JSON
  • Monitor: System health metrics (models discovered, active runs, total runs, error rates)