Smart Site
Home Docs Smart Data API Interface

API Interface

Smart SCADA exposes a REST API for integration with external systems, mobile clients, and automation tooling. All endpoints are JSON over HTTP, secured by JWT, and documented with Swagger.

1. API Basics

  • Base URL — http://localhost:4000/api/v1
  • Format — application/json
  • Auth — Authorization: Bearer <JWT> header
  • CORS — configured in apps/ss-data-server/src/main.ts (or ss-node/src/main.ts)
  • Rate limiting — @nestjs/throttler guards protect auth and write endpoints

A successful response is wrapped by the TransformInterceptor:

{
  "code": 200,
  "message": "success",
  "data": { ... }
}

Errors follow the same envelope with a non-200 code and a localized message.

2. JWT Authentication

  1. POST /api/v1/auth/login with { username, password } returns { accessToken, refreshToken, user }
  2. Send accessToken as Authorization: Bearer ... on every protected request
  3. When it expires, call POST /api/v1/auth/refresh with the refresh token
  4. POST /api/v1/auth/logout invalidates the session
  5. Optional MFA: POST /api/v1/auth/mfa/verify after login if MFA is enabled

For development and testing, a debug-mock-token header bypasses JWT verification in non-production mode — never enable this in production.

3. API Documentation (Swagger)

Interactive docs are generated by @nestjs/swagger and served at:

http://localhost:4000/api/docs

The Swagger UI lists every controller, its routes, request/response DTOs, and lets you try calls inline. DTOs are decorated with class-validator decorators so the schema reflects validation rules.

4. Common Endpoint List

4.1 Authentication

Method Path Description
POST /auth/login Login, returns JWT
POST /auth/refresh Refresh access token
POST /auth/logout Invalidate session
GET /auth/profile Current user profile
POST /auth/change-password Change password

4.2 Devices & Tags

Method Path Description
GET /projects/:projectId/devices List devices
POST /projects/:projectId/devices Create device
GET /projects/:projectId/devices/:id Device detail
POST /projects/:projectId/devices/:deviceId/tags Add tag
POST /projects/:projectId/devices/:deviceId/tags/batch Batch add tags
GET /projects/:projectId/devices/:id/data/latest Latest cached values
GET /projects/:projectId/devices/:id/data/history Historical series
POST /projects/:projectId/devices/:id/data/write Write a tag value

4.3 Alarms

Method Path Description
GET /alarms List alarms
POST /alarms/:id/ack Acknowledge an alarm
GET /alarms/stats Alarm statistics

4.4 Scripts

Method Path Description
GET /scripts List scripts
POST /scripts Create script
POST /scripts/:id/run Execute a script manually
GET /scripts/:id/executions Execution history

4.5 Reports

Method Path Description
GET /reports List report definitions
POST /reports/:id/preview Preview a report
POST /reports/:id/export Export a report

5. Using the API

5.1 cURL Example

# Login
TOKEN=$(curl -s -X POST http://localhost:4000/api/v1/auth/login \
  -H "Content-Type: application/json" \
  -d '{"username":"admin","password":"admin"}' | jq -r .data.accessToken)

# List devices
curl -s http://localhost:4000/api/v1/projects/{projectId}/devices \
  -H "Authorization: Bearer $TOKEN"

5.2 Error Handling

HTTP Code Meaning
200 Success
400 Validation error (check message)
401 Missing or invalid token
403 Insufficient permission
404 Resource not found
409 Conflict (e.g. delete system role)
429 Rate limit exceeded
500 Server error

6. FAQ

Q: The API returns 401 on every request. A: The JWT has expired or is missing. Re-login to obtain a fresh token, or implement the refresh-token flow. Check that the Authorization header uses the Bearer scheme exactly.

Q: Swagger docs are empty. A: The Swagger setup scans controllers at startup. If a module is dynamically loaded later, its routes may not appear. Restart the backend. Also confirm @nestjs/swagger is in dependencies and SwaggerModule.setup is called in main.ts.

Q: How do I get a long-lived token for an integration? A: Use the refresh-token flow. Access tokens are short-lived by design; refresh tokens are longer-lived and can be rotated. Avoid storing admin credentials in scripts.

Q: Are webhooks available for external systems? A: Yes — configure a Webhook notification channel (see Alarm Management) for alarm events. For data streaming, subscribe over WebSocket instead of polling REST.

  • ../../01-permission/README.md
  • ../../09-websocket/README.md
  • API Architecture Document
  • ../../../../../services/ss-node/src/main.ts