Service projections and OData — SAP Guided Tutorial
Free sample lesson · open to everyone
How CAP services expose a deliberate API over the domain model using projections, read-only and excluded elements, actions and functions — and how to read the resulting OData V4 $metadata.
In this lessonShow sectionsHide sections
Visual overview
The situation: a real project scenario
An admin UI and a public catalog shared one service exposing Orders directly. Public users could see createdBy e-mail addresses and could even PATCH stock. Splitting into a read-only CatalogService (projection without audit fields) and an AdminService with @requires: 'admin' closed both gaps without touching the database model.
Explanation
A CAP service is the deliberate, public API you choose to expose — the domain model underneath it stays internal, and clients should only ever see projections, not raw tables.
A projection such as entity Books as projection on my.Books { ID, title, author.name as author } can flatten associations, rename fields, and limit what is visible, which is exactly how you avoid freezing your internal table design into a contract that other teams depend on, and how you avoid leaking fields like audit columns that were never meant to be public.
Services are declared with their own path, for example service CatalogService @(path: '/browse') { ... }, served under an OData V4 endpoint whose exact prefix depends on your CAP version and protocol settings.
A good rule of thumb is one service per consumer type — a read-facing CatalogService for shoppers and a separate AdminService for back-office staff — because each service gets its own authorization, and a single service trying to serve every audience tends to either over-expose data or become unworkable to secure.
Annotations like @readonly, @insertonly, and excluding { createdBy, modifiedBy } control what operations and fields a projection allows, and when two projections expose the same underlying entity, @cds.redirection.target tells CAP which projection an association should resolve to, avoiding ambiguous navigation.
Beyond plain CRUD, services expose actions and functions. Actions change data and are called with POST — an unbound one might look like action submitOrder(book : Books:ID, quantity : Integer) returns { stock : Integer };, while a bound action lives on an entity, declared inside actions { action cancel(reason : String); } and called at a URL like /Orders(ID)/CatalogService.cancel.
Functions are the read-only counterpart, called with GET, and are the right tool for calculations or lookups that have no side effects — using a function for something that mutates data, or vice versa, breaks the HTTP semantics clients and tooling rely on.
The resulting contract is visible in $metadata, which you can inspect directly via a GET request or generate offline with cds compile srv --to edmx. It's worth reviewing before a UI gets built against it — checking keys, types, and nullability match what consumers will expect — because once consumers exist, renaming or removing an element is a breaking change; the safer path is to add new fields and, when you truly must break something, version the service.
To practice this: build a CatalogService that exposes Books read-only, excludes the audit fields, and flattens the author's name; add an unbound submitOrder action and a bound cancel action on Orders; then diff $metadata before and after to see the new action imports, and call the bound action to see its URL pattern. You should be able to point at any element in $metadata and say exactly which projection or action in srv/ produced it.
Code example
using my.bookshop as my from '../db/schema'; service CatalogService @(path: '/browse') { @readonly entity Books as projection on my.Books { ID, title, price, currency, author.name as authorName } excluding { createdBy, modifiedBy }; action submitOrder(book : Books:ID, quantity : Integer) returns { stock : Integer };}Watch out
Common mistakes
- One giant service exposing all entities to every user group.
- Using functions for operations that change data.
- Renaming service elements without telling consumers — silently breaking UIs and integrations.
- Forgetting @cds.redirection.target when two projections of one entity exist, causing ambiguous associations.
Do this
Best practices
- Design services per consumer and role; keep the domain model internal.
- Mark read-only consumer services @readonly and exclude technical fields.
- Review $metadata in pull requests for any service change that UIs or integrations consume.
In the interview
Interview angle
Interviewers want to hear that a CAP service is a deliberate API, not a database dump. Talk about projections per consumer, read-only exposure, actions vs functions, and how you treat $metadata as a contract you cannot change casually.
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
Which operation type should a CAP service use for a side-effect-free calculation such as getting a shipping quote?
Question 2 of 4
What is the main purpose of a service projection like `entity Books as projection on my.Books { ID, title }`?
Question 3 of 4
Two projections of Orders exist in one service and associations become ambiguous. Which annotation resolves which one associations should target?
Question 4 of 4
A consumer UI broke after a release although the database did not change. What is the most likely cause?
Practise this in an interview
Answer in your own words and get instant feedback, the way an interviewer would judge it.
1How do you design CAP services for different consumers of the same data?
2When would you use a bound action instead of an unbound action in CAP?
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.