Documentation

Create custom Flux functions

This page documents an earlier version of InfluxDB OSS. InfluxDB 3 Core is the latest stable version.

API token hashing is enabled by default in InfluxDB OSS 2.9.0

Stronger token security: tokens are stored as hashes on disk, so a copy of the database file doesn’t expose usable tokens. Existing tokens are hashed on first startup and the original strings can’t be recovered afterward — capture any plaintext tokens you still need before you upgrade.

For more information, see Token hashing.

Flux’s functional syntax lets you create custom functions. This guide walks through the basics of creating your own function.

Function definition syntax

The basic syntax for defining functions in Flux is as follows:

// Basic function definition syntax
functionName = (functionParameters) => functionOperations
functionName

The name used to call the function in your Flux script.

functionParameters

A comma-separated list of parameters passed into the function and used in its operations. Parameter defaults can be defined for each.

functionOperations

Operations and functions that manipulate the input into the desired output.

Basic function examples

Example square function
// Function definition
square = (n) => n * n

// Function usage
> square(n:3)
9
Example multiply function
// Function definition
multiply = (x, y) => x * y

// Function usage
> multiply(x: 2, y: 15)
30

Use piped-forward data in a custom function

Most Flux functions process piped-forward data. To process piped-forward data, one of the function parameters must capture the input tables using the <- pipe-receive expression.

In the example below, the tables parameter is assigned to the <- expression, which represents all data piped-forward into the function. tables is then piped-forward into other operations in the function definition.

functionName = (tables=<-) => tables |> functionOperations

Pipe-forwardable function example

Multiply row values by x

The example below defines a multByX function that multiplies the _value column of each row in the input table by the x parameter. It uses the map() function to modify each _value.

// Function definition
multByX = (tables=<-, x) => tables
    |> map(fn: (r) => ({r with _value: r._value * x}))

// Function usage
from(bucket: "example-bucket")
    |> range(start: -1m)
    |> filter(fn: (r) => r._measurement == "mem" and r._field == "used_percent")
    |> multByX(x: 2.0)

Define parameter defaults

Use the = assignment operator to assign a default value to function parameters in your function definition:

functionName = (param1=defaultValue1, param2=defaultValue2) => functionOperation

Defaults are overridden by explicitly defining the parameter in the function call.

Example functions with defaults

Get a list of leaders

The example below defines a leaderBoard function that returns a limited number of records sorted by values in specified columns. It uses the sort() function to sort records in either descending or ascending order. It then uses the limit() function to return a specified number of records from the sorted table.

// Function definition
leaderBoard = (tables=<-, limit=4, columns=["_value"], desc=true) => tables
    |> sort(columns: columns, desc: desc)
    |> limit(n: limit)

// Function usage
// Get the 4 highest scoring players
from(bucket: "example-bucket")
    |> range(start: -1m)
    |> filter(fn: (r) => r._measurement == "player-stats" and r._field == "total-points")
    |> leaderBoard()

// Get the 10 shortest race times
from(bucket: "example-bucket")
    |> range(start: -1m)
    |> filter(fn: (r) => r._measurement == "race-times" and r._field == "elapsed-time")
    |> leaderBoard(limit: 10, desc: false)

Define functions with scoped variables

To create custom functions with variables scoped to the function, place your function operations and variables inside of a block ({}) and use a return statement to return a specific variable.

functionName = (functionParameters) => {
    exampleVar = "foo"
    
    return exampleVar
}

Example functions with scoped variables

Return an alert level based on a value

The following function uses conditional logic to return an alert level based on a numeric input value:

alertLevel = (v) => {
    level = if float(v: v) >= 90.0 then
        "crit"
    else if float(v: v) >= 80.0 then
        "warn"
    else if float(v: v) >= 65.0 then
        "info"
    else
        "ok"

    return level
}

alertLevel(v: 87.3)
// Returns "warn"

Convert a HEX color code to a name

The following function converts a hexadecimal (HEX) color code to the equivalent HTML color name. The functions uses the Flux dictionary package to create a dictionary of HEX codes and their corresponding names.

import "dict"

