content/influxdb/cloud/api-guide/api-invokable-scripts/_index.md
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:
To create an API-invokable script
for your organization,
send a request using the POST method to the /api/v2/scripts InfluxDB API endpoint.
{{< api-endpoint method="post" endpoint="https://cloud2.influxdata.com/api/v2/scripts" api-ref="/influxdb/cloud/api/#operation/PostScripts" >}}
Provide the following in your API request:
INFLUX_API_TOKENJSON object with the following fields:
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"
}
The following example creates a new Flux script that references the params.mybucket parameter and returns the last point from the bucket.
{{% get-shared-text "api/v2.0/api-invokable-scripts/create-flux-script.sh" %}}
Replace the following:
INFLUX_API_TOKEN: your InfluxDB API tokenINFLUX_ORG_ID: your InfluxDB organization IDInfluxDB 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"
}
To invoke a script, send a request using the POST method to the /api/v2/scripts/SCRIPT_ID/invoke InfluxDB API endpoint.
{{< api-endpoint method="post" endpoint="https://cloud2.influxdata.com/api/v2/scripts/SCRIPT_ID" api-ref="/influxdb/cloud/api/#operation/PostScriptsIDInvoke" >}}
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:
INFLUX_API_TOKENJSON 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.
{{% get-shared-text "api/v2.0/api-invokable-scripts/invoke-post.sh" %}}
InfluxDB returns query results in annotated CSV from the air_sensor bucket.
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.groupColumnTo 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.
{{% get-shared-text "api/v2.0/api-invokable-scripts/invoke_with_params.sh" %}}
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
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:
INFLUX_API_TOKEN{{% get-shared-text "api/v2.0/api-invokable-scripts/list.sh" %}}
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.
{{< api-endpoint method="get" endpoint="https://cloud2.influxdata.com/api/v2/scripts/SCRIPT_ID" api-ref="/influxdb/cloud/api/#operation/GetScriptsID" >}}
Replace SCRIPT_ID with the ID of the script you want to find.
Provide the following in your request:
INFLUX_API_TOKEN{{% get-shared-text "api/v2.0/api-invokable-scripts/find.sh" %}}
Use the API to replace the following properties of an invokable 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.
{{< api-endpoint method="patch" endpoint="https://cloud2.influxdata.com/api/v2/scripts/SCRIPT_ID" api-ref="/influxdb/cloud/api/#operation/PatchScriptsID" >}}
Provide the following in your request:
INFLUX_API_TOKENThe following example finds an invokable script containing a numeric date range, replaces the date with a new parameter, and updates the invokable script.
{{% get-shared-text "api/v2.0/api-invokable-scripts/update-flux-script.sh" %}}
GET /api/v2/scripts to retrieve an object that contains a list of scripts.jq to find the first invokable script object that has a script property that contains a hard-coded numeric date range.script property with a new params.myrangestart dynamic parameter and assign the object to a new_script variable.script_id variable.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.
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.
{{< api-endpoint method="delete" endpoint="https://cloud2.influxdata.com/api/v2/scripts/SCRIPT_ID" api-ref="/influxdb/cloud/api/#operation/DeleteScriptsID" >}}
Provide the following in your request:
INFLUX_API_TOKEN