DEFINE ROUTE APIConnector WITH TYPE REST_API ADD REST_API_CONFIG WITH BASE_ADDRESS "https://api.example.com" WITH ENABLE_CLIENT "true" WITH USE_SSL "true" ADD EVENT GetData WITH SOURCE_TOPIC "api/requests/data" WITH DESTINATION_TOPIC "api/responses/data" WITH METHOD "GET" WITH ENDPOINT "/v1/data"
A REST API route bridges the HTTP and MQTT worlds. It contains events that define what happens when data flows through—either from MQTT to HTTP (client mode) or from HTTP to MQTT (server mode).
Fetch data from an external API when an MQTT message arrives:
Copy
Ask AI
DEFINE ROUTE WeatherAPI WITH TYPE REST_API ADD REST_API_CONFIG WITH BASE_ADDRESS "https://api.openweathermap.org" WITH ENABLE_CLIENT "true" WITH USE_SSL "true" WITH REQUEST_TIMEOUT '30' ADD EVENT GetWeather WITH SOURCE_TOPIC "weather/request" WITH DESTINATION_TOPIC "weather/response" WITH METHOD "GET" WITH ENDPOINT "/data/2.5/weather?q={value.json.city}&appid={value.json.apikey}"
When a message like {"city": "London", "apikey": "abc123"} arrives at weather/request, the route calls the API and publishes the weather data to weather/response.
Send data to an external API:
Copy
Ask AI
DEFINE ROUTE NotificationAPI WITH TYPE REST_API ADD REST_API_CONFIG WITH BASE_ADDRESS "https://api.notification-service.com" WITH ENABLE_CLIENT "true" WITH USE_SSL "true" ADD EVENT SendNotification WITH SOURCE_TOPIC "notifications/send" WITH DESTINATION_TOPIC "notifications/result" WITH METHOD "POST" WITH ENDPOINT "/v1/notify" WITH HEADERS ADD "Content-Type" "application/json" ADD "Authorization" "Bearer {value.json.api_key}" WITH BODY "{payload}"
The entire MQTT payload is forwarded as the HTTP request body using {payload}.
Use basic authentication for protected APIs:
Copy
Ask AI
DEFINE ROUTE AuthenticatedAPI WITH TYPE REST_API ADD REST_API_CONFIG WITH BASE_ADDRESS "https://secure-api.example.com" WITH ENABLE_CLIENT "true" WITH USERNAME "api_user" WITH PASSWORD "api_secret" WITH USE_SSL "true" ADD EVENT SecureRequest WITH SOURCE_TOPIC "secure/request" WITH DESTINATION_TOPIC "secure/response" WITH METHOD "GET" WITH ENDPOINT "/protected/data"
The username and password are sent as HTTP Basic Authentication headers automatically.
Use server mode when external systems need to send data to your broker via HTTP. The broker exposes an HTTP endpoint that accepts requests and publishes the data to MQTT topics.
Common use case: Your web application, mobile app, or third-party service needs to push data into the MQTT broker without implementing an MQTT client.
Expose an endpoint for devices to submit readings via HTTP:
Copy
Ask AI
DEFINE ROUTE SensorHTTPBridge WITH TYPE REST_API ADD REST_API_CONFIG WITH ENABLE_SERVER "true" WITH SERVER_PORT '8080' WITH ENABLE_CORS "true" ADD EVENT ReceiveSensorData WITH ENDPOINT "/api/sensors" WITH METHOD "POST" WITH DESTINATION_TOPIC "sensors/from/http"
The data is published to sensors/from/http and available to all MQTT subscribers.
Expose different endpoints for different data types:
Copy
Ask AI
DEFINE ROUTE MultiEndpointAPI WITH TYPE REST_API ADD REST_API_CONFIG WITH ENABLE_SERVER "true" WITH SERVER_PORT '8080' WITH ENABLE_CORS "true" ADD EVENT ReceiveTemperature WITH ENDPOINT "/api/temperature" WITH METHOD "POST" WITH DESTINATION_TOPIC "sensors/temperature/http" ADD EVENT ReceivePressure WITH ENDPOINT "/api/pressure" WITH METHOD "POST" WITH DESTINATION_TOPIC "sensors/pressure/http" ADD EVENT ReceiveCommands WITH ENDPOINT "/api/commands" WITH METHOD "POST" WITH DESTINATION_TOPIC "commands/from/http"
Each endpoint routes to a different MQTT topic for organized data handling.
Accept webhooks from external services:
Copy
Ask AI
DEFINE ROUTE WebhookReceiver WITH TYPE REST_API ADD REST_API_CONFIG WITH ENABLE_SERVER "true" WITH SERVER_PORT '9000' ADD EVENT GitHubWebhook WITH ENDPOINT "/webhooks/github" WITH METHOD "POST" WITH DESTINATION_TOPIC "webhooks/github/events" ADD EVENT StripeWebhook WITH ENDPOINT "/webhooks/stripe" WITH METHOD "POST" WITH DESTINATION_TOPIC "webhooks/stripe/events"
Configure GitHub or Stripe to send webhooks to http://your-broker:9000/webhooks/github. Events appear on the corresponding MQTT topics.
Enable cross-origin requests for browser-based applications:
Copy
Ask AI
DEFINE ROUTE WebAppAPI WITH TYPE REST_API ADD REST_API_CONFIG WITH ENABLE_SERVER "true" WITH SERVER_PORT '3000' WITH ENABLE_CORS "true" WITH CORS_ORIGINS "https://app.example.com, https://admin.example.com" ADD EVENT SubmitData WITH ENDPOINT "/api/submit" WITH METHOD "POST" WITH DESTINATION_TOPIC "webapp/submissions"
Only requests from the specified origins are allowed. Use "*" for development (not recommended for production).
A single route can operate in both modes—accepting HTTP requests AND calling external APIs:
Copy
Ask AI
DEFINE ROUTE BidirectionalHTTP WITH TYPE REST_API ADD REST_API_CONFIG WITH BASE_ADDRESS "https://external-api.com" WITH ENABLE_CLIENT "true" WITH ENABLE_SERVER "true" WITH SERVER_PORT '8080' WITH USE_SSL "true" WITH ENABLE_CORS "true" // Server: Accept HTTP, publish to MQTT ADD EVENT ReceiveFromHTTP WITH ENDPOINT "/api/receive" WITH METHOD "POST" WITH DESTINATION_TOPIC "http/incoming" // Client: MQTT triggers external HTTP call ADD EVENT CallExternalAPI WITH SOURCE_TOPIC "http/outgoing" WITH DESTINATION_TOPIC "http/response" WITH METHOD "POST" WITH ENDPOINT "/api/data"
Full API for a web dashboard to read sensor data and send commands:
Copy
Ask AI
DEFINE ROUTE DashboardAPI WITH TYPE REST_API ADD REST_API_CONFIG WITH ENABLE_SERVER "true" WITH SERVER_PORT '8080' WITH ENABLE_CORS "true" WITH DEFAULT_CONTENT_TYPE "application/json" ADD EVENT PostSensorData WITH ENDPOINT "/api/sensors" WITH METHOD "POST" WITH DESTINATION_TOPIC "dashboard/sensors/incoming" ADD EVENT PostCommand WITH ENDPOINT "/api/commands" WITH METHOD "POST" WITH DESTINATION_TOPIC "commands/from/dashboard" ADD EVENT PostAlert WITH ENDPOINT "/api/alerts" WITH METHOD "POST" WITH DESTINATION_TOPIC "alerts/from/dashboard"
Integrate with an external CRM or ERP system:
Copy
Ask AI
DEFINE ROUTE CRMIntegration WITH TYPE REST_API ADD REST_API_CONFIG WITH BASE_ADDRESS "https://api.crm-system.com" WITH ENABLE_CLIENT "true" WITH USE_SSL "true" WITH REQUEST_TIMEOUT '60' ADD EVENT CreateRecord WITH SOURCE_TOPIC "crm/create" WITH DESTINATION_TOPIC "crm/created" WITH METHOD "POST" WITH ENDPOINT "/v2/records" WITH HEADERS ADD "Authorization" "Bearer {value.json.token}" ADD "Content-Type" "application/json" WITH BODY "{value.json.data}" ADD EVENT UpdateRecord WITH SOURCE_TOPIC "crm/update" WITH DESTINATION_TOPIC "crm/updated" WITH METHOD "PUT" WITH ENDPOINT "/v2/records/{value.json.id}" WITH BODY "{value.json.data}" ADD EVENT DeleteRecord WITH SOURCE_TOPIC "crm/delete" WITH DESTINATION_TOPIC "crm/deleted" WITH METHOD "DELETE" WITH ENDPOINT "/v2/records/{value.json.id}"