hexName = (hex) => {
    hexNames = dict.fromList(
        pairs: [
            {key: "#00ffff", value: "Aqua"},
            {key: "#000000", value: "Black"},
            {key: "#0000ff", value: "Blue"},
            {key: "#ff00ff", value: "Fuchsia"},
            {key: "#808080", value: "Gray"},
            {key: "#008000", value: "Green"},
            {key: "#00ff00", value: "Lime"},
            {key: "#800000", value: "Maroon"},
            {key: "#000080", value: "Navy"},
            {key: "#808000", value: "Olive"},
            {key: "#800080", value: "Purple"},
            {key: "#ff0000", value: "Red"},
            {key: "#c0c0c0", value: "Silver"},
            {key: "#008080", value: "Teal"},
            {key: "#ffffff", value: "White"},
            {key: "#ffff00", value: "Yellow"},
        ],
    )
    name = dict.get(dict: hexNames, key: hex, default: "No known name")

    return name
}

hexName(hex: "#000000")
// Returns "Black"

hexName(hex: "#8b8b8b")
// Returns "No known name"

Was this page helpful?

Thank you for your feedback!


InfluxDB OSS 2.9.0: API tokens are hashed by default

Stronger token security in InfluxDB OSS 2.9.0 — tokens are hashed on disk by default. Existing tokens are hashed on first startup and can’t be recovered afterward. Capture any plaintext tokens you still need before you upgrade.

View InfluxDB OSS 2.9.0 release notes

Hashed tokens authenticate exactly like unhashed tokens — clients and integrations keep working.

Also new in 2.9.0:

  • Configurable backup compression
  • Restore support for backups containing hashed tokens
  • Tighter Edge Data Replication queue validation
  • Flux upgrade
  • Compaction reliability improvements

Key enhancements in Explorer 1.8

Explorer 1.8 is now available with streaming data subscriptions (beta), line protocol preview, and query history & saved queries.

View Explorer 1.8 release notes

Explorer 1.8 includes new features and improvements that make it easier to ingest, explore, and manage data.

Highlights:

  • Streaming data subscriptions (beta): Stream data into Explorer from MQTT, Kafka, and AMQP sources.
  • Line protocol preview: Preview line protocol, schema, and parse errors before data is written.
  • Custom sample data: Generate custom sample datasets with line protocol and schema preview.
  • Query history and saved queries: Browse query history and save/re-run named queries.
  • Retention period management: Set, update, or clear retention periods on databases and tables.

For more details, see Explorer 1.8 release notes

InfluxDB 3.9: Performance upgrade preview

InfluxDB 3 Enterprise 3.9 includes a beta of major performance upgrades with faster single-series queries, wide-and-sparse table support, and more.

InfluxDB 3 Enterprise 3.9 includes a beta of major performance and feature updates.

Key improvements:

  • Faster single-series queries
  • Consistent resource usage
  • Wide-and-sparse table support
  • Automatic distinct value caches for reduced latency with metadata queries

Preview features are subject to breaking changes.

For more information, see:

Telegraf Enterprise now in public beta

Get early access to the Telegraf Controller and provide feedback to help shape the future of Telegraf Enterprise.

See the Blog Post

The upcoming Telegraf Enterprise offering is for organizations running Telegraf at scale and is comprised of two key components:

  • Telegraf Controller: A control plane (UI + API) that centralizes Telegraf configuration management and agent health visibility.
  • Telegraf Enterprise Support: Official support for Telegraf Controller and Telegraf plugins.

Join the Telegraf Enterprise beta to get early access to the Telegraf Controller and provide feedback to help shape the future of Telegraf Enterprise.

For more information:

Telegraf Controller v0.0.7-beta now available

Telegraf Controller v0.0.7-beta is now available with new features, improvements, bug fixes, and an important breaking change.

View the release notes
Download Telegraf Controller v0.0.7-beta

InfluxDB Docker latest tag changing to InfluxDB 3 Core

On May 27, 2026, the latest tag for InfluxDB Docker images will point to InfluxDB 3 Core. To avoid unexpected upgrades, use specific version tags in your Docker deployments.

If using Docker to install and run InfluxDB, the latest tag will point to InfluxDB 3 Core. To avoid unexpected upgrades, use specific version tags in your Docker deployments. For example, if using Docker to run InfluxDB v2, replace the latest version tag with a specific version tag in your Docker pull command–for example:

docker pull influxdb:2