Skip to contentSkip to navigationSkip to topbar
On this page

Function Version


Function Versions are specific versions of JavaScript Node.js code that execute at a particular domain.

The steps to create Functions are as follows:

  1. Create a Function
  2. Create a Function Version (this resource) by making a POST request to https://serverless-upload.twilio.com

You will need the Function Version SID that the create request returns to include this Function in a Build.


Function Version properties

function-version-properties page anchor
Property nameTypeRequiredDescriptionChild properties
sidSID<ZN>Optional
Not PII

The unique string that we created to identify the Function Version resource.

Pattern: ^ZN[0-9a-fA-F]{32}$Min length: 34Max length: 34

account_sidSID<AC>Optional

The SID of the Account that created the Function Version resource.

Pattern: ^AC[0-9a-fA-F]{32}$Min length: 34Max length: 34

service_sidSID<ZS>Optional

The SID of the Service that the Function Version resource is associated with.

Pattern: ^ZS[0-9a-fA-F]{32}$Min length: 34Max length: 34

function_sidSID<ZH>Optional

The SID of the Function resource that is the parent of the Function Version resource.

Pattern: ^ZH[0-9a-fA-F]{32}$Min length: 34Max length: 34

pathstringOptional
PII MTL: 7 days

The URL-friendly string by which the Function Version resource can be referenced. It can be a maximum of 255 characters. All paths begin with a forward slash ('/'). If a Function Version creation request is submitted with a path not containing a leading slash, the path will automatically be prepended with one.


visibilityenum<string>Optional

The access control that determines how the Function Version resource can be accessed. Can be: public, protected, or private.

Possible values:
publicprivateprotected

date_createdstring<date-time>Optional

urlstring<uri>Optional

The absolute URL of the Function Version resource.


linksobject<uri-map>Optional

Create a Function Version resource

create-a-function-version-resource page anchor

A Function Version resource is created by making a POST request to the following, dedicated URL:

https://serverless-upload.twilio.com/v1/Services/{ServiceSid}/Functions/{FunctionSid}/Versions

The following example creates a Function Version resource using the language of your choice (or curl) and an external file, firstfunc.js, which contains the function body.

Upload Function bodyLink to code sample: Upload Function body
1
const fs = require('fs');
2
// Before running this code, install "form-data" and "axios" using `npm install form-data axios`
3
const FormData = require('form-data');
4
const axios = require('axios');
5
6
// Provision API Keys at twilio.com/console/runtime/api-keys
7
// and set the environment variables. See http://twil.io/secure
8
const apiKey = process.env.TWILIO_API_KEY;
9
const apiSecret = process.env.TWILIO_API_SECRET;
10
11
const serviceSid = 'ZSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
12
const functionSid = 'ZHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
13
14
const serviceUrl = `https://serverless-upload.twilio.com/v1/Services/${serviceSid}`;
15
const uploadUrl = `${serviceUrl}/Functions/${functionSid}/Versions`;
16
17
const form = new FormData();
18
form.append('Path', '/thanos');
19
form.append('Visibility', 'public');
20
form.append('Content', fs.createReadStream('firstfunc.js'), {
21
contentType: 'application/javascript',
22
});
23
24
// Create a new Function Version
25
axios
26
.post(uploadUrl, form, {
27
auth: {
28
username: apiKey,
29
password: apiSecret,
30
},
31
headers: form.getHeaders(),
32
})
33
.then((response) => {
34
const newVersionSid = response.data.sid;
35
console.log(newVersionSid);
36
});
(warning)

Warning

Note that the Serverless upload endpoint is on a different subdomain from the rest of the Serverless API (serverless-upload.twilio.com instead of serverless.twilio.com), and is not supported by the Twilio Helper Libraries at this time.

The create action accepts these parameters:

ParameterDescription
ContentThe Function code to upload as a JavaScript file.
FunctionSidThe SID of the Function resource to upload this code to.
PathThe path to assign the Function. Must be URL Friendly, without fragments, and the characters ;,?:@+&$()' " are disallowed.
ServiceSidThe SID of the Function's Service.
VisibilityThe visibility of the Function. Can be public, protected, or private.

Fetch a FunctionVersion resource

fetch-a-functionversion-resource page anchor
GET https://serverless.twilio.com/v1/Services/{ServiceSid}/Functions/{FunctionSid}/Versions/{Sid}

Path parameters

