ERPClimb logoERPClimb

Code examples · SAP BTP · Intermediate

SAP BTP: build and deploy a Node.js CAP service (Support Desk with a Resolve action)

The problem

The support team needs a small cloud service to log tickets.

How we solve it

  1. 1Projectcds init
  2. 2Data modeldb/schema.cds
  3. 3Servicesrv/*.cds + .js
  4. 4Run + testcds watch
  5. 5DeployCloud Foundry

The result

A running CAP service on your laptop with sample data and tests, plus the exact commands to deploy the same project to SAP BTP.

Overview drawn by ERPClimb from this example. The steps are the same as in the code below.

1. The requirement

The support team needs a small cloud service to log tickets. A ticket must have a title, starts OPEN with priority MEDIUM unless stated otherwise, and a team member must be able to mark it RESOLVED, but only once. The service must run on SAP BTP and be callable as OData V4.

2. How we design it

Model first, then service, then logic, then deploy. The data model says what exists, the service decides what is published, the handler adds the rules CAP cannot guess, and the same project runs locally with an in-memory database and on BTP with SAP HANA Cloud.

Why we decided it this way

  1. 1CAP with Node.jsRuns on your laptop in seconds, so you can test every step before touching BTP.
  2. 2Reuse the cuid and managed aspectsUUID keys and created/modified fields come for free and behave like the standard CAP examples.
  3. 3Validation in a before handlerThe rule runs before anything is written and reports a precise field error.
  4. 4Resolve as a bound actionResolving a ticket is a business event with its own rule (only once), not a plain field update.
  5. 5Deploy with an MTAOne archive creates the app, the database deployment and the service instances in the right order.

Before you start

  • Node.js LTS and npm installed, plus an editor such as VS Code with the 'SAP CDS Language Support' extension.
  • For deployment only: a BTP subaccount with Cloud Foundry enabled, an SAP HANA Cloud instance, and the Cloud Foundry CLI with the multiapps plugin, and the Cloud MTA Build Tool (mbt).

3. The code, step by step

Step 1 of 6: Create the project

Install the CAP development kit once and scaffold an empty project.

Shell · Terminal
# Install the CAP development kit
# one-time
npm install --global @sap/cds-dk
# Scaffold and install
cds init support-desk
cd support-desk
npm install
code .   # optional: open in VS Code
Part by part (plain text)
  1. Install the CAP development kit. @sap/cds-dk gives you the 'cds' command-line tool (create projects, run a local server, generate deployment files). It is installed globally once per computer.
  2. Scaffold and install. 'cds init' creates the folders: db for the data model, srv for services, and a package.json. 'npm install' downloads the CAP runtime libraries.

Open in the portal

The SAP objects named in this code are linked in the code above and listed here. Each opens its page in the SAP Library.

4. The final outcome

A running CAP service on your laptop with sample data and tests, plus the exact commands to deploy the same project to SAP BTP.

What you should see

Locally: two sample tickets, a new ticket gets status OPEN and priority MEDIUM, a blank title returns HTTP 400. resolve returns the ticket with status RESOLVED; a second call returns 409. On BTP: cf apps shows the service as started.

Try it and check the result

  • After 'cds watch', open http://localhost:4004 and click the Tickets link: two sample rows appear.
  • Create a ticket with a blank title: you get HTTP 400 and the message 'A ticket needs a title'.
  • Call resolve on a ticket twice: first call returns status RESOLVED, the second returns 409 'Ticket is already resolved'.
  • After deployment, 'cf apps' lists support-desk-srv as started, and 'cf logs ... --recent' shows no startup errors.

Common mistakes

  • The .js file must have the same base name as the .cds service file (support-service.js) in the srv folder, otherwise the handlers are silently ignored.
  • Forgetting 'return super.init()' removes CAP's default behavior.
  • Mocked users exist only in development. After deploying with xsuaa, a call without a valid token gets 401.
  • SAP HANA Cloud instances stop after inactivity on trial accounts; start the instance in BTP Cockpit before deploying.
  • Never commit credentials or default-env.json files with secrets to GitHub.

Keep learning

Checking status: Reviewed line by line against SAP documentation. Not executed on a live system. Reviewed 2026-11-25. Release: SAP Cloud Application Programming Model with Node.js (current @sap/cds; check the version you install).

Original ERPClimb teaching code with illustrative names. Not taken from a customer system. Syntax can differ by release; check the references below and activate in a sandbox first. ERPClimb is independent of SAP SE.