## Overview

The data bridge connects FastBee with external data systems. It can integrate with MQTT servers, HTTP services, and relational databases such as MySQL, SQL Server, Oracle, and PostgreSQL.

With the data bridge, users can forward messages from FastBee to external systems in real time, or pull data from external systems and publish it to specified MQTT topics. Combined with rule scripts, data processing and distribution can be completed without writing additional backend code.

Bridge input supports:

* Product-level events such as device online/offline, device reporting, and platform command delivery.
* Data-flow input through HTTP access or MQTT topic subscription.

Bridge output supports:

* Built-in MQTT republishing.
* Publishing to an external MQTT server.
* Sending data to third-party HTTP OpenAPI endpoints.
* Inserting data into relational databases through SQL.

::: warning
Input and output can be used together, making data forwarding and stream processing more flexible.
:::

## Bridge Input

### HTTP Input

FastBee provides fixed HTTP access routes such as `/bridege/get`, `/bridege/post`, and `/bridege/put`. Authentication uses a JWT token. Create a system authorization token and include it in the request header.

Operation path:

```text
Data Bridge -> Add
Select input direction -> Select HTTP bridge -> Select access route

Rule Script -> Add
Select HTTP input -> Select HTTP bridge access point -> Edit script content
```

### MQTT Input

Operation path:

```text
Data Bridge -> Add
Select input direction -> Select MQTT bridge -> Enter server configuration and bridge topic

Rule Script -> Add
Select MQTT input -> Select MQTT bridge access point -> Edit script content
```

## Bridge Output

### Built-In MQTT Republishing

After data enters the platform, a rule script can adjust the republished topic, payload format, and calculation logic, then publish the message back to the internal MQTT server for business processing.

### External MQTT Server Output

1. Create an MQTT bridge configuration: **Rule Engine -> Data Bridge -> Add**.

<img src="../../../../business/v2/manual/images/bridge/bridge-01.png" style="border:1px solid #ccc;border-radius:6px;margin:8px 0;" alt="External MQTT Server Output" loading="lazy" />

Configuration fields:

* Bridge name: the configuration name.
* Bridge direction: output.
* MQTT server address: address and port, for example `fastbee.cn:1883` or `101.33.237.12:1883`.
* Client ID: MQTT client ID, for example `S&D1LN0D9DN5X6&205&1`.
* Username and password: credentials for connecting to the MQTT server.
* Bridge route: can be empty for output direction.

After adding the configuration, use the connection test button to verify whether the MQTT client can be created successfully.

<img src="../../../../business/v2/manual/images/bridge/bridge-02.png" style="border:1px solid #ccc;border-radius:6px;margin:8px 0;" alt="External MQTT Server Output" loading="lazy" />

2. Create an MQTT output script: **Rule Engine -> Rule Script -> Add**.

<img src="../../../../business/v2/manual/images/bridge/bridge-03.png" style="border:1px solid #ccc;border-radius:6px;margin:8px 0;" alt="External MQTT Server Output" loading="lazy" />
<img src="../../../../business/v2/manual/images/bridge/bridge-04.png" style="border:1px solid #ccc;border-radius:6px;margin:8px 0;" alt="External MQTT Server Output" loading="lazy" />
<img src="../../../../business/v2/manual/images/bridge/bridge-05.png" style="border:1px solid #ccc;border-radius:6px;margin:8px 0;" alt="External MQTT Server Output" loading="lazy" />

3. Test the script with MQTTX by selecting the product and device associated with the rule script and simulating device reporting.

<img src="../../../../business/v2/manual/images/bridge/bridge-06.png" style="border:1px solid #ccc;border-radius:6px;margin:8px 0;" alt="External MQTT Server Output" loading="lazy" />

The following screenshots show local device data bridged to the FastBee demo site. Both platforms can receive the reported data through the MQTT bridge.

<img src="../../../../business/v2/manual/images/bridge/bridge-07.png" style="border:1px solid #ccc;border-radius:6px;margin:8px 0;" alt="External MQTT Server Output" loading="lazy" />
<img src="../../../../business/v2/manual/images/bridge/bridge-08.png" style="border:1px solid #ccc;border-radius:6px;margin:8px 0;" alt="External MQTT Server Output" loading="lazy" />

