Skip to contentSkip to navigationSkip to topbar
Rate this page:
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 nameTypePIIDescription
sidSID<ZN>
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>

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>

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>

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

pathstring
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>

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

Possible values:
publicprivateprotected

urlstring<uri>

The absolute URL of the Function Version resource.


linksobject<uri-map>

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 body

upload-function-body page anchor
Node.js
Python
Java
PHP
Ruby
curl

_36
const fs = require('fs');
_36
// Before running this code, install "form-data" and "axios" using `npm install form-data axios`
_36
const FormData = require('form-data');
_36
const axios = require('axios');
_36
_36
// Provision API Keys at twilio.com/console/runtime/api-keys
_36
// and set the environment variables. See http://twil.io/secure
_36
const apiKey = process.env.TWILIO_API_KEY;
_36
const apiSecret = process.env.TWILIO_API_SECRET;
_36
_36
const serviceSid = 'ZSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
_36
const functionSid = 'ZHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
_36
_36
const serviceUrl = `https://serverless-upload.twilio.com/v1/Services/${serviceSid}`;
_36
const uploadUrl = `${serviceUrl}/Functions/${functionSid}/Versions`;
_36
_36
const form = new FormData();
_36
form.append('Path', '/thanos');
_36
form.append('Visibility', 'public');
_36
form.append('Content', fs.createReadStream('firstfunc.js'), {
_36
contentType: 'application/javascript',
_36
});
_36
_36
// Create a new Function Version
_36
axios
_36
.post(uploadUrl, form, {
_36
auth: {
_36
username: apiKey,
_36
password: apiSecret,
_36
},
_36
headers: form.getHeaders(),
_36
})
_36
.then((response) => {
_36
const newVersionSid = response.data.sid;
_36
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

Fetch a Function Version resource

fetch-a-function-version-resource page anchor
Node.js
Python
C#
Java
Go
PHP
Ruby
twilio-cli
curl

_20
// Download the helper library from https://www.twilio.com/docs/node/install
_20
const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";
_20
_20
// Find your Account SID and Auth Token at twilio.com/console
_20
// and set the environment variables. See http://twil.io/secure
_20
const accountSid = process.env.TWILIO_ACCOUNT_SID;
_20
const authToken = process.env.TWILIO_AUTH_TOKEN;
_20
const client = twilio(accountSid, authToken);
_20
_20
async function fetchFunctionVersion() {
_20
const functionVersion = await client.serverless.v1
_20
.services("ServiceSid")
_20
.functions("ZHaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
_20
.functionVersions("ZNaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
_20
.fetch();
_20
_20
console.log(functionVersion.sid);
_20
}
_20
_20
fetchFunctionVersion();

Output

_13
{
_13
"sid": "ZNaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_13
"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_13
"service_sid": "ServiceSid",
_13
"function_sid": "ZHaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_13
"path": "/test-path",
_13
"visibility": "public",
_13
"date_created": "2018-11-10T20:00:00Z",
_13
"url": "https://serverless.twilio.com/v1/Services/ZS00000000000000000000000000000000/Functions/ZH00000000000000000000000000000000/Versions/ZN00000000000000000000000000000000",
_13
"links": {
_13
"function_version_content": "https://serverless.twilio.com/v1/Services/ZS00000000000000000000000000000000/Functions/ZH00000000000000000000000000000000/Versions/ZN00000000000000000000000000000000/Content"
_13
}
_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.

Read multiple Function Version resources

read-multiple-function-version-resources page anchor
Node.js
Python
C#
Java
Go
PHP
Ruby
twilio-cli
curl

_19
// Download the helper library from https://www.twilio.com/docs/node/install
_19
const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";
_19
_19
// Find your Account SID and Auth Token at twilio.com/console
_19
// and set the environment variables. See http://twil.io/secure
_19
const accountSid = process.env.TWILIO_ACCOUNT_SID;
_19
const authToken = process.env.TWILIO_AUTH_TOKEN;
_19
const client = twilio(accountSid, authToken);
_19
_19
async function listFunctionVersion() {
_19
const functionVersions = await client.serverless.v1
_19
.services("ServiceSid")
_19
.functions("ZHaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
_19
.functionVersions.list({ limit: 20 });
_19
_19
functionVersions.forEach((f) => console.log(f.sid));
_19
}
_19
_19
listFunctionVersion();

Output

_12
{
_12
"function_versions": [],
_12
"meta": {
_12
"first_page_url": "https://serverless.twilio.com/v1/Services/ZS00000000000000000000000000000000/Functions/ZH00000000000000000000000000000000/Versions?PageSize=50&Page=0",
_12
"key": "function_versions",
_12
"next_page_url": null,
_12
"page": 0,
_12
"page_size": 50,
_12
"previous_page_url": null,
_12
"url": "https://serverless.twilio.com/v1/Services/ZS00000000000000000000000000000000/Functions/ZH00000000000000000000000000000000/Versions?PageSize=50&Page=0"
_12
}
_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.


Rate this page: