SAP Guide: Your First CAP Service

This content was generated with the assistance of AI and should be verified against the official documentation before being relied on in production.

Checked on 2026-11-13 against CAP Node.js 10.2 (@sap/cds 10.2.2, @sap/cds-dk 10.2.1) and Node.js 24.21.

What you build

This is a teaching model built with CAP. It is not SAP S/4HANA or SAP SuccessFactors and does not reproduce their screens.

You build a small service for expense claims. You describe one entity, Claims, with a title, a status and an amount, and one service, ExpenseService, that offers it. A CSV file gives the service one claim to start with. CAP turns these three small files into a running web service with a database behind it, and you read its answers in the browser: the service description and the list of claims.

Browser with the address localhost:4004/odata/v4/expense/Claims?$select=title,amount&$count=true: the JSON response shows odata.count 1 and one claim with the title Train ticket, the amount 45.50 and its ID

Before you start

  • No account is needed: the CAP tools install from the public npm registry without signing in. Installing @sap/cds-dk and @sap/cds means accepting the SAP Developer License Agreement, version 3.2, which the packages state themselves; the lab README names it.

  • Install Node.js 22.12 or newer. node -v prints the version.

  • Use a code editor such as Visual Studio Code, and a terminal. A browser shows the results.

  • Read CAP Getting Started and CAP Concepts first, or at least know what an entity, a service and OData are.

  • The figures are drawings of the editor, the terminal and the browser, not screenshots. A highlighted box with a number marks the file, the line or the address of the step with the same number.

Steps

The numbered overview shows the editor that you use in the first steps. The regions are the explorer with the project files, the editor with the open file and the terminal in which the commands run.

Map of Visual Studio Code with the CAP project open and six numbered regions: the explorer, the schema file, the service file, the editor tabs, the editor with the service source and the integrated terminal that runs cds watch

Create the project

  1. Install the CAP command line tools once. The package is named @sap/cds-dk and it brings the command cds. Ask for the version afterward:

    npm install --global @sap/cds-dk
    cds version

    The first lines of the answer name the versions that this guide was checked with:

      @sap/cds            10.2.2
      @sap/cds-compiler   7.2.1
      @sap/cds-fiori      2.3.0
      @cap-js/db-service  3.2.0
      @cap-js/sqlite      3.1.3
  2. Create the project folder. The option --nodejs chooses the JavaScript runtime of CAP:

    cds init expenses --nodejs

    The command prints Successfully initialized CAP project and creates the folders app, db and srv, plus package.json, a readme and an editor configuration. Without --nodejs it creates the folders but asks you to choose a runtime with cds add nodejs.

  3. Go into the folder and install the project’s dependencies. package.json lists two: the CAP runtime @sap/cds and, as a development dependency, @cap-js/sqlite, the in-memory database that CAP uses while you develop. Open the folder in your editor (in Visual Studio Code, code .).

    cd expenses
    npm install
    Visual Studio Code with the new expenses project: the explorer lists the folders app, db and srv and the files package.json and readme.md, the editor shows the generated package.json, and the terminal shows cds init expenses with the option nodejs, then npm install ending in found 0 vulnerabilities; callout 2 marks the app, db and srv folders and callout 3 marks package.json

Describe the data and the service

The db folder holds the data model, the srv folder holds the services. Both are written in CDS, the text language of CAP.

  1. Create the file db/schema.cds. The first line imports two ready-made building blocks from CAP: cuid adds a key field ID that CAP fills with a new unique value, and managed adds the fields createdAt, createdBy, modifiedAt and modifiedBy, which CAP fills for you. The namespace prefixes the names in the database. The entity then lists its own fields with their types.

    using { cuid, managed } from '@sap/cds/common';
    namespace sap.guide.expenses;
    entity Claims : cuid, managed {
      title  : String(80);
      status : String(1) default 'N';
      amount : Decimal(9,2);
    }
    Visual Studio Code with the file db/schema.cds open: it imports cuid and managed, sets the namespace sap.guide.expenses and defines the entity Claims with the fields title, status and amount; callout 4 marks the file in the explorer
  2. Create the file srv/expense-service.cds. A service is a window onto the data: it names the entities that outsiders may use. Here Claims is a projection on the entity of the model, which means it shows its fields unchanged. CAP serves every service as an OData service, the web protocol that browsers and SAP apps understand.

    using { sap.guide.expenses as db } from '../db/schema';
    service ExpenseService {
      entity Claims as projection on db.Claims;
    }
  3. Create the folder db/data and in it the file sap.guide.expenses-Claims.csv. CAP loads every CSV file in db/data into the database when the server starts. The file name must be the namespace, a dash and the entity name, and the first row names the fields:

    ID,title,status,amount
    11111111-1111-1111-1111-111111111111,Train ticket,N,45.50
    Visual Studio Code with the file srv/expense-service.cds open: the service ExpenseService exposes the entity Claims as a projection on db.Claims; callout 5 marks the service file and callout 6 marks the CSV file sap.guide.expenses-Claims.csv in the folder db/data

Run the service

  1. In the terminal, in the project folder, start the server in watch mode:

    cds watch
  2. Read the log. CAP lists the model files it loaded, connects to a database that lives only in memory (sqlite { url: ':memory:' }), fills it from your CSV file (init from db/data/sap.guide.expenses-Claims.csv) and reports that the database is deployed.

  3. The last lines name the service path and the address. ExpenseService is served at /odata/v4/expense (CAP derives expense from the service name) and the server listens on http://localhost:4004. The line using auth strategy { kind: 'mocked' } says that no real login is used while you develop. Leave the terminal running: cds watch restarts the server when you save a file.

    Terminal after cds watch: the log lists the model files srv/expense-service.cds, db/schema.cds and the common.cds of CAP, connects to an in-memory SQLite database, initializes it from db/data/sap.guide.expenses-Claims.csv, serves ExpenseService at /odata/v4/expense and ends with server listening on http://localhost:4004; callout 7 marks the command, callout 8 the database lines and callout 9 the listening line

    The figure leaves out the banner that cds watch prints before the first line.

Read the service in the browser

  1. Open http://localhost:4004. CAP generates a welcome page that lists the service Expense Service with the entity Claims and a link to /$metadata. It states itself that it is a generated page that you can replace with your own app/index.html.

  2. Open http://localhost:4004/odata/v4/expense/$metadata. The answer is the service description, written in XML: the entity set Claims, its key ID and the properties that come from your model, among them the four fields of managed. The real answer is longer, because CAP adds annotations that describe the fields; the figure keeps the schema.

    Browser with the address localhost:4004/odata/v4/expense/$metadata, marked by callout 11: the XML document, shortened to its schema, describes the entity container with the entity set Claims and the entity type Claims with the key ID and the properties ID, createdAt, createdBy, modifiedAt, modifiedBy, title, status and amount
  3. Open http://localhost:4004/odata/v4/expense/Claims. The answer is JSON with your claim in value: the fields of your CSV file, and the managed fields that CAP set when it loaded the data (createdBy is anonymous, because nobody logged in).

    Browser with the address localhost:4004/odata/v4/expense/Claims, marked by callout 12: the JSON response holds one claim with the ID, the created and modified fields, the title Train ticket, the status N and the amount 45.50
  4. Ask for less. A query option after the question mark chooses fields and asks for a count: open http://localhost:4004/odata/v4/expense/Claims?$select=title,amount&$count=true. The answer holds the count and only the title and the amount; the key ID is always part of the answer.

    {"@odata.context":"$metadata#Claims","@odata.count":1,"value":[{"title":"Train ticket","amount":"45.50","ID":"11111111-1111-1111-1111-111111111111"}]}

    Your browser may show this answer on one line, as above, or formatted. The data is the same.

Check the result

  • The terminal shows server listening on { url: 'http://localhost:4004' } and no line that starts with [ERROR].

  • /odata/v4/expense/Claims returns the claim Train ticket with the status N and the amount 45.50.

  • /odata/v4/expense/$metadata lists an entity set Claims.

Variations

  • Add a second line to the CSV file, for example 22222222-2222-2222-2222-222222222222,Taxi,N,18.00, and save it. cds watch restarts, and Claims?$count=true reports two claims.

  • Add a filter: Claims?$filter=amount gt 20&$select=title,amount returns only the train ticket.

What can go wrong

Symptom Cause Fix

The log shows [ERROR] srv/expense-service.cds:3:31-34: Mismatched ‹Identifier›, expecting ‘on’ and the service does not start

A typo in a CDS file, here projection onn for projection on. The message names the file, the line and the columns.

Correct the text at the position named in the message and save. cds watch tries again.

Claims answers "value":[]

The CSV file name does not match the namespace and the entity (for example claims.csv), so CAP did not load it.

Rename it to sap.guide.expenses-Claims.csv, in the folder db/data.

The address of a wrong entity set answers HTTP 404

/odata/v4/expense/Nothing is not an entity of the service.

Compare the name with the entity set in $metadata.

command not found: cds

The tools were not installed, or the global npm folder is not on your PATH.

Run npm install --global @sap/cds-dk again and open a new terminal.

Clean up

Press Ctrl+C in the terminal to stop cds watch. The database lived only in memory, so there is nothing to delete. The folder expenses is yours to keep: later guides build on it.

Where to go next