# API Reference

The FastBee backend provides RESTful APIs, and all requests must carry a JWT token.

## Page Guide

- **Use Cases**: Third-party systems or custom frontends integrate with FastBee through RESTful APIs to manage resources such as products, devices, thing models, and scenes.
- **Prerequisites**: The backend service is running (API port 8080) and you have a system account that can log in.
- **Operation Path**: Get a captcha → Call /login to get a token → Carry Authorization: Bearer {token} in the request header → Call business endpoints.
- **Acceptance Criteria**: Authenticated calls return normal business data, and /swagger-ui.html opens the full Swagger API documentation.

## Scope and Boundaries

* The RESTful APIs target management-side integration and custom development, covering management operations on products, devices, thing models, scene linkage, and other resources.
* Device data reporting and command delivery do not go through HTTP APIs; they use the MQTT protocol and are handled by the backend's built-in MQTT Broker.
* The endpoint definitions in the backend source code `fastbee-open-api` module are authoritative; see [Backend Development](https://www.fastbee.cn/doc/en/open/v2/dev/backend.md) for the module structure.

## Authentication

### Get a Captcha

Login requires a graphical captcha by default (controlled by the system parameter `sys.account.captchaEnabled`, on by default). Call the captcha endpoint first:

```
GET /captchaImage
```

Response:

```json
{
  "code": 200,
  "msg": "操作成功",
  "captchaEnabled": true,
  "uuid": "c9a1b2c3...",
  "img": "/9j/4AAQSkZJRg..."
}
```

* `img` is the base64-encoded captcha image; the default captcha type is arithmetic, so `code` is the result of the expression shown in the image
* `uuid` links the captcha to the login attempt and must be passed unchanged in the login request
* If `captchaEnabled` is `false` (captcha disabled in the system parameters), the login request can omit `code` and `uuid`

### Get a Token

```
POST /login
Content-Type: application/json

{
  "username": "admin",
  "password": "admin123",
  "code": "8",
  "uuid": "the uuid returned by captchaImage above"
}
```

Response:

```json
{
  "code": 200,
  "msg": "操作成功",
  "token": "eyJhbGciOiJIUzI1NiJ9..."
}
```

### Request Header

All subsequent requests must carry:

```
Authorization: Bearer {token}
```

## Common Endpoints

### Product Management

| Method | Path | Description |
|--------|------|-------------|
| GET | /iot/product/list | Product list |
| POST | /iot/product | Create a product |
| PUT | /iot/product | Edit a product |
| DELETE | /iot/product/{productId} | Delete a product |

### Device Management

| Method | Path | Description |
|--------|------|-------------|
| GET | /iot/device/list | Device list |
| POST | /iot/device | Create a device |
| PUT | /iot/device | Edit a device |
| DELETE | /iot/device/{deviceId} | Delete a device |
| GET | /iot/device/runningStatus?deviceId={deviceId} | Device running status |

### Thing Model

| Method | Path | Description |
|--------|------|-------------|
| GET | /iot/thingsModel/list | Thing model list |
| POST | /iot/thingsModel | Create a thing model |

### Scene Linkage

| Method | Path | Description |
|--------|------|-------------|
| GET | /iot/scene/list | Scene list |
| POST | /iot/scene | Create a scene |
| PUT | /iot/scene | Edit a scene |
| DELETE | /iot/scene/{sceneIds} | Delete scenes |
| PUT | /iot/scene/updateStatus | Enable/disable a scene |

## Online Documentation and Call Verification

The full API documentation is available via Swagger: after starting the backend, visit `http://localhost:8080/swagger-ui.html`.

Call verification example:

```bash
# 1. Get a captcha, note the returned uuid, and solve the arithmetic expression in the image
curl http://localhost:8080/captchaImage

# 2. Log in to get a token (code is the captcha result, uuid comes from the previous step)
curl -X POST http://localhost:8080/login \
  -H "Content-Type: application/json" \
  -d '{"username":"admin","password":"admin123","code":"8","uuid":"uuid from the previous step"}'

# 3. Call a business endpoint with the token
curl http://localhost:8080/iot/product/list \
  -H "Authorization: Bearer {token}"
```

If step 3 returns the product list data, authentication and endpoint calls are working.

## FAQ

### The endpoint returns 401?

The token is missing or expired, or it is not placed in the `Authorization` header in the `Bearer {token}` format; call `/login` again to get a new token and retry.

### Login returns a captcha error?

Login requires a captcha by default: call `/captchaImage` first to get the `uuid` and captcha image, fill `code` with the result of the arithmetic expression in the image (the default type), and pass the `uuid` unchanged in the login request. Make sure `code` and `uuid` come from the same captcha request and have not expired.

### How do I see the complete endpoint list?

Visit the Swagger documentation at `http://localhost:8080/swagger-ui.html` after starting the backend; the corresponding controller list is also in the "API Controller Layer" section of [Backend Development](https://www.fastbee.cn/doc/en/open/v2/dev/backend.md).

### Which endpoint returns device real-time data?

Use `GET /iot/device/runningStatus?deviceId={deviceId}`; the device must be connected to the platform and have reported data, otherwise the result is empty.
