---
title: Triggering functions from IoT Hub messages
description: This page explains how to configure your IoT Hub and a Serverless function to trigger the function when a message is published on the Hub.
tags: IoT Hub iot-hub Serverless FaaS function
dates:
  validation: 2025-07-16
  validation_frequency: 12
  posted: 2020-08-21
---
import Requirements from '@macros/iam/requirements.mdx'


Function as a Service allows developers to run code without the hassle of managing the server/Instance running it.

IoT Hub allows messages to be exchanged between devices and business-specific software. Some or all components of this software can be implemented using Scaleway Serverless Functions, and IoT Hub can be configured to trigger these functions to process messages being exchanged on the hub.

<Requirements />

- A Scaleway account logged into the [console](https://console.scaleway.com)
- [Owner](/iam/concepts/#owner) status or [IAM permissions](/iam/concepts/#permission) allowing you to perform actions in the intended Organization
- A [Serverless function](/serverless-functions/how-to/create-a-function/)
- Created an [IoT Hub](/iot-hub/how-to/create-hub/) and [added a device](/iot-hub/how-to/add-device/) to it

## How to set up a route to a function

1. Instruct your IoT Hub to trigger your function when a message is published on the `my/super/topic` topic. To do so we will use an IoT Hub REST Route.
2. Click the **Routes** tab of your IoT Hub.
3. Create a new route with the following settings:
    - Name: any name you want
    - Topic: `my/super/topic`
    - Route type: REST Query
    - Method: `POST`
    - Endpoint: the function endpoint you got earlier
    - HTTP Headers: leave empty

In a typical use case, the same function will be used for many topics. It is possible to achieve this by using MQTT wildcards in the route topic. You can then rely on the `X-Mqtt-Topic` HTTP header in your Serverless Function for your topic-based processing. See [this blog post](https://www.scaleway.com/en/blog/scaleway-iot-hub-introduction-to-mqtt-topics/) for more information about MQTT topics.

## How to trigger the function

1. Click the `MQTT Webclient` button on your device information page. The client will open and automatically connect.
2. Publish a message to the `my/super/topic` topic. In the `Publish` block:
    - Write `my/super/topic` as the topic
    - Leave QoS as 0
    - Leave Retain unticked
    - Write `Hello World !` as the message
    - Click `Publish` (no confirmation will be issued)

    You can see your published message in the function logs.

    <Message type="note">
      There may be a few minutes delay before the logs show up.
    </Message>

## How to reply to a message

In many cases, your function will want to publish a message to react to the received message.

To allow this action, add an `X-Mqtt-Topic` header to your reply, and a new message will be published on your IoT Hub under the topic.

The content of the message will be the body of the reply.

<Message type="important">
  The HTTP status code needs to be in the 2xx range.
</Message>


## How to use private functions

If you require authentication to protect your Serverless Function, here is how to do it:

1. Click the settings tab of your function settings tab and tick the `Private` option.
2. [Generate a token](/serverless-functions/how-to/create-auth-token-from-console/) for your function.
3. Re-create your IoT Hub Route with the following HTTP header: `SCW_FUNCTIONS_TOKEN: <function token here>`.

    Your function is now private and still accessible from IoT Hub.