path-parameters page anchor
Property nameTypeRequiredPIIDescription
ServiceSidstringrequired

The SID of the Service to fetch the Function Version resource from.


FunctionSidSID<ZH>required

The SID of the function that is the parent of the Function Version resource to fetch.

Pattern: ^ZH[0-9a-fA-F]{32}$Min length: 34Max length: 34

SidSID<ZN>required

The SID of the Function Version resource to fetch.

Pattern: ^ZN[0-9a-fA-F]{32}$Min length: 34Max length: 34
1
// Download the helper library from https://www.twilio.com/docs/node/install
2
const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";
3
4
// Find your Account SID and Auth Token at twilio.com/console
5
// and set the environment variables. See http://twil.io/secure
6
const accountSid = process.env.TWILIO_ACCOUNT_SID;
7
const authToken = process.env.TWILIO_AUTH_TOKEN;
8
const client = twilio(accountSid, authToken);
9
10
async function fetchFunctionVersion() {
11
const functionVersion = await client.serverless.v1
12
.services("ServiceSid")
13
.functions("ZHaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
14
.functionVersions("ZNaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
15
.fetch();
16
17
console.log(functionVersion.sid);
18
}
19
20
fetchFunctionVersion();

Output

1
{
2
"sid": "ZNaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
3
"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
4
"service_sid": "ServiceSid",
5
"function_sid": "ZHaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
6
"path": "/test-path",
7
"visibility": "public",
8
"date_created": "2018-11-10T20:00:00Z",
9
"url": "https://serverless.twilio.com/v1/Services/ZS00000000000000000000000000000000/Functions/ZH00000000000000000000000000000000/Versions/ZN00000000000000000000000000000000",
10
"links": {
11
"function_version_content": "https://serverless.twilio.com/v1/Services/ZS00000000000000000000000000000000/Functions/ZH00000000000000000000000000000000/Versions/ZN00000000000000000000000000000000/Content"
12
}
13
}

Read multiple FunctionVersion resources

read-multiple-functionversion-resources page anchor
GET https://serverless.twilio.com/v1/Services/{ServiceSid}/Functions/{FunctionSid}/Versions

Property nameTypeRequiredPIIDescription
ServiceSidstringrequired

The SID of the Service to read the Function Version resources from.


FunctionSidSID<ZH>required

The SID of the function that is the parent of the Function Version resources to read.

Pattern: ^ZH[0-9a-fA-F]{32}$Min length: 34Max length: 34
Property nameTypeRequiredPIIDescription
PageSizeintegerOptional

How many resources to return in each list page. The default is 50, and the maximum is 1000.

Minimum: 1Maximum: 1000

PageintegerOptional

The page index. This value is simply for client state.

Minimum: 0

PageTokenstringOptional

The page token. This is provided by the API.

1
// Download the helper library from https://www.twilio.com/docs/node/install
2
const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";
3
4
// Find your Account SID and Auth Token at twilio.com/console
5
// and set the environment variables. See http://twil.io/secure
6
const accountSid = process.env.TWILIO_ACCOUNT_SID;
7
const authToken = process.env.TWILIO_AUTH_TOKEN;
8
const client = twilio(accountSid, authToken);
9
10
async function listFunctionVersion() {
11
const functionVersions = await client.serverless.v1
12
.services("ServiceSid")
13
.functions("ZHaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
14
.functionVersions.list({ limit: 20 });
15
16
functionVersions.forEach((f) => console.log(f.sid));
17
}
18
19
listFunctionVersion();

Output

1
{
2
"function_versions": [],
3
"meta": {
4
"first_page_url": "https://serverless.twilio.com/v1/Services/ZS00000000000000000000000000000000/Functions/ZH00000000000000000000000000000000/Versions?PageSize=50&Page=0",
5
"key": "function_versions",
6
"next_page_url": null,
7
"page": 0,
8
"page_size": 50,
9
"previous_page_url": null,
10
"url": "https://serverless.twilio.com/v1/Services/ZS00000000000000000000000000000000/Functions/ZH00000000000000000000000000000000/Versions?PageSize=50&Page=0"
11
}
12
}
(information)

Info

There is no API endpoint for deleting a Function Version, only Functions. Function Versions are automatically purged if they are not used by a Build for 30 days. See our retention policy to learn more.

Need some help?

Terms of service

Copyright © 2024 Twilio Inc.