Documentation

Invoke custom scripts as API endpoints

Use API invokable scripts to create custom InfluxDB API endpoints that query, process, and shape data. API invokable scripts let you assign scripts to API endpoints and then execute them as standard REST operations in InfluxDB Cloud. Learn how to manage API invokable scripts and invoke them with runtime parameters.

Use the /api/v2/scripts InfluxDB API endpoint to:

Create an invokable script

To create an API-invokable script for your organization, send a request using the POST method to the /api/v2/scripts InfluxDB API endpoint.

POST https://cloud2.influxdata.com/api/v2/scripts

Provide the following in your API request:

Request headers

  • Content-Type: application/json
  • Authorization: Token INFLUX_API_TOKEN

Request body

JSON object with the following fields:

  • script : Flux script as a string.
  • language : language of your script (“flux”)
  • name : script name, unique within your organization
  • description : script description
  • orgID: your InfluxDB organization ID

Use parameters in a script

To create an invokable script that accepts parameters (variables), reference the parameters as properties of the params object, e.g. params.firstparam. params is an InfluxDB object that defines runtime variables. You provide values for params when you invoke a script. If you don’t provide a value for a referenced parameter, InfluxDB returns the following error:

{
  "code":"invalid",
  "message":"invalid parameters provided"
}

Examples

Create an invokable Flux script

The following example creates a new Flux script that references the params.mybucket parameter and returns the last point from the bucket.

curl -X 'POST' \
  "https://cloud2.influxdata.com/api/v2/scripts" \
  --header "Authorization: Token ${INFLUX_TOKEN}" \
  --header 'accept: application/json' \
  --header 'Content-Type: application/json' \
  --data-binary @- << EOF | jq .
  {
    "name": "getLastPoint",
    "description": "getLastPoint finds the last point in a bucket",
    "orgID": "${INFLUX_ORG_ID}",
    "script": "from(bucket: params.mybucket) \
     |> range(start: -7d) \
     |> limit(n:1)",
     "language": "flux"
  }
EOF

Replace the following:

InfluxDB returns the newly created script. Next, see how to invoke a script.

{
  "id": "084d693d93048000",
  "orgID": "INFLUX_ORG_ID",
  "name": "getLastPoint",
  "script": "from(bucket: params.mybucket)      |> range(start: -7d)      |> limit(n:1)",
  "description": "getLastPoint finds the last point in a bucket",
  "language": "flux",
  "createdAt": "2021-10-15T20:32:04.172938Z",
  "updatedAt": "2021-10-15T20:32:04.172938Z"
}

Invoke a script

To invoke a script, send a request using the POST method to the /api/v2/scripts/SCRIPT_ID/invoke InfluxDB API endpoint.

POST https://cloud2.influxdata.com/api/v2/scripts/SCRIPT_ID

Replace SCRIPT_ID with the ID of the script you want to execute. To find the script ID, see how to list scripts.

Provide the following in your request:

Request headers

  • Content-Type: application/json
  • Accept: application/csv
  • Authorization: Token INFLUX_API_TOKEN

Request body

JSON object that contains a params object. In params, provide key-value pairs for parameters referenced in your script. The create example references the parameter params.mybucket.

  "from(bucket: params.mybucket) \
   |> range(start: -7d) \
   |> limit(n:1)"

The following example invokes the created script and passes “air_sensor” as the value for params.mybucket.

SCRIPT_ID=085138a111448000

curl -X 'POST' \
  "${INFLUX_URL}/api/v2/scripts/${SCRIPT_ID}/invoke" \
  --header "Authorization: Token ${INFLUX_TOKEN}" \
  --header 'Accept: application/csv' \
  --header 'Content-Type: application/json' \
  --data-binary '{ "params": { "mybucket": "air_sensor" } }'

InfluxDB returns query results in annotated CSV from the air_sensor bucket.

Pass multiple parameter values to a script

If the script references multiple parameters, provide values for all parameters. To provide values for multiple parameters, send an object that contains a params object. In params, add the parameter names as keys and define a value for each key.

The following invokable script object references four parameters:

    {
      "name": "filter-and-group",
      "description": "Filters and groups points in a bucket. Expects parameters bucket, filterField, filterField2, and groupColumn.",
      "orgID": "${INFLUX_ORG_ID}",
      "script": "from(bucket: params.bucket) \
                 |> range(start: -30d) \
                 |> filter(fn: (r) => r._field == params.filterField or r._field == params.filterField2) \
                 |> group(columns: [params.groupColumn])",
       "language": "flux"
    }

The Flux script references the following parameters:

  • params.bucket
  • params.filterField
  • params.filterField2
  • params.groupColumn

To provide values for the parameters, send a POST request to /api/v2/scripts/SCRIPT_ID/invoke and provide a JSON object that contains a params object. Inside the params object, define a key-value pair for each parameter referenced in the script. The object must be valid JSON.