## HTTP OpenAPI Output

### FastBee Configuration

Create an HTTP bridge configuration from **Rule Engine -> Data Bridge -> Add**.

<img src="../../../../business/v2/manual/images/bridge/bridge-09.png" style="border:1px solid #ccc;border-radius:6px;margin:8px 0;" alt="FastBee Configuration" loading="lazy" />

Configuration fields:

* Bridge name: configuration name.
* Bridge direction: output.
* Request method: choose `GET`, `POST`, or `PUT` according to the target API.
* Bridge address: full API URL, for example `http://localhost:18083/api/v5/metrics`.
* Request headers: add target API authentication parameters, such as `Authorization` and `application/json`.
* Request parameters and body: configure according to the target API document.

Create an HTTP bridge output rule script:

<img src="../../../../business/v2/manual/images/bridge/bridge-10.png" style="border:1px solid #ccc;border-radius:6px;margin:8px 0;" alt="FastBee Configuration" loading="lazy" />
<img src="../../../../business/v2/manual/images/bridge/bridge-11.png" style="border:1px solid #ccc;border-radius:6px;margin:8px 0;" alt="FastBee Configuration" loading="lazy" />
<img src="../../../../business/v2/manual/images/bridge/bridge-12.png" style="border:1px solid #ccc;border-radius:6px;margin:8px 0;" alt="FastBee Configuration" loading="lazy" />

### Output Test

Use MQTTX to simulate device data reporting and verify backend logs.

<img src="../../../../business/v2/manual/images/bridge/bridge-13.png" style="border:1px solid #ccc;border-radius:6px;margin:8px 0;" alt="Output Test" loading="lazy" />
<img src="../../../../business/v2/manual/images/bridge/bridge-14.png" style="border:1px solid #ccc;border-radius:6px;margin:8px 0;" alt="Output Test" loading="lazy" />

## Third-Party Integration References

### EMQX

Reference documentation: `https://www.emqx.io/docs/en/v5.1/admin/api.html`

1. Create an API key in the MQTT server.
2. Use HTTP Basic authentication or Bearer authentication according to the target API.
3. Create the corresponding HTTP bridge configuration in FastBee.

<img src="../../../../business/v2/manual/images/bridge/bridge-15.png" style="border:1px solid #ccc;border-radius:6px;margin:8px 0;" alt="EMQX" loading="lazy" />
<img src="../../../../business/v2/manual/images/bridge/bridge-16.png" style="border:1px solid #ccc;border-radius:6px;margin:8px 0;" alt="EMQX" loading="lazy" />
<img src="../../../../business/v2/manual/images/bridge/bridge-17.png" style="border:1px solid #ccc;border-radius:6px;margin:8px 0;" alt="EMQX" loading="lazy" />

### Ezviz

Reference documentation: `https://open.ys7.com/help/81`

Common HTTP parameters include `Content-Type: application/x-www-form-urlencoded` and `accessToken`.

<img src="../../../../business/v2/manual/images/bridge/bridge-18.png" style="border:1px solid #ccc;border-radius:6px;margin:8px 0;" alt="Ezviz" loading="lazy" />
<img src="../../../../business/v2/manual/images/bridge/bridge-19.png" style="border:1px solid #ccc;border-radius:6px;margin:8px 0;" alt="Ezviz" loading="lazy" />
<img src="../../../../business/v2/manual/images/bridge/bridge-20.png" style="border:1px solid #ccc;border-radius:6px;margin:8px 0;" alt="Ezviz" loading="lazy" />

### OneNET

Create the required API credentials on the OneNET platform and configure the matching request headers and parameters in FastBee.

<img src="../../../../business/v2/manual/images/bridge/bridge-21.png" style="border:1px solid #ccc;border-radius:6px;margin:8px 0;" alt="OneNET" loading="lazy" />
<img src="../../../../business/v2/manual/images/bridge/bridge-22.png" style="border:1px solid #ccc;border-radius:6px;margin:8px 0;" alt="OneNET" loading="lazy" />
<img src="../../../../business/v2/manual/images/bridge/bridge-23.png" style="border:1px solid #ccc;border-radius:6px;margin:8px 0;" alt="OneNET" loading="lazy" />

