---
title: Get started with InfluxDB tasks
description: Learn the basics of writing an InfluxDB task that processes data, and then performs an action, such as storing the modified data in a new bucket or sending an alert.
url: https://docs.influxdata.com/influxdb/v2/process-data/get-started/
estimated_tokens: 3945
product: InfluxDB OSS v2
version: v2
---

# Get started with InfluxDB tasks

This page documents an earlier version of InfluxDB OSS. [InfluxDB 3 Core](/influxdb3/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](/influxdb/v2/admin/tokens/#token-hashing).

An **InfluxDB task** is a scheduled Flux script that takes a stream of input data, modifies or analyzes it in some way, then writes the modified data back to InfluxDB or performs other actions.

This article walks through writing a basic InfluxDB task that downsamples data and stores it in a new bucket.

## Components of a task

Every InfluxDB task needs the following components. Their form and order can vary, but they are all essential parts of a task.

-   [Task options](#define-task-options)
-   [A data source](#define-a-data-source)
-   [Data processing or transformation](#process-or-transform-your-data)
-   [A destination](#define-a-destination)

*[Skip to the full example task script](#full-example-flux-task-script)*

## Define task options

Task options define the schedule, name, and other information about the task. The following example shows how to set task options in a Flux script:

```js
option task = {name: "downsample_5m_precision", every: 1h, offset: 0m}
```

*See [Task configuration options](/influxdb/v2/process-data/task-options) for detailed information about each option.*

*Note that InfluxDB doesn’t guarantee that a task will run at the scheduled time. See [View task run logs for a task](/influxdb/v2/process-data/manage-tasks/task-run-history) for detailed information on task service-level agreements (SLAs).*

The InfluxDB UI provides a form for defining task options.

## Retrieve and filter data

A minimal Flux script uses the following functions to retrieve a specified amount of data from a data source and then filter the data based on time or column values:

1. [`from()`](/flux/v0/stdlib/influxdata/influxdb/from/): queries data from InfluxDB .
2. [`range()`](/flux/v0/stdlib/universe/range/): defines the time range to return data from.
3. [`filter()`](/flux/v0/stdlib/universe/filter/): filters data based on column values.

The following sample Flux retrieves data from an InfluxDB bucket and then filters by the `_measurement` and `host` columns:

```js
from(bucket: "example-bucket")
    |> range(start: -task.every)
    |> filter(fn: (r) => r._measurement == "mem" and r.host == "myHost")
```

*To retrieve data from other sources, see [Flux input functions](/flux/v0/function-types/#inputs).*

#### Use task options in your Flux script

InfluxDB stores options in a `task` option record that you can reference in your Flux script. The following sample Flux uses the time range `-task.every`:

```js
from(bucket: "example-bucket")
    |> range(start: -task.every)
    |> filter(fn: (r) => r._measurement == "mem" and r.host == "myHost")
```

`task.every` is dot notation that references the `every` property of the `task` option record. `every` is defined as `1h`, therefore `-task.every` equates to `-1h`.

Using task options to define values in your Flux script can make reusing your task easier.

## Process or transform your data

Tasks run scripts automatically at regular intervals. Scripts process or transform data in some way–for example: downsampling, detecting anomalies, or sending notifications.

Consider a task that runs hourly and downsamples data by calculating the average of set intervals. It uses [`aggregateWindow()`](/flux/v0/stdlib/universe/aggregatewindow/) to group points into 5-minute (`5m`) windows and calculate the average of each window with [`mean()`](/flux/v0/stdlib/universe/mean/).

The following sample code shows the Flux script with task options:

```js
option task = {name: "downsample_5m_precision", every: 1h, offset: 0m}

from(bucket: "example-bucket")
    |> range(start: -task.every)
    |> filter(fn: (r) => r._measurement == "mem" and r.host == "myHost")
    |> aggregateWindow(every: 5m, fn: mean)
```

#### Use offset to account for latent data

Use the `offset` task option to account for potentially latent data (like data from edge devices). A task that runs at one hour intervals (`every: 1h`) with an offset of five minutes (`offset: 5m`) executes 5 minutes after the hour, but queries data from the original one-hour interval.

*See [Common tasks](/influxdb/v2/process-data/common-tasks) for examples of tasks commonly used with InfluxDB.*

## Define a destination

In most cases, you’ll want to send and store data after the task has transformed it. The destination could be a separate InfluxDB measurement or bucket.

The example below uses [`to()`](/flux/v0/stdlib/universe/to) to write the transformed data back to another InfluxDB bucket:

```js
// ...
    |> to(bucket: "example-downsampled", org: "my-org")
```

To write data into InfluxDB, `to()` requires the following columns:

-   `_time`
-   `_measurement`
-   `_field`
-   `_value`

*To write data to other destinations, see [Flux output functions](/flux/v0/function-types/#outputs).*

## Full example Flux task script

The following sample Flux combines all the components described in this guide:

```js
// Task options
option task = {name: "downsample_5m_precision", every: 1h, offset: 0m}

// Data source
from(bucket: "example-bucket")
    |> range(start: -task.every)
    |> filter(fn: (r) => r._measurement == "mem" and r.host == "myHost")
    // Data processing
    |> aggregateWindow(every: 5m, fn: mean)
    // Data destination
    |> to(bucket: "example-downsampled")
```

To learn more about InfluxDB tasks and how they work, watch the following video:

#### Related

-   [Manage tasks in InfluxDB](/influxdb/v2/process-data/manage-tasks/)
-   [Create a task](/influxdb/v2/process-data/manage-tasks/create-task/)
-   [Tasks & InfluxDB](/resources/videos/influxdb-tasks/)

[tasks](/influxdb/v2/tags/tasks/)
