メニュー

Expand
ページを評価:

クイックスタート

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.

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:

Cool, I really do want to learn this underlying API!

Great. Let's have a look at how to deploy a single Function via the API.

Deploying a single Function

Serviceを作成する

First, we'll create a Service, which serves as our container for the environments we set up.

The uniqueName will form part of your domain name, so choose carefully.

        
        
        

        We'll get back a Service SID, in the format ZSxxx.

        OK, I have the SID, what's next?

        Create an Environment

        Next, we'll use that Service SID to create a "dev" Environment.

              
              
              

              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.

                    
                    
                    
                    OK, got an Environment. Now what?

                    Create a Function

                    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.

                          
                          
                          

                          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.

                          Done. Let's keep going.

                          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:

                          exports.handler = (context, event, callback) => {
                            const sparedByThanos = Math.random() > 0.5;
                          
                            callback(null, {
                              sparedByThanos,
                              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.

                          That's ominous. OK, what now?

                          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).

                                
                                
                                

                                With the upload complete, you will see a SID logged to your terminal. Save this Version SID for the next step.

                                This is a lot of SIDs. OK, next...

                                Create a Build

                                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.

                                      
                                      
                                      

                                      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:

                                            
                                            
                                            

                                            When the build is completed, we're ready to deploy!

                                            Yes! I'm ready to deploy.

                                            Create a Deployment

                                            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.

                                                  
                                                  
                                                  

                                                  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).

                                                  That's great! Anything else I should know?

                                                  Adding dependencies

                                                  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 names and versions.

                                                        
                                                        
                                                        

                                                        Handling Asset uploads

                                                        The pattern we used above for uploading Functions is exactly the same as it for Assets.

                                                        1. Create an Asset
                                                        2. Create the Asset Version via a POST request to the serverless-uploads.twilio.com domain
                                                        3. Include the AssetVersion SID(s) we want to be a part of the Build, alongside any functionVersions and dependencies we might need.
                                                              
                                                              
                                                              

                                                              関連トピック

                                                              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.

                                                              ページを評価:

                                                              ヘルプが必要ですか?

                                                              We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd by visiting Twilio's Stack Overflow Collective or browsing the Twilio tag on Stack Overflow.

                                                                    
                                                                    
                                                                    

                                                                    フィードバックくださりありがとうございます!

                                                                    Please select the reason(s) for your feedback. The additional information you provide helps us improve our documentation:

                                                                    Sending your feedback...
                                                                    🎉 Thank you for your feedback!
                                                                    Something went wrong. Please try again.

                                                                    Thanks for your feedback!

                                                                    Refer us and get $10 in 3 simple steps!

                                                                    ステップ1

                                                                    Get link

                                                                    Get a free personal referral link here

                                                                    ステップ2:

                                                                    Give $10

                                                                    Your user signs up and upgrade using link

                                                                    ステップ3

                                                                    Get $10

                                                                    1,250 free SMSes
                                                                    OR 1,000 free voice mins
                                                                    OR 12,000 chats
                                                                    OR more