Modern enterprise applications rarely work in isolation. CAP makes it easy to integrate with external APIs — whether they are REST-based, OData-based, SAP systems, or third-party services. With CAP’s built-in connectivity, developers can consume remote services securely, consistently, and with minimal boilerplate code.
This guide walks you through how CAP handles external REST APIs, OData services, destinations, handlers, and best practices.
Why External Integrations Matter in CAP
CAP applications commonly integrate with:
- SAP S/4HANA OData APIs
- External REST APIs (payments, logistics, CRM, etc.)
- SAP SuccessFactors, Ariba, Concur
- Business partner or vendor systems
- Microservices within the enterprise
CAP offers a unified way to consume them using cds.connect.to(), service proxies, and destination bindings.
Benefits:
- Clean separation between internal & external logic
- Reusable service definitions
- Simplified authentication
- Works across local and BTP environments
Consuming REST Services in CAP
REST APIs are the most common integration scenario.
Step 1: Define the External Service in package.json
"cds": {
"requires": {
"WeatherService": {
"kind": "rest",
"credentials": {
"url": "https://api.weather.com"
}
}
}
}
Step 2: Connect to the Service in Your Handler
const cds = require('@sap/cds');
module.exports = cds.service.impl(async function () {
const weather = await cds.connect.to('WeatherService');
this.on('getWeather', async req => {
const city = req.data.city;
return weather.send('GET', `/forecast?city=${city}`);
});
});
CAP handles:
- HTTP calls
- Authentication via credentials or destinations
- Request forwarding
- Error handling
Step 3: Define the Action in CDS
service WeatherAPI {
action getWeather(city: String) returns String;
}
Consuming OData Services in CAP
CAP has first-class support for OData V2 and V4 — ideal for SAP system integrations.
Step 1: Import OData Metadata
cds import https://services.odata.org/V4/OData/OData.svc
This generates a model under srv/external/.
Step 2: Bind the External OData Service in package.json
"cds": {
"requires": {
"Northwind": {
"kind": "odata-v4",
"model": "srv/external/Northwind"
}
}
}
Step 3: Use the OData Service in Handlers
module.exports = cds.service.impl(async function () {
const northwind = await cds.connect.to('Northwind');
this.on('getProducts', async req => {
return northwind.run(SELECT.from('Products'));
});
});
CAP translates queries into OData calls automatically.
Using Destinations (BTP-Ready Integration)
Destinations are essential for secure API consumption in SAP BTP.
Example destination-based setup
"WeatherService": {
"kind": "rest",
"credentials": {
"destination": "weather-api"
}
}
On BTP:
- No API keys are stored in code
- Authentication handled through destination service
- Works in production securely
Calling REST APIs With Custom Payloads
this.on('createOrder', async req => {
const payload = {
product: req.data.product,
qty: req.data.qty
};
return externalService.send({
method: "POST",
path: "/orders",
data: payload
});
});
Handling Authentication
CAP supports:
- API Key
- OAuth2 Client Credentials
- Basic Auth
- JWT tokens
- SAP Destination Service
Example: API Key
"credentials": {
"url": "https://api.xyz.com",
"headers": {
"apikey": "MY_KEY"
}
}
Example: OAuth2 (Destination)
Handled automatically by BTP:
"credentials": {
"destination": "sales-api"
}
Error Handling in External Calls
try {
const result = await svc.send('GET', '/path');
return result;
} catch (err) {
req.error(500, `External API Error: ${err.message}`);
}
Always wrap external calls in try-catch.
Real-World Example
A CAP-based procurement app may:
- Fetch supplier data from SAP S/4HANA OData API
- Call a REST-based logistics API to track shipments
- Sync pricing from an external ERP
- Update inventory in a partner’s system
CAP’s modular approach ensures all these integrations remain clean, secured, and maintainable.
Best Practices
- Keep external service logic inside service handlers
- Never hard-code API keys; use destinations
- Use CDS import for OData services to auto-generate models
- Separate integration logic into dedicated service files
- Handle errors gracefully and return meaningful messages
- Cache frequent external calls when required
- Log requests in debug mode for troubleshooting
Conclusion
Consuming external REST and OData services in CAP is simple, secure, and highly extensible. CAP abstracts the complexity of API calls, authentication, and query translation, letting developers focus on business logic instead of integration plumbing.
With external service consumption, CAP becomes a powerful integration hub for SAP and non-SAP systems alike.

WhatsApp us