The Cloud Application Programming Model (CAP) is SAP’s recommended framework for building enterprise-grade applications on SAP Business Technology Platform (BTP). One of CAP’s core strengths is its seamless integration with the OData protocol, which enables standardized service exposure for easy consumption by SAP Fiori apps and other clients.
This article explores how developers using SAP Business Application Studio (BAS) can expose backend services as OData services in CAP, facilitating smooth communication across SAP solutions.
OData (Open Data Protocol) is an open web protocol used to query and update data, built on standard web technologies such as HTTP, REST, and JSON/XML. It provides:
CAP natively supports OData v2 and v4, making it simple to expose business logic as OData services.
Use Core Data Services (CDS) to model your entities in .cds files.
Example db/schema.cds:
namespace my.bookshop;
entity Books {
key ID : UUID;
title : String;
author : String;
stock : Integer;
}
Define a service exposing your entities, typically in srv/service.cds:
using my.bookshop as db from '../db/schema';
service CatalogService {
entity Books as projection on db.Books;
}
This projection allows the entity Books to be exposed through the service.
Create a service implementation in JavaScript or TypeScript (e.g., srv/service.js) to add custom logic, validations, or actions.
const cds = require('@sap/cds');
module.exports = cds.service.impl(async function() {
const { Books } = this.entities;
this.after('READ', Books, each => {
if (each.stock <= 0) {
each.status = 'Out of stock';
}
});
});
Run your CAP application locally in BAS terminal:
cds run
By default, your OData service will be available at http://localhost:4004/catalog/Books with metadata at http://localhost:4004/catalog/$metadata.
You can test queries with standard OData syntax:
GET /catalog/Books?$filter=stock gt 0&$orderby=title
Exposing services with OData in CAP is straightforward and powerful, enabling developers to build interoperable, scalable applications on SAP BTP using SAP Business Application Studio. By leveraging CDS for modeling and CAP’s service layer, you can rapidly develop OData services that integrate natively with SAP Fiori and other consumer applications.