### Baidu AI Cloud

Reference documentation: `https://cloud.baidu.com/doc/EVS/s/Ekowke5c8`

1. Create an application.
2. Obtain `access_token`.
3. Configure `Content-Type: application/json; charset=utf-8` and the required token parameters.

<img src="../../../../business/v2/manual/images/bridge/bridge-24.png" style="border:1px solid #ccc;border-radius:6px;margin:8px 0;" alt="Baidu AI Cloud" loading="lazy" />
<img src="../../../../business/v2/manual/images/bridge/bridge-25.png" style="border:1px solid #ccc;border-radius:6px;margin:8px 0;" alt="Baidu AI Cloud" loading="lazy" />
<img src="../../../../business/v2/manual/images/bridge/bridge-26.png" style="border:1px solid #ccc;border-radius:6px;margin:8px 0;" alt="Baidu AI Cloud" loading="lazy" />

### Hikvision API

Register an account on the Hikvision platform, obtain `ak`, `sk`, and token information, then configure the request headers in FastBee.

<img src="../../../../business/v2/manual/images/bridge/bridge-27.png" style="border:1px solid #ccc;border-radius:6px;margin:8px 0;" alt="Hikvision API" loading="lazy" />
<img src="../../../../business/v2/manual/images/bridge/bridge-28.png" style="border:1px solid #ccc;border-radius:6px;margin:8px 0;" alt="Hikvision API" loading="lazy" />
<img src="../../../../business/v2/manual/images/bridge/bridge-29.png" style="border:1px solid #ccc;border-radius:6px;margin:8px 0;" alt="Hikvision API" loading="lazy" />

For other platforms, follow the API document of the target platform and map authentication, headers, parameters, and body fields to FastBee bridge configuration.

## Database Storage

### Create Database Storage Configuration

Create a database storage bridge from **Rule Engine -> Data Bridge -> Add**.

<img src="../../../../business/v2/manual/images/bridge/bridge-30.png" style="border:1px solid #ccc;border-radius:6px;margin:8px 0;" alt="Create Database Storage Configuration" loading="lazy" />

Configuration fields:

* Bridge name.
* Bridge direction: usually input.
* Database type: relational databases are currently supported.
* Data source: MySQL, Oracle, SQL Server, PostgreSQL, and similar databases.
* Connection address: IP and port, for example `localhost:3306`.
* Username and password.
* Database name.
* SQL statement.

Example:

```sql
INSERT INTO database_storage(topic, payload, serial_number, product_id, protocol_code)
VALUES ('${topic}', '${payload}', '${serialNumber}', '${productId}', '${protocolCode}');
```

Custom values can be set in the rule script and referenced in SQL, HTTP body, or other output templates:

```js
msgContext.setData("test", 1);
```

::: warning
When using custom placeholders such as `${field}` inside SQL `values`, keep single quotes for string values to avoid SQL errors.
:::

### Create Database Storage Rule Script

Create a rule script from **Rule Engine -> Rule Script -> Add**.

<img src="../../../../business/v2/manual/images/bridge/bridge-31.png" style="border:1px solid #ccc;border-radius:6px;margin:8px 0;" alt="Create Database Storage Rule Script" loading="lazy" />

The following fields are already encapsulated by the backend and can be omitted unless custom SQL requires additional values:

```js
msgContext.setData("topic", NewTopic);
msgContext.setData("payload", NewPayload);
msgContext.setData("serialNumber", serialNumber);
msgContext.setData("productId", productId);
msgContext.setData("protocolCode", protocolCode);
```

### Test Database Storage

<img src="../../../../business/v2/manual/images/bridge/bridge-32.png" style="border:1px solid #ccc;border-radius:6px;margin:8px 0;" alt="Test Database Storage" loading="lazy" />
<img src="../../../../business/v2/manual/images/bridge/bridge-33.png" style="border:1px solid #ccc;border-radius:6px;margin:8px 0;" alt="Test Database Storage" loading="lazy" />
