Quickstart
In addition to the Console UI and the Serverless Toolkit, you have the option of directly using the Serverless API to create and manage your Services, Functions, and Assets. This empowers you to create custom build and deployment automations to suit your specific needs.
Warning
Functions and Assets created via Functions(Classic) and Assets(Classic) are not available via the API. Similarly, Functions and Assets created via the API aren't available in the Functions(Classic) and Assets(Classic) interface.
Get an overview of the API and the Serverless Toolkit by watching our overview video from Twilio Signal 2019:
Great. Let's have a look at how to deploy a single Function via the API.
First, we'll create a Service, which serves as our container for the environments we set up.
Warning
The uniqueName will form part of your domain name, so choose carefully.
_14// Download the helper library from https://www.twilio.com/docs/node/install
_14// Find your Account SID and Auth Token at twilio.com/console
_14// and set the environment variables. See http://twil.io/secure
_14const accountSid = process.env.TWILIO_ACCOUNT_SID;
_14const authToken = process.env.TWILIO_AUTH_TOKEN;
_14const client = require('twilio')(accountSid, authToken);
_14client.serverless.v1.services
_14 includeCredentials: true,
_14 friendlyName: 'testing'
_14 .then(service => console.log(service.sid));
_18 "sid": "ZS00000000000000000000000000000000",
_18 "account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_18 "friendly_name": "testing",
_18 "unique_name": "demo",
_18 "include_credentials": true,
_18 "ui_editable": false,
_18 "domain_base": "service-unique-1234",
_18 "date_created": "2018-11-10T20:00:00Z",
_18 "date_updated": "2018-11-10T20:00:00Z",
_18 "url": "https://serverless.twilio.com/v1/Services/ZS00000000000000000000000000000000",
_18 "environments": "https://serverless.twilio.com/v1/Services/ZS00000000000000000000000000000000/Environments",
_18 "functions": "https://serverless.twilio.com/v1/Services/ZS00000000000000000000000000000000/Functions",
_18 "assets": "https://serverless.twilio.com/v1/Services/ZS00000000000000000000000000000000/Assets",
_18 "builds": "https://serverless.twilio.com/v1/Services/ZS00000000000000000000000000000000/Builds"
We'll get back a Service SID, in the format ZSxxx
.
Next, we'll use that Service SID to create a "dev" Environment.
_14// Download the helper library from https://www.twilio.com/docs/node/install
_14// Find your Account SID and Auth Token at twilio.com/console
_14// and set the environment variables. See http://twil.io/secure
_14const accountSid = process.env.TWILIO_ACCOUNT_SID;
_14const authToken = process.env.TWILIO_AUTH_TOKEN;
_14const client = require('twilio')(accountSid, authToken);
_14client.serverless.v1.services('ZSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
_14 uniqueName: 'dev-environment'
_14 .then(environment => console.log(environment.sid));
_18 "sid": "ZE00000000000000000000000000000000",
_18 "account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_18 "service_sid": "ZSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_18 "unique_name": "dev-environment",
_18 "domain_suffix": "dev",
_18 "domain_name": "foobar-1234-stage.twil.io",
_18 "custom_domain_name": null,
_18 "date_created": "2018-11-10T20:00:00Z",
_18 "date_updated": "2018-11-10T20:00:00Z",
_18 "url": "https://serverless.twilio.com/v1/Services/ZSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Environments/ZE00000000000000000000000000000000",
_18 "variables": "https://serverless.twilio.com/v1/Services/ZSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Environments/ZE00000000000000000000000000000000/Variables",
_18 "deployments": "https://serverless.twilio.com/v1/Services/ZSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Environments/ZE00000000000000000000000000000000/Deployments",
_18 "logs": "https://serverless.twilio.com/v1/Services/ZSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Environments/ZE00000000000000000000000000000000/Logs"
This will give us an empty environment, e.g. demo-X4HX-dev.twil.io
. To see the domain name for your environment, you can fetch your environment using the ZExxx
SID output from the prior code sample.
_11// Download the helper library from https://www.twilio.com/docs/node/install
_11// Find your Account SID and Auth Token at twilio.com/console
_11// and set the environment variables. See http://twil.io/secure
_11const accountSid = process.env.TWILIO_ACCOUNT_SID;
_11const authToken = process.env.TWILIO_AUTH_TOKEN;
_11const client = require('twilio')(accountSid, authToken);
_11client.serverless.v1.services('ZSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
_11 .environments('ZEXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
_11 .then(environment => console.log(environment.domainName));
_18 "sid": "ZEXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_18 "account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_18 "service_sid": "ZSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_18 "build_sid": "ZB00000000000000000000000000000000",
_18 "unique_name": "testing-environment",
_18 "domain_suffix": "testing",
_18 "domain_name": "foobar-1234-testing.twil.io",
_18 "custom_domain_name": null,
_18 "date_created": "2018-11-10T20:00:00Z",
_18 "date_updated": "2018-11-10T20:00:00Z",
_18 "url": "https://serverless.twilio.com/v1/Services/ZSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Environments/ZE00000000000000000000000000000000",
_18 "variables": "https://serverless.twilio.com/v1/Services/ZSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Environments/ZE00000000000000000000000000000000/Variables",
_18 "deployments": "https://serverless.twilio.com/v1/Services/ZSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Environments/ZE00000000000000000000000000000000/Deployments",
_18 "logs": "https://serverless.twilio.com/v1/Services/ZSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Environments/ZE00000000000000000000000000000000/Logs"
Now let's create our Function. We create the Function at the service level, not inside a particular environment (we'll deploy our Function to an environment later on). Also, we only set the name for the Function at this step. We'll choose a path, visibility, and upload the code for the Function in later steps.
_11// Download the helper library from https://www.twilio.com/docs/node/install
_11// Find your Account SID and Auth Token at twilio.com/console
_11// and set the environment variables. See http://twil.io/secure
_11const accountSid = process.env.TWILIO_ACCOUNT_SID;
_11const authToken = process.env.TWILIO_AUTH_TOKEN;
_11const client = require('twilio')(accountSid, authToken);
_11client.serverless.v1.services('ZSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
_11 .create({friendlyName: 'firstfunc'})
_11 .then(function_ => console.log(function_.sid));
_12 "sid": "ZH00000000000000000000000000000000",
_12 "account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_12 "service_sid": "ZSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_12 "friendly_name": "firstfunc",
_12 "date_created": "2018-11-10T20:00:00Z",
_12 "date_updated": "2018-11-10T20:00:00Z",
_12 "url": "https://serverless.twilio.com/v1/Services/ZSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Functions/ZH00000000000000000000000000000000",
_12 "function_versions": "https://serverless.twilio.com/v1/Services/ZSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Functions/ZH00000000000000000000000000000000/Versions"
You will get back a Function SID in the format ZHxxx
. Save this Function SID for the next step, where we'll create a Version for this Function.
With a Function created, we're ready to create our first Version. Versions of Functions or Assets are where you add the path, visibility (public, protected or private), and the file content itself.
First, we need some code upload. Let's write a simple Function that decides the fate of the universe:
_10exports.handler = (context, event, callback) => {
_10 const sparedByThanos = Math.random() > 0.5;
_10 quote: 'You should have gone for the head.'
Now we have a Function worth uploading. Save this as a file on your computer named firstfunc.js
.
Now we can upload that file. The create API action is not currently represented in the helper libraries, so you have to do a manual file upload in the language of your choice. See an example below using Node.js.
Remember to replace ZSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
with your Service SID, and ZHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
with the SID of the newly created Function (from this step).
_36const fs = require('fs');
_36// Before running this code, install "form-data" and "axios" using `npm install form-data axios`
_36const FormData = require('form-data');
_36const axios = require('axios');
_36// Provision API Keys at twilio.com/console/runtime/api-keys
_36// and set the environment variables. See http://twil.io/secure
_36const apiKey = process.env.TWILIO_API_KEY;
_36const apiSecret = process.env.TWILIO_API_SECRET;
_36const serviceSid = 'ZSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
_36const functionSid = 'ZHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
_36const serviceUrl = `https://serverless-upload.twilio.com/v1/Services/${serviceSid}`;
_36const uploadUrl = `${serviceUrl}/Functions/${functionSid}/Versions`;
_36const form = new FormData();
_36form.append('Path', '/thanos');
_36form.append('Visibility', 'public');
_36form.append('Content', fs.createReadStream('firstfunc.js'), {
_36 contentType: 'application/javascript',
_36// Create a new Function Version
_36 .post(uploadUrl, form, {
_36 headers: form.getHeaders(),
_36 .then((response) => {
_36 const newVersionSid = response.data.sid;
_36 console.log(newVersionSid);
With the upload complete, you will see a SID logged to your terminal. Save this Version SID for the next step.
Now, we need to create a Build. A Build will compile Function and Asset Versions into a single, deployable package. This build will live in the Twilio cloud, so you just need the Build SID to reference it — there are no files to keep track of.
_13// Download the helper library from https://www.twilio.com/docs/node/install
_13// Find your Account SID and Auth Token at twilio.com/console
_13// and set the environment variables. See http://twil.io/secure
_13const accountSid = process.env.TWILIO_ACCOUNT_SID;
_13const authToken = process.env.TWILIO_AUTH_TOKEN;
_13const client = require('twilio')(accountSid, authToken);
_13client.serverless.v1.services('ZSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
_13 functionVersions: ['ZNXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX']
_13 .then(build => console.log(build.sid));
_45 "sid": "ZB00000000000000000000000000000000",
_45 "account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_45 "service_sid": "ZSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_45 "sid": "ZN00000000000000000000000000000000",
_45 "account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_45 "service_sid": "ZS00000000000000000000000000000000",
_45 "asset_sid": "ZH00000000000000000000000000000000",
_45 "date_created": "2018-11-10T20:00:00Z",
_45 "path": "/asset-path",
_45 "visibility": "PUBLIC"
_45 "function_versions": [
_45 "sid": "ZN00000000000000000000000000000001",
_45 "account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_45 "service_sid": "ZS00000000000000000000000000000000",
_45 "function_sid": "ZH00000000000000000000000000000001",
_45 "date_created": "2018-11-10T20:00:00Z",
_45 "path": "/function-path",
_45 "visibility": "PUBLIC"
_45 "name": "@twilio/runtime-handler",
_45 "status": "building",
_45 "date_created": "2018-11-10T20:00:00Z",
_45 "date_updated": "2018-11-10T20:00:00Z",
_45 "url": "https://serverless.twilio.com/v1/Services/ZSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Builds/ZB00000000000000000000000000000000",
_45 "build_status": "https://serverless.twilio.com/v1/Services/ZSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Builds/ZB00000000000000000000000000000000/Status"
You'll receive a result straight away, but note the status
property. We need to wait until the status returns completed
before continuing. The best way to handle the status changes is to poll every second or two. Here is some sample code to get the current status
of a Build:
_11// Download the helper library from https://www.twilio.com/docs/node/install
_11// Find your Account SID and Auth Token at twilio.com/console
_11// and set the environment variables. See http://twil.io/secure
_11const accountSid = process.env.TWILIO_ACCOUNT_SID;
_11const authToken = process.env.TWILIO_AUTH_TOKEN;
_11const client = require('twilio')(accountSid, authToken);
_11client.serverless.v1.services('ZSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
_11 .builds('ZBXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
_11 .then(build => console.log(build.status));
_45 "sid": "ZBXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_45 "account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_45 "service_sid": "ZSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_45 "sid": "ZN00000000000000000000000000000000",
_45 "account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_45 "service_sid": "ZS00000000000000000000000000000000",
_45 "asset_sid": "ZH00000000000000000000000000000000",
_45 "date_created": "2018-11-10T20:00:00Z",
_45 "path": "/asset-path",
_45 "visibility": "PUBLIC"
_45 "function_versions": [
_45 "sid": "ZN00000000000000000000000000000001",
_45 "account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_45 "service_sid": "ZS00000000000000000000000000000000",
_45 "function_sid": "ZH00000000000000000000000000000001",
_45 "date_created": "2018-11-10T20:00:00Z",
_45 "path": "/function-path",
_45 "visibility": "PUBLIC"
_45 "name": "@twilio/runtime-handler",
_45 "status": "building",
_45 "date_created": "2018-11-10T20:00:00Z",
_45 "date_updated": "2018-11-10T20:00:00Z",
_45 "url": "https://serverless.twilio.com/v1/Services/ZSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Builds/ZB00000000000000000000000000000000",
_45 "build_status": "https://serverless.twilio.com/v1/Services/ZSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Builds/ZB00000000000000000000000000000000/Status"
When the build is completed
, we're ready to deploy!
For the final step, you must associate the Build we just created with the Environment we created earlier in this tutorial. (Remember the Environment SID!) This is called a Deployment.
_12// Download the helper library from https://www.twilio.com/docs/node/install
_12// Find your Account SID and Auth Token at twilio.com/console
_12// and set the environment variables. See http://twil.io/secure
_12const accountSid = process.env.TWILIO_ACCOUNT_SID;
_12const authToken = process.env.TWILIO_AUTH_TOKEN;
_12const client = require('twilio')(accountSid, authToken);
_12client.serverless.v1.services('ZSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
_12 .environments('ZEXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
_12 .then(deployment => console.log(deployment.sid));
_10 "sid": "ZD00000000000000000000000000000000",
_10 "account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_10 "service_sid": "ZSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_10 "environment_sid": "ZEXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_10 "build_sid": "ZB00000000000000000000000000000000",
_10 "date_created": "2018-11-10T20:00:00Z",
_10 "date_updated": "2018-11-10T20:00:00Z",
_10 "url": "https://serverless.twilio.com/v1/Services/ZSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Environments/ZE00000000000000000000000000000000/Deployments/ZD00000000000000000000000000000000"
Your Function will now be live and accessible via URL! You can test it out by going to its URL, such as https://demo-X4HX-dev.twil.io/thanos
(be sure to replace demo-X4HX-dev.twil.io
with the unique domain name for your Environment, which you fetched earlier).
When creating a build, you can also specify any dependencies that your code may have.
To do so, include the dependencies
property, which must be a JSON stringified array of package name
s and version
s.
_14// Download the helper library from https://www.twilio.com/docs/node/install
_14// Find your Account SID and Auth Token at twilio.com/console
_14// and set the environment variables. See http://twil.io/secure
_14const accountSid = process.env.TWILIO_ACCOUNT_SID;
_14const authToken = process.env.TWILIO_AUTH_TOKEN;
_14const client = require('twilio')(accountSid, authToken);
_14client.serverless.v1.services('ZSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
_14 functionVersions: ['ZNXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'],
_14 dependencies: `[{"name": "randomcolor", "version": "0.5.4"}]`
_14 .then(build => console.log(build.sid));
_45 "sid": "ZB00000000000000000000000000000000",
_45 "account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_45 "service_sid": "ZSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_45 "sid": "ZN00000000000000000000000000000000",
_45 "account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_45 "service_sid": "ZS00000000000000000000000000000000",
_45 "asset_sid": "ZH00000000000000000000000000000000",
_45 "date_created": "2018-11-10T20:00:00Z",
_45 "path": "/asset-path",
_45 "visibility": "PUBLIC"
_45 "function_versions": [
_45 "sid": "ZN00000000000000000000000000000001",
_45 "account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_45 "service_sid": "ZS00000000000000000000000000000000",
_45 "function_sid": "ZH00000000000000000000000000000001",
_45 "date_created": "2018-11-10T20:00:00Z",
_45 "path": "/function-path",
_45 "visibility": "PUBLIC"
_45 "name": "@twilio/runtime-handler",
_45 "status": "building",
_45 "date_created": "2018-11-10T20:00:00Z",
_45 "date_updated": "2018-11-10T20:00:00Z",
_45 "url": "https://serverless.twilio.com/v1/Services/ZSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Builds/ZB00000000000000000000000000000000",
_45 "build_status": "https://serverless.twilio.com/v1/Services/ZSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Builds/ZB00000000000000000000000000000000/Status"
The pattern we used above for uploading Functions is exactly the same as it for Assets.
-
Create an
Asset
-
Create the
Asset Version
via a
POST
request to the
serverless-uploads.twilio.com
domain
-
Include the
AssetVersion
SID(s) we want to be a part of the Build, alongside any
functionVersions
and dependencies we might need.
_14// Download the helper library from https://www.twilio.com/docs/node/install
_14// Find your Account SID and Auth Token at twilio.com/console
_14// and set the environment variables. See http://twil.io/secure
_14const accountSid = process.env.TWILIO_ACCOUNT_SID;
_14const authToken = process.env.TWILIO_AUTH_TOKEN;
_14const client = require('twilio')(accountSid, authToken);
_14client.serverless.v1.services('ZSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
_14 functionVersions: ['ZNXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'],
_14 assetVersions: ['ZNYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY']
_14 .then(build => console.log(build.sid));
_45 "sid": "ZB00000000000000000000000000000000",
_45 "account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_45 "service_sid": "ZSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_45 "sid": "ZN00000000000000000000000000000000",
_45 "account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_45 "service_sid": "ZS00000000000000000000000000000000",
_45 "asset_sid": "ZH00000000000000000000000000000000",
_45 "date_created": "2018-11-10T20:00:00Z",
_45 "path": "/asset-path",
_45 "visibility": "PUBLIC"
_45 "function_versions": [
_45 "sid": "ZN00000000000000000000000000000001",
_45 "account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_45 "service_sid": "ZS00000000000000000000000000000000",
_45 "function_sid": "ZH00000000000000000000000000000001",
_45 "date_created": "2018-11-10T20:00:00Z",
_45 "path": "/function-path",
_45 "visibility": "PUBLIC"
_45 "name": "@twilio/runtime-handler",
_45 "status": "building",
_45 "date_created": "2018-11-10T20:00:00Z",
_45 "date_updated": "2018-11-10T20:00:00Z",
_45 "url": "https://serverless.twilio.com/v1/Services/ZSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Builds/ZB00000000000000000000000000000000",
_45 "build_status": "https://serverless.twilio.com/v1/Services/ZSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Builds/ZB00000000000000000000000000000000/Status"
Definitely take a look at the API reference section for all of the API resources below. You might be particularly interested in Variables to allow setting Environment Variables for a given Environment.