Consider an air_sensor bucket that contains airSensors measurements. Each point has a temperature, humidity, or co field.

,result,table,_start,_stop,_time,_value,_field,_measurement,sensor_id
,_result,0,2021-09-25T22:20:11.493547551Z,2021-10-25T22:20:11.493547551Z,2021-09-28T16:13:05Z,75.30007505999716,temperature,airSensors,TLM0202
,_result,1,2021-09-25T22:20:11.493547551Z,2021-10-25T22:20:11.493547551Z,2021-09-28T16:13:05Z,73,temperature,airSensors,TLM0201
,_result,2,2021-09-25T22:20:11.493547551Z,2021-10-25T22:20:11.493547551Z,2021-09-28T16:13:05Z,35,humidity,airSensors,TLM0201
,_result,3,2021-09-25T22:20:11.493547551Z,2021-10-25T22:20:11.493547551Z,2021-09-28T16:13:05Z,0.5141876544505826,co,airSensors,TLM0202
,_result,4,2021-09-25T22:20:11.493547551Z,2021-10-25T22:20:11.493547551Z,2021-09-28T16:13:05Z,0.48445310567793615,co,airSensors,TLM0201
,_result,5,2021-09-25T22:20:11.493547551Z,2021-10-25T22:20:11.493547551Z,2021-09-28T16:13:05Z,35.651929918691714,humidity,airSensors,TLM0202

The following params object provides a key-value pair for each parameter referenced in the script.

{ "params":
  {
    "bucket": "air_sensor",
    "filterField": "temperature",
    "filterField2": "humidity",
    "groupColumn": "_time"
  }
}

The following example uses the /api/v2/scripts InfluxDB API endpoint to create the script and invoke the new script ID with params.

new_script_id=$(
  curl -v -X 'POST' \
    "${INFLUX_URL}/api/v2/scripts" \
    --header "Authorization: Token ${INFLUX_TOKEN}" \
    --header 'Accept: application/json' \
    --header 'Content-Type: application/json' \
    --data-binary @- << EOF | jq -r '.id' 
    {
      "name": "filter-and-group",
      "description": "Filters and groups points in a bucket. Expects parameters bucket, filterField, filterField2, and groupColumn.",
      "orgID": "${INFLUX_ORG_ID}",
      "script": "from(bucket: params.bucket) \
                 |> range(start: -30d) \
                 |> filter(fn: (r) => r._field == params.filterField or r._field == params.filterField2) \
                 |> group(columns: [params.groupColumn])",
       "language": "flux"
    }
EOF
)

curl -vv -X 'POST' \
  "${INFLUX_URL}/api/v2/scripts/${new_script_id}/invoke" \
  --header "Authorization: Token ${INFLUX_TOKEN}" \
  --header 'Accept: application/csv' \
  --header 'Content-Type: application/json' \
  --data-binary @- << EOF
    { "params":
      {
        "bucket": "air_sensor",
        "filterField": "temperature",
        "filterField2": "humidity",
        "groupColumn": "_time"
      }
    }
EOF

InfluxDB returns points from air_sensor that have temperature or humidity fields. Points are grouped by the _time column. Each unique table value represents a group.

,result,table,_start,_stop,_time,_value,_field,_measurement,sensor_id
,_result,0,2021-09-25T21:10:01.810564864Z,2021-10-25T21:10:01.810564864Z,2021-09-28T16:13:05Z,73,temperature,airSensors,TLM0201
,_result,0,2021-09-25T21:10:01.810564864Z,2021-10-25T21:10:01.810564864Z,2021-09-28T16:13:05Z,75.30007505999716,temperature,airSensors,TLM0202
,_result,0,2021-09-25T21:10:01.810564864Z,2021-10-25T21:10:01.810564864Z,2021-09-28T16:13:05Z,35,humidity,airSensors,TLM0201
,_result,0,2021-09-25T21:10:01.810564864Z,2021-10-25T21:10:01.810564864Z,2021-09-28T16:13:05Z,35.651929918691714,humidity,airSensors,TLM0202
,_result,1,2021-09-25T21:10:01.810564864Z,2021-10-25T21:10:01.810564864Z,2021-09-28T16:57:57Z,75.30007505999716,temperature,airSensors,TLM0202
,_result,1,2021-09-25T21:10:01.810564864Z,2021-10-25T21:10:01.810564864Z,2021-09-28T16:57:57Z,35.651929918691714,humidity,airSensors,TLM0202

List invokable scripts

To list scripts for an organization, send a request using the GET method to the /api/v2/scripts InfluxDB API endpoint.

Provide the following in your request:

Request headers

  • Content-Type: application/json
  • Authorization: Token INFLUX_API_TOKEN

Request query parameters

  • org: your organization name (mutually exclusive with orgID)
  • orgID: your organization ID (mutually exclusive with org)
  • limit: (Optional) number of scripts to return
  • offset: (Optional) number to offset the pagination
curl -X 'GET' \
  "${INFLUX_URL}/api/v2/scripts" \
  --header "Authorization: Token ${INFLUX_TOKEN}" \
  --header 'accept: application/json' \
  --header 'Content-Type: application/json' \
  --data-urlencode "org=${INFLUX_ORG}" \
  --data-urlencode "limit=10"

To find a specific script for an organization, send a request using the GET method to the /api/v2/scripts/SCRIPT_ID InfluxDB API endpoint.

GET https://cloud2.influxdata.com/api/v2/scripts/SCRIPT_ID

Replace SCRIPT_ID with the ID of the script you want to find.

Provide the following in your request:

Request headers

  • Authorization: Token INFLUX_API_TOKEN
  • Accept: application/json
curl -X 'GET' \
  "${INFLUX_URL}/api/v2/scripts/${SCRIPT_ID}" \
  --header "Authorization: Token ${INFLUX_TOKEN}" \
  --header 'accept: application/json' \
  --header 'Content-Type: application/json'

Update an invokable script

Use the API to replace the following properties of an invokable script:

  • name
  • description
  • script

To update an existing script for an organization, send a request using the PATCH method to the /api/v2/scripts/SCRIPT_ID InfluxDB API endpoint. Replace SCRIPT_ID with the ID of the script you want to update.

PATCH https://cloud2.influxdata.com/api/v2/scripts/SCRIPT_ID

Provide the following in your request:

Request headers

  • Authorization: Token INFLUX_API_TOKEN
  • Content-Type: application/json
  • Accept: application/json'

Request body

  • A modified invokable script as a JSON object.

The following example finds an invokable script containing a numeric date range, replaces the date with a new parameter, and updates the invokable script.

find_and_update() {
  script=$(curl -X 'GET' \
    "https://cloud2.influxdata.com/api/v2/scripts" \
    --header "Authorization: Token ${INFLUX_TOKEN}" \
    --header 'Accept: application/json' \
    --header 'Content-Type: application/json' \
    --data-urlencode "org=${INFLUX_ORG}&limit=10" \
    | jq '[.scripts[] | select(.script | test("start: -?\\d\\w"))]' \
    | jq '.[0]')
  new_script=$(jq '.script |= sub("start: .*d"; "start: params.myrangestart")' <<< "${script}")
  script_id=$(jq -r '.id' <<< "${new_script}")

  curl -X 'PATCH' \
    "${INFLUX_URL}/api/v2/scripts/${script_id}" \
    --header "Authorization: Token ${INFLUX_TOKEN}" \
    --header 'Accept: application/json' \
    --header 'Content-Type: application/json' \
    --data "${new_script}" | jq .
}

find_and_update
  1. Use GET /api/v2/scripts to retrieve an object that contains a list of scripts.
  2. With the scripts array, use jq to find the first invokable script object that has a script property that contains a hard-coded numeric date range.
  3. Replace the hard-coded date range in the script property with a new params.myrangestart dynamic parameter and assign the object to a new_script variable.
  4. Assign the script ID to a script_id variable.
  5. Update the script by sending a request to PATCH /api/v2/scripts/ with $script_id in the URL path and $new_script as data (in the request body).

InfluxDB returns the updated invokable script.

Delete an invokable script

To delete a script, send a request using the DELETE method to the /api/v2/scripts/SCRIPT_ID InfluxDB API endpoint. Replace SCRIPT_ID with the ID of the script you want to update.

DELETE https://cloud2.influxdata.com/api/v2/scripts/SCRIPT_ID

Provide the following in your request:

Request headers

  • Authorization: Token INFLUX_API_TOKEN
  • Accept: application/json'

Was this page helpful?

Thank you for your feedback!


The future of Flux

Flux is going into maintenance mode. You can continue using it as you currently are without any changes to your code.

Read more

InfluxDB v3 enhancements and InfluxDB Clustered is now generally available

New capabilities, including faster query performance and management tooling advance the InfluxDB v3 product line. InfluxDB Clustered is now generally available.

InfluxDB v3 performance and features

The InfluxDB v3 product line has seen significant enhancements in query performance and has made new management tooling available. These enhancements include an operational dashboard to monitor the health of your InfluxDB cluster, single sign-on (SSO) support in InfluxDB Cloud Dedicated, and new management APIs for tokens and databases.

Learn about the new v3 enhancements


InfluxDB Clustered general availability

InfluxDB Clustered is now generally available and gives you the power of InfluxDB v3 in your self-managed stack.

Talk to us about InfluxDB Clustered

InfluxDB Cloud powered by TSM