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"
}
EOFReplace the following:
INFLUX_API_TOKEN: your InfluxDB API tokenINFLUX_ORG_ID: your InfluxDB organization ID
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.bucketparams.filterFieldparams.filterField2params.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,TLM0202The 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"
}
}
EOFInfluxDB 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,TLM0202List 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- Use
GET /api/v2/scriptsto retrieve an object that contains a list of scripts. - With the scripts array, use
jqto find the first invokable script object that has ascriptproperty that contains a hard-coded numeric date range. - Replace the hard-coded date range in the
scriptproperty with a newparams.myrangestartdynamic parameter and assign the object to anew_scriptvariable. - Assign the script ID to a
script_idvariable. - Update the script by sending a request to
PATCH /api/v2/scripts/with$script_idin the URL path and$new_scriptas 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!
Support and feedback
Thank you for being part of our community! We welcome and encourage your feedback and bug reports for InfluxDB and this documentation. To find support, use the following resources:
Customers with an annual or support contract can contact InfluxData Support.