Skip to Content

Create simple CAP Service with Node.js using the SAP Business Application Studio

Use SAP Business Application Studio to develop a basic CAP Node.js service for Cloud Foundry. You will use only simple JavaScript functions (no database) and learn some basics from the SAP Cloud Application Programming Models (CAP).
You will learn
  • How to create a Dev Space in SAP Business Application Studio
  • How to create a Project using the CAP Project template
  • How to develop a sample service using CAP and Node.js
  • How to run your service locally
tj-66Thomas JentschMarch 15, 2023
Created by
March 15, 2023



Find more information on SAP Cloud Application Programming Model Welcome to CAP.

In this tutorial group you will learn all the steps to finally consume a CAP Service in a process of SAP Build Process Automation.


The Use Case

In this series of tutorials, You will implement a CAP Service with very easy functions like converting a String to a number, converting a list of ids to a comma separated String and finally consume the CAP Service API functions in SAP Build Process Automation Process.

  • Step 1
    1. Open SAP Business Application Studio.

    2. Choose Create Dev Space.

      Create DEV Space
    3. Use name demo for your dev space and select kind Full-Stack Cloud Application Using Productivity Tools.


      By selecting Full-Stack Cloud Application Using Productivity Tools, your dev space comes with several extensions out-of-the-box that you need to develop CAP services.

    4. Choose Create Dev Space.


      The creation of the dev space takes a while. You see that the status for your dev space will change from STARTING to RUNNING.

    5. Once the dev space is running, click the dev space name to open it.


      Opening the dev space and starting the Application Studio takes a while.

    What is a dev space?

    Log in to complete tutorial
  • Step 2
    1. We will not create a project using the wizard, choose Cancel.

      create project
    2. Close the Home tab, choose X.

      close hometab
    3. Use the File menu to create a New Project from Template.

      file menu
    4. Choose CAP Project from the templates and choose Start.

      cap project
    5. Enter the CAP Project Details, use name sap-build-cap-sample-library, select MTA based... and choose Finish.

      Runtime Node.js is already selected.

    6. Close the Home and Guided Development tabs, we are not using them.

    7. Change the view from Project Explorer to Explorer, choose View > Explorer.


      The Explorer view will show all files of your project.

    Log in to complete tutorial
  • Step 3
    1. In the Explorer view you will find the file mta.yaml in the root folder of your project sap-build-cap-sample-library.

    2. Check the file in a text editor, right-click > Open With… > Text Editor default.

    3. In the srv folder, create a new file called service.cds.

    4. In the same folder, create another new file called service.js.

    5. Select the file service.cds and copy the following code into the editor.

      Contains the definition of data types and function headers. Functions and actions will make the API of your service.

      Simple functions like converting a String to an Integer, a list to a CSV String or getting the list of Todos.

      @Capabilities.BatchSupported : false
      service sap_build_cap_sample_library @(path : '/api/v1') {
      define type DataString {
          value : String;
      define type DataInteger {
          value : Integer;
      define type DataNumber {
          value : Double;
      define type DataList {
          id : Integer;
          title: String;
          userId: Integer;
          completed: Boolean;
      define type DataListArray {
          responseArray : array of DataList;
      @Core.Description : 'toInteger'
      function toInteger(value : String) returns DataInteger;
      @Core.Description : 'toNumber'
      function toNumber(value : String) returns DataNumber;
      @Core.Description : 'toString'
      function toStr(value : Double) returns DataString;
      @Core.Description : 'addQuotes'
      function addQuotes(value : String) returns DataString;
      @Core.Description : 'listToString'
      action listToString(responseArray : array of DataList, field : String) returns DataString;
      @Core.Description : 'get list of dodos'
      function getListOfTodos() returns DataListArray;
    6. Select the file service.js and copy the following code into the editor.

      The JavaScript implementation for the API of your service.

      module.exports = srv => {
          srv.on('toInteger', req => {
              const {value} =;
              return { 'value' : parseInt(value) };
          srv.on('toNumber', req => {
              const {value} =;
              return { 'value' : parseFloat(value) };
          srv.on('toStr', req => {
              const {value} =;
              return { 'value' : value.toString() };
          srv.on('addQuotes', req => {
              const {value} =;
              return { 'value' : "'" + value + "'"};
          srv.on('listToString', req => {
              var values =;
              var resultList = [];
              var field =;
              if (values) {
                  for (var i = 0; i < values.length; i++) {
              return { value : resultList.toString() };
          srv.on('getListOfTodos', req => {
              return { responseArray : [            {
                  "id": 1,
                  "completed": false,
                  "title": "delectus aut autem",
                  "userId": 1
                  "id": 2,
                  "completed": false,
                  "title": "quis ut nam facilis et officia qui",
                  "userId": 1
              }] }; 

    What is the Explorer?

    Log in to complete tutorial
  • Step 4

    This step describes how to test the service locally using a file that contains the Service Endpoints.

    1. On the sap-build-cap-sample-library folder, create a new file called test.http.

    2. Select the file test.http and copy the following content into the editor.

      Samples for HTTP requests for the services of your API.

      ### call toInteger
      GET http://localhost:4004/api/v1/toInteger(value='20')
      ### call toNumber
      GET http://localhost:4004/api/v1/toNumber(value='20.2')
      ### call toString
      GET http://localhost:4004/api/v1/toStr(value=20.2)
      ### call addQoutes
      GET http://localhost:4004/api/v1/addQuotes(value='abcd')
      ### call getListOfTodods
      GET http://localhost:4004/api/v1/getListOfTodos()
      ### call listToString
      POST http://localhost:4004/api/v1/listToString
      Content-Type: application/json
          "field" : "id",
          "responseArray": [
                  "id": 1,
                  "title": "delectus aut autem",
                  "userId": 1,
                  "completed": false
                  "id": 2,
                  "title": "quis ut nam facilis et officia qui",
                  "userId": 1,
                  "completed": false
    3. From the Terminal menu, select New Terminal.

      open terminal
    4. On the sap-build-cap-sample-library folder, run the following command in the Terminal window:

      This will start a local server with your service that will allow you to test your API calls.

      Shell / Bash
      cds watch
    5. Choose Send Request, you will get the response in a separate window.

    6. Choose Ctrl - C to terminate the Server session.

    Log in to complete tutorial
  • Step 5

    We will use a script to build and deploy the service.

    1. On the sap-build-cap-sample-library folder, create a new file called

    2. Select the file and copy the following content into the editor.

      Shell / Bash
      # set API endpoint and login to Cloud Foundry
      cf api
      cf login
      # some needed npm settings
      npm update --package-lock-only
      npm set registry=
      # build the service locally
      mbt build -t gen --mtar mta.tar
      # deploy the service to the BTP space
      cf deploy gen/mta.tar
      # generate the openAPI spec for the service, used to create the Action project
      cds compile srv --service all -o docs --to openapi
    3. Check your API endpoint definition.

      Check in BTP Cockpit, if the API Endpoint of your Cloud Foundry environment is matching the information you used for the script

    4. Run the following command in the Terminal window:

      Shell / Bash
    5. During script execution, when asked for:

      • your login information, enter Email and Password

      • your space, on trial dev is created as default Cloud Foundry space

      exec script

    Once the tasks are completed, your service should be available and running in your Cloud Foundry space. Also a file with the openAPI specification is created. This file will be used in a next tutorial.

    In the BTP Cockpit you can check if the service is running:

    service started

    Log in to complete tutorial
Back to top