Function Versions are specific versions of JavaScript Node.js code that execute at a particular domain.
The steps to create Functions are as follows:
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.
The unique string that we created to identify the Function Version resource.
^ZN[0-9a-fA-F]{32}$
Min length: 34
Max length: 34
The SID of the Account that created the Function Version resource.
^AC[0-9a-fA-F]{32}$
Min length: 34
Max length: 34
The SID of the Service that the Function Version resource is associated with.
^ZS[0-9a-fA-F]{32}$
Min length: 34
Max length: 34
The SID of the Function resource that is the parent of the Function Version resource.
^ZH[0-9a-fA-F]{32}$
Min length: 34
Max length: 34
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.
The access control that determines how the Function Version resource can be accessed. Can be: public
, protected
, or private
.
public
private
protected
The date and time in GMT when the Function Version resource was created specified in ISO 8601 format.
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.
1const fs = require('fs');2// Before running this code, install "form-data" and "axios" using `npm install form-data axios`3const FormData = require('form-data');4const axios = require('axios');56// Provision API Keys at twilio.com/console/runtime/api-keys7// and set the environment variables. See http://twil.io/secure8const apiKey = process.env.TWILIO_API_KEY;9const apiSecret = process.env.TWILIO_API_SECRET;1011const serviceSid = 'ZSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';12const functionSid = 'ZHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';1314const serviceUrl = `https://serverless-upload.twilio.com/v1/Services/${serviceSid}`;15const uploadUrl = `${serviceUrl}/Functions/${functionSid}/Versions`;1617const form = new FormData();18form.append('Path', '/thanos');19form.append('Visibility', 'public');20form.append('Content', fs.createReadStream('firstfunc.js'), {21contentType: 'application/javascript',22});2324// Create a new Function Version25axios26.post(uploadUrl, form, {27auth: {28username: apiKey,29password: apiSecret,30},31headers: form.getHeaders(),32})33.then((response) => {34const newVersionSid = response.data.sid;35console.log(newVersionSid);36});
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:
Parameter | Description |
---|---|
Content | The Function code to upload as a JavaScript file. |
FunctionSid | The SID of the Function resource to upload this code to. |
Path | The path to assign the Function. Must be URL Friendly, without fragments, and the characters ;,?:@+&$()' " are disallowed. |
ServiceSid | The SID of the Function's Service. |
Visibility | The visibility of the Function. Can be public , protected , or private . |
GET https://serverless.twilio.com/v1/Services/{ServiceSid}/Functions/{FunctionSid}/Versions/{Sid}
The SID of the function that is the parent of the Function Version resource to fetch.
^ZH[0-9a-fA-F]{32}$
Min length: 34
Max length: 34
The SID of the Function Version resource to fetch.
^ZN[0-9a-fA-F]{32}$
Min length: 34
Max length: 34
1// Download the helper library from https://www.twilio.com/docs/node/install2const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";34// Find your Account SID and Auth Token at twilio.com/console5// and set the environment variables. See http://twil.io/secure6const accountSid = process.env.TWILIO_ACCOUNT_SID;7const authToken = process.env.TWILIO_AUTH_TOKEN;8const client = twilio(accountSid, authToken);910async function fetchFunctionVersion() {11const functionVersion = await client.serverless.v112.services("ServiceSid")13.functions("ZHaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")14.functionVersions("ZNaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")15.fetch();1617console.log(functionVersion.sid);18}1920fetchFunctionVersion();
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}
GET https://serverless.twilio.com/v1/Services/{ServiceSid}/Functions/{FunctionSid}/Versions
The SID of the function that is the parent of the Function Version resources to read.
^ZH[0-9a-fA-F]{32}$
Min length: 34
Max length: 34
How many resources to return in each list page. The default is 50, and the maximum is 1000.
1
Maximum: 1000
The page token. This is provided by the API.
1// Download the helper library from https://www.twilio.com/docs/node/install2const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";34// Find your Account SID and Auth Token at twilio.com/console5// and set the environment variables. See http://twil.io/secure6const accountSid = process.env.TWILIO_ACCOUNT_SID;7const authToken = process.env.TWILIO_AUTH_TOKEN;8const client = twilio(accountSid, authToken);910async function listFunctionVersion() {11const functionVersions = await client.serverless.v112.services("ServiceSid")13.functions("ZHaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")14.functionVersions.list({ limit: 20 });1516functionVersions.forEach((f) => console.log(f.sid));17}1819listFunctionVersion();
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}
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.