CAP project foundation — SAP Guided Tutorial
Free sample lesson · open to everyone
How a CAP project is laid out (db/, srv/, app/), how cds watch, profiles and cds env decide what actually runs, and how cds build turns your model into deployable modules — so a service that works locally also starts in test.
In this lessonShow sectionsHide sections
Visual overview
The situation: a real project scenario
A bookshop service runs fine with cds watch. After cf push, the app crashes on start with a database connection error and the Fiori app shows no data. cds env --profile production shows requires.db.kind = hana, but no HDI container was ever created or bound, and the gen/db module was never deployed because the team pushed only gen/srv.
Explanation
A CAP project follows a simple folder convention: db/ holds the domain model, srv/ holds the services and their handlers, and app/ holds the UIs, with package.json carrying the CDS configuration that ties it all together.
npm i -g @sap/cds-dk installs the CLI, and cds init bookshop scaffolds the skeleton; later facets like HANA, XSUAA, or MTA support are added with cds add hana, cds add xsuaa, cds add mta, and similar, rather than copied in by hand from blog posts, which keeps the generated config consistent with your actual CAP version.
The thing to understand early is that cds watch, the everyday development command, is not a preview of production — it's a convenience layer that hides a lot.
It runs against an in-memory SQLite database that's recreated on every restart and seeded from db/data/*.csv; it authenticates with mocked users like "alice" and "bob" defined in package.json, with no real XSUAA token involved; and remote services can be mocked locally so a missing destination never surfaces as an error at development time.
The practical consequence is that a service which starts fine under cds watch can still fail in test or production on a database connection, token validation, or a missing service binding — none of which cds watch ever exercised.
Profiles are how CAP resolves this gap. Entries like "[development]" and "[production]" under cds.requires swap out implementations — SQLite versus HANA, mocked auth versus XSUAA — and cds env requires.db --profile production prints exactly what the runtime will actually use for a given profile, which is worth comparing against the development output before every deployment.
On Cloud Foundry, NODE_ENV=production is set automatically, so the production profile activates without extra configuration — but that also means configuration drift between profiles, not a code bug, is the most common cause of "works on my machine" CAP failures.
Finally, cds build --production compiles the CDS model into deployable artifacts: gen/srv for the Node.js application plus its compiled model (csn.json), and gen/db for the HANA deployment artifacts (HDI). The build log lists every task and its output folder, and a missing gen/db after a build is a strong sign that no HANA tables will be deployed at all. Separately, cds compile srv --to edmx prints the OData $metadata your service will actually publish, which is worth checking directly before assuming a UI problem is really a service problem.
To see this for yourself: run cds init bookshop && cd bookshop && cds add sample, then cds watch and open /odata/v4/catalog/$metadata; run cds env requires --profile development and again with --profile production and note what differs; run cds build --production and list what appears under gen/; then deliberately break the app by setting [production].requires.db to a kind with no binding and starting with NODE_ENV=production cds-serve to see the resulting error.
By the end, you should be able to explain, from cds env output rather than from memory, exactly which database, auth strategy, and remote bindings each profile in your own project uses.
Code example
// package.json (excerpt){ "cds": { "requires": { "db": { "[development]": { "kind": "sqlite", "credentials": { "url": ":memory:" } }, "[production]": { "kind": "hana" } }, "auth": { "[development]": { "kind": "mocked" }, "[production]": { "kind": "xsuaa" } } } }}// Verify before you deploy:// cds env requires.db --profile production// cds build --production -> gen/srv, gen/dbWatch out
Common mistakes
- Treating
cds watchsuccess as proof the service will start on Cloud Foundry. - Hand-editing gen/ files — they are overwritten on the next build.
- Committing credentials in package.json or default-env.json instead of using service bindings.
- Pinning old @sap/cds versions across modules so db and srv run different compiler versions.
Do this
Best practices
- Use
cds add <facet>for HANA, XSUAA, MTA and multitenancy instead of copying config from blogs. - Check
cds envper profile in code review whenever package.json cds settings change. - Keep a small hybrid profile (
cds bind) so you can run locally against real HANA/XSUAA before deploying.
In the interview
Interview angle
Interviewers use this topic to separate people who have only run tutorials from people who have deployed CAP. Explain the folder conventions, then spend most of the answer on profiles: what cds watch mocks, how the production profile changes db and auth, and how you verify effective configuration with cds env before deploying.
Practice this lesson
Original educational case studies for SAP consultants, not official SAP certification exam questions. Verify product edition and implementation details against the current SAP documentation.
Quick check
0 of 4 questions attempted
Question 1 of 4
A CAP service starts with `cds watch` but crashes on Cloud Foundry with a database error. What is the most direct first check?
Question 2 of 4
What does `cds watch` use as its database by default in a new project?
Question 3 of 4
Which statement about the gen/ folder after `cds build --production` is correct?
Question 4 of 4
Where should XSUAA and HANA credentials for a deployed CAP app come from?
Practise this in an interview
Answer in your own words and get instant feedback, the way an interviewer would judge it.
1Your CAP app works with `cds watch` but fails after deployment to BTP Cloud Foundry. How do you approach it?
2How would you structure a new CAP project for a team of five developers?
Was this lesson helpful?
Keep exploring
Check before use: lesson examples are educational, not a guarantee of correctness for your SAP edition or release. Sample code has not been executed on your SAP system. Confirm API availability, syntax, authorizations and business impact in an approved development or test environment.