> ## Documentation Index
> Fetch the complete documentation index at: https://docs.coreflux.org/llms.txt
> Use this file to discover all available pages before exploring further.

# PurpleAir

> Client-mode REST API connector for a curated PurpleAir v1 slice: list sensors and read one sensor

Technical Preview

# PurpleAir

Client-mode REST API connector for a curated PurpleAir v1 slice: list sensors and read one sensor.

* Hub category: **Public data**.
* Base: `https://api.purpleair.com`

## Template reference

Generated from the shipped broker template `purpleair` (`-routeCode PURPLEAIR`). It lists every event the template defines.

### Auth

Auth headers used by the template: `X-API-Key: Bearer {value.json.api_token}`.

### Template parameters

| Placeholder | Type | Description | Required |
| - | - | - | - |
| `RouteName` | string | Name of the PurpleAir connector | no |
| `BaseUrl` | string | PurpleAir base URL without trailing slash (e.g. [https://api.purpleair.com](https://api.purpleair.com)) | yes |

`BASE_ADDRESS` default: `https://api.purpleair.com`.

### Events

| Event | Method | Endpoint | Request topic | Response topic | Payload fields |
| - | - | - | - | - | - |
| `Listsensors` | GET | `/v1/sensors?fields=name,humidity,temperature,pm2.5` | `public/purpleair/sensors/list` | `public/purpleair/sensors/list/response` | `api_token` |
| `Getsensor` | GET | `/v1/sensors/{value.json.sensor_index}` | `public/purpleair/sensors/get` | `public/purpleair/sensors/get/response` | `api_token`, `sensor_index` |

### Deploy

Get the template card (JSON with `template` and `metadata`) from the broker, then deploy the connector:

```bash theme={"theme":"css-variables","languages":{"custom":["/languages/lot.json"]}}
mosquitto_pub -t '$SYS/Coreflux/Command' -m "-routeCode PURPLEAIR"
```

The deployable connector from the REST API app, with `BASE_ADDRESS` already filled in:

```lot theme={"theme":"css-variables","languages":{"custom":["/languages/lot.json"]}}
DEFINE ROUTE PurpleAirRestApi WITH TYPE REST_API
    ADD REST_API_CONFIG
        WITH BASE_ADDRESS "https://api.purpleair.com"
        WITH ENABLE_CLIENT "true"
        WITH USE_SSL "true"
        WITH REQUEST_TIMEOUT '60'
        WITH DEFAULT_CONTENT_TYPE "application/json"
    ADD EVENT Listsensors
        WITH SOURCE_TOPIC "public/purpleair/sensors/list"
        WITH DESTINATION_TOPIC "public/purpleair/sensors/list/response"
        WITH METHOD "GET"
        WITH ENDPOINT "/v1/sensors?fields=name,humidity,temperature,pm2.5"
        WITH HEADERS
            ADD "Accept" "application/json"
            ADD "X-API-Key" "Bearer {value.json.api_token}"
    ADD EVENT Getsensor
        WITH SOURCE_TOPIC "public/purpleair/sensors/get"
        WITH DESTINATION_TOPIC "public/purpleair/sensors/get/response"
        WITH METHOD "GET"
        WITH ENDPOINT "/v1/sensors/{value.json.sensor_index}"
        WITH HEADERS
            ADD "Accept" "application/json"
            ADD "X-API-Key" "Bearer {value.json.api_token}"
```

Deploy it with `-addRoute` (do not include `ADD METADATA`).

### Usage example

Call `Listsensors` by publishing a JSON payload to its request topic, and read the reply on the response topic:

```bash theme={"theme":"css-variables","languages":{"custom":["/languages/lot.json"]}}
mosquitto_sub -t 'public/purpleair/sensors/list/response' &
mosquitto_pub -t 'public/purpleair/sensors/list' -m '{"api_token": "<api_token>"}'
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.