Adding and Deploying Stages

A stage is a piece of reusable code that performs specific capabilities for an API. You can apply various stages to an operation. For more details about supported stages, see Stage Reference.

In the Petstore sample, a POST/user/createWithArray API is provided with which you can create a number of users. However, this API does not limit the number of users that can be created. If a large number of users are created at one time, the server might crash. In this situation, you can use the Transformation stage to limit the number of users that can be created in one request per second.

Prerequisite:

Creating a Microflow® Application

To add stages, complete these steps:

  1. From the left Operations list, click the POST/user/createWithArray operation.
  2. From the request pipeline, click the plus button and click .
  3. On the Throttling page, set TPS to 1. Click Add to add the Throttling stage.
  4. From the request pipeline, click the plus button and click .
  5. On the Transform request page, enter a stage name and description, and then enter the following JavaScript code in the JavaScript payload transformation section. Click Add.

     var _ = require('lodash');
     if(payload&&payload.length>=4){
     		var newPayload = _.slice(payload, 0, 3);
         payload = newPayload;
     }
     done(payload);
    
  6. From the response pipeline, click the plus button and click .
  7. On the Transform response page, enter a stage name and description, and then enter the following JavaScript code in the Javascript Transformation section. Click Add.

     var _ = require( 'lodash' );
     if ( !body ) {
       body = {
         "message": "If you pass more than 3 users, only the first 3 users are added, other users will be dropped."
       };
     } else if ( _.isObject( body ) ) {
       body.message = "If you pass more than 3 users, only the first 3 users are added, other users will be dropped.";
     }
     done(body);
    
  8. Click Push updates to deploy the configured stages to the API. The pushing process might take several minutes.

The application color turns to green when the Microflow application is successfully pushed and run.

What to Do Next

Testing Deployed Stages