SAP Guide: Consume an SAP API Specification in CAP

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 partner dashboard that gets its business partners from an SAP API. You do not have an SAP system, so you do the next best thing: you take the real description of the SAP Business Partner API (API_BUSINESS_PARTNER), let CAP turn it into a service definition, and let CAP pretend to be that API from a small CSV file of made-up partners. Your own service, PartnerDashboard, offers a short view of the partners, Partners, with three fields, and forwards every read to the API. The code that you write would not change if the API were a real SAP S/4HANA system; only the place that CAP sends the request to would.

Browser with the address localhost:4004/odata/v4/partner-dashboard/Partners with a filter on category 2 and a select of ID and name: the JSON response lists three partners, Example Trading Co. with ID 1000001, Northwind Retail GmbH with ID 1000002 and Warehouse Supplies Ltd. with ID 1000006

Before you start

  • No account is needed. The CAP tools install from the public npm registry without signing in, and the API description comes from a public GitHub repository without signing in. The SAP Business Accelerator Hub, where SAP publishes these descriptions, asks for a sign-in before a download, so this guide does not use it. Installing @sap/cds-dk and @sap/cds means accepting the SAP Developer License Agreement, version 3.2, which the packages state themselves.

  • The API description is the file API_BUSINESS_PARTNER.edmx of the repository capire/s4, which the CAP team publishes under the Apache License 2.0 (license text). Its readme calls the repository a sample that is not meant for production use. The file is 695 KB and is not part of this site’s lab: you download it in step 2.

  • Install Node.js 22.12 or newer and the tools of SAP Guide: Your First CAP Service (npm install --global @sap/cds-dk, then cds version). Use a code editor such as Visual Studio Code, and a terminal. A browser shows one of the results. You need curl, which current versions of Windows, macOS and Linux include.

  • Read Consuming SAP APIs from CAP first, or at least know what a remote service is. This guide runs the local part of that page step by step. Where the page goes on to a real system with a destination, this guide stops, because that needs an SAP BTP account.

  • The names API_BUSINESS_PARTNER, A_BusinessPartner, BusinessPartner, BusinessPartnerFullName and BusinessPartnerCategory are SAP’s: they are in the downloaded file, and Master Data and the Business Partner describes what the API offers. The names PartnerDashboard, Partners, ID, name and category are the model’s own. The six partners, their IDs and their categories are made up.

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

Steps

Get the API definition and import it

  1. Create the project and install its dependencies. The option --nodejs chooses the JavaScript runtime of CAP.

    cds init partners-s4 --nodejs
    cd partners-s4
    npm install
  2. Download the API description into the project folder. Use the address of the raw file, not the address of the GitHub page that shows it (see the table at the end). The option -L follows redirects, -sS hides the progress meter and still prints errors, and -o names the file that is written.

    curl -sSL -o API_BUSINESS_PARTNER.edmx \
      https://raw.githubusercontent.com/capire/s4/main/srv/external/API_BUSINESS_PARTNER.edmx
    wc -c API_BUSINESS_PARTNER.edmx

    The file was 695017 bytes and had the SHA-256 checksum 810008e1ac47b2ba3c5412178285f608a9017ee26ca42e7ea839efc19b4faf77 when this guide was checked (shasum -a 256 API_BUSINESS_PARTNER.edmx prints it). The repository may change the file later; a different size is not an error as long as the next step works. EDMX is the XML format in which an OData V2 service describes its entities, properties and associations. The file is data: cds import reads it and nothing runs it.

  3. Import it. The command copies the file into srv/external, converts it to CDS, and adds an entry for the service to package.json. The option --as cds writes the converted model as readable CDS text; without it the model is written as a .csn file, which is the same model as JSON.

    cds import API_BUSINESS_PARTNER.edmx --as cds

    The answer names the CDS line that you will use in step 5. The command also changed package.json: it added a block that tells CAP that a service named API_BUSINESS_PARTNER exists, that it is an OData V2 service, and where its model is.

    "cds": {
      "requires": {
        "API_BUSINESS_PARTNER": {
          "kind": "odata-v2",
          "model": "srv/external/API_BUSINESS_PARTNER"
        }
      }
    }

    The import also added three SAP Cloud SDK packages to the dependencies of package.json. They are what CAP uses to call a real system. The mocked service of this guide does not use them, so you do not need to install them here.

    Terminal with the three commands of the first phase: cds init partners-s4 with the option nodejs and npm install, then curl that saves API_BUSINESS_PARTNER.edmx, whose size wc reports as 695017 bytes, then cds import with the option as cds, which reports that it updated package.json and imported the API to srv/external/API_BUSINESS_PARTNER; callout 1 marks cds init, callout 2 marks the curl download and callout 3 marks cds import
  4. Open srv/external/API_BUSINESS_PARTNER.cds and find entity A_BusinessPartner. This is the business partner of the SAP API. Every element of the real API is listed, with SAP’s labels as @sap.label annotations. The two annotations at the top matter most: @cds.external : true says that the entity belongs to a service somewhere else, and @cds.persistence.skip : true says that CAP must not create a database table for it. The key is BusinessPartner; BusinessPartnerCategory holds the category of the partner (the sample data of step 6 uses 1 for a person and 2 for an organization); the associations at the end, such as to_BusinessPartnerAddress, lead to the addresses and other related data. CAP marks each association of an imported service with an empty key list, { }, because the file does not say how to look up the related rows.

    Visual Studio Code with the generated file srv/external/API_BUSINESS_PARTNER.cds open, shortened to an excerpt: the entity A_BusinessPartner with the annotation cds.external true and cds.persistence.skip true, the key BusinessPartner, the elements BusinessPartnerCategory and BusinessPartnerFullName and the association to_BusinessPartnerAddress; callout 4 marks the file in the explorer folder srv/external

    The figure shows an excerpt: the real entity has many more elements and associations, and the line numbers of the figure count the excerpt, not the file.

Describe your own view and add sample data

  1. Create the file srv/partner-service.cds. The first line imports the imported service under the short name bupa, with the line that step 3 printed. The service PartnerDashboard then offers one entity, Partners, as a projection on A_BusinessPartner: it keeps three elements and gives them your own names. Your code stays independent of the long SAP names, and a request asks the remote service only for what it needs. @readonly makes the entity read-only.

    using { API_BUSINESS_PARTNER as bupa } from './external/API_BUSINESS_PARTNER';
    
    service PartnerDashboard {
      @readonly entity Partners as projection on bupa.A_BusinessPartner {
        key BusinessPartner as ID,
        BusinessPartnerFullName as name,
        BusinessPartnerCategory as category
      };
    }
    Visual Studio Code with the file srv/partner-service.cds open: it imports API_BUSINESS_PARTNER as bupa and defines the service PartnerDashboard with the read-only entity Partners, a projection on bupa.A_BusinessPartner that renames BusinessPartner to ID, BusinessPartnerFullName to name and BusinessPartnerCategory to category; callout 5 marks the service file in the explorer
  2. Give the mocked API something to answer with. Create the folder srv/external/data and in it the file API_BUSINESS_PARTNER.A_BusinessPartner.csv. The name is the service, a dot and the entity, and CAP loads every such file into the mock’s table when it starts. The first row holds the SAP element names, the fields are separated by semicolons here, and an empty field stays empty. Customers have a number in Customer, suppliers one in Supplier; the six rows are made up.

    BusinessPartner;BusinessPartnerFullName;BusinessPartnerCategory;Customer;Supplier
    1000001;Example Trading Co.;2;1000001;
    1000002;Northwind Retail GmbH;2;1000002;
    1000003;Ana Torres;1;;
    1000004;Ben Okoye;1;;2000004
    1000005;Chloe Martin;1;;
    1000006;Warehouse Supplies Ltd.;2;;2000006

    Source: Consuming Services (archived CAP guide) (section Add Mock Data)

    Visual Studio Code with the file srv/external/data/API_BUSINESS_PARTNER.A_BusinessPartner.csv open: a header row with BusinessPartner, BusinessPartnerFullName, BusinessPartnerCategory, Customer and Supplier separated by semicolons, followed by six partners, from Example Trading Co. with the ID 1000001 to Warehouse Supplies Ltd. with the ID 1000006; callout 6 marks the folder data in the explorer

Run the mocked service

  1. Create the file srv/partner-service.js. It connects to the remote service by the name that package.json gives it, and answers every read of Partners by handing the incoming query to that service. cds.connect.to returns the mock while the API is mocked and a proxy to the real system when it is not; the line that forwards the query stays the same.

    import cds from '@sap/cds'
    
    export default class PartnerDashboard extends cds.ApplicationService {
      async init() {
        const bupa = await cds.connect.to('API_BUSINESS_PARTNER')
        this.on('READ', 'Partners', req => bupa.run(req.query))
        return super.init()
      }
    }

    Source: CAP-Level Service Integration (section Connecting to Remote Services)

    While the API is mocked in the same process, as it is here, the dashboard answers the same way without this file, because the mock’s table sits in the same database. The handler is the code that matters once the service is not mocked in the same process; Consuming SAP APIs from CAP runs that case.

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

    cds watch
  3. Read the log. CAP loads the model of your service and the imported one, fills an in-memory database from your CSV file (init from srv/external/data/API_BUSINESS_PARTNER.A_BusinessPartner.csv), and then lists two services. serving PartnerDashboard is yours, at /odata/v4/partner-dashboard, with your handler as impl. mocking API_BUSINESS_PARTNER is the foreign API that CAP plays itself, at /odata/v4/api-business-partner. CAP serves the mock as OData V4 although the imported file is OData V2, so your queries below use V4 syntax. Leave the terminal running.

    Terminal after cds watch: the log lists the model files srv/partner-service.cds and srv/external/API_BUSINESS_PARTNER.cds, connects to an in-memory SQLite database, initializes it from srv/external/data/API_BUSINESS_PARTNER.A_BusinessPartner.csv, serves PartnerDashboard at /odata/v4/partner-dashboard with the handler srv/partner-service.js, then reports mocking API_BUSINESS_PARTNER at /odata/v4/api-business-partner and ends with server listening on http://localhost:4004; callout 8 marks the command and callout 9 the lines that report the mocked service

    The figure leaves out the banner and the line about service bindings that cds watch may print. It shows the default port 4004. If another program uses that port, start the server with PORT=4005 cds watch and use that port in every address below.

Query the dashboard

  1. Open a second terminal and read three partners from your dashboard. The answer holds your names ID, name and category, not SAP’s.

    curl -s 'http://localhost:4004/odata/v4/partner-dashboard/Partners?$top=3'
  2. Ask for the companies only, and only for two fields. In a URL the spaces and quotes of the filter are written as %20 and %27, so category%20eq%20%272%27 is category eq '2'. The key ID is part of every answer.

    curl -s 'http://localhost:4004/odata/v4/partner-dashboard/Partners?$filter=category%20eq%20%272%27&$select=ID,name'
  3. Count the partners:

    curl -s 'http://localhost:4004/odata/v4/partner-dashboard/Partners/$count'
    Terminal with three curl requests to the dashboard and their answers: the first three partners with ID, name and category, the companies only (category 2) with ID and name, and the count 6; callout 10 marks the first request, callout 11 the second and callout 12 the third
  4. Look at the mocked API itself. Open this address in a browser: the answer uses SAP’s names, BusinessPartnerFullName and BusinessPartner, not yours. This is what a real system would also answer, apart from the path and the sign-in.

    http://localhost:4004/odata/v4/api-business-partner/A_BusinessPartner('1000003')?$select=BusinessPartnerFullName
    Browser with the address localhost:4004/odata/v4/api-business-partner/A_BusinessPartner with the key 1000003 and the select option BusinessPartnerFullName, marked by callout 13: the JSON response holds the context A_BusinessPartner entity, BusinessPartnerFullName Ana Torres and BusinessPartner 1000003, the names of the SAP API

Check the result

  • The log of cds watch has the lines serving PartnerDashboard and mocking API_BUSINESS_PARTNER and no line that starts with [ERROR].

  • Partners/$count answers 6, the number of rows in your CSV file.

  • The companies query answers the three rows with the category 2: Example Trading Co., Northwind Retail GmbH and Warehouse Supplies Ltd.

  • The address of the mocked API answers with BusinessPartnerFullName Ana Torres.

Variations

  • Sort the companies by name, descending, by adding &$orderby=name%20desc to the query of step 11. The answer lists Warehouse Supplies Ltd. first and Example Trading Co. last:

    {"@odata.context":"$metadata#Partners","value":[{"ID":"1000006","name":"Warehouse Supplies Ltd."},{"ID":"1000002","name":"Northwind Retail GmbH"},{"ID":"1000001","name":"Example Trading Co."}]}

What can go wrong

Symptom Cause Fix

cds import stops with Either the OData version is not valid, or the encoding of the file is not utf-8. and a hint about the versions [1.0, 2.0, 4.0, 4.01]

The downloaded file is not the EDMX file. The address of the GitHub page (github.com/.../blob/...) answers with the HTML of the page, and a wrong path under raw.githubusercontent.com answers with the text 404: Not Found; curl saved either one under the file name.

Look at the start of the file with head -c 100 API_BUSINESS_PARTNER.edmx: it must start with <?xml. Download again from the raw address of step 2.

cds watch prints [ERROR] srv/partner-service.cds:1:45-78: Can't find local module ‘./external/API_BUSINESS_PARTNER’, followed by errors that elements of bupa.A_BusinessPartner were not found

The import of step 3 did not run, or it ran in another folder, so srv/external/API_BUSINESS_PARTNER.cds does not exist.

Run cds import API_BUSINESS_PARTNER.edmx --as cds in the project folder. cds watch tries again when the file appears.

cds watch prints [ERROR] srv/partner-service.cds:6:5-27: Element “BusinessPartnerFullNam” has not been found (in entity:“PartnerDashboard.Partners”/column:“name”)

A typo in an element name of the projection (here BusinessPartnerFullNam). The message names the file, the line and the columns.

Compare the name with the entity in srv/external/API_BUSINESS_PARTNER.cds and correct it.

Partners answers "value":[], and the log has no init from srv/external/data/... line

The CSV file name does not match the service and the entity (for example A_BusinessPartner.csv), or the file is in another folder, so CAP did not load it.

Rename it to API_BUSINESS_PARTNER.A_BusinessPartner.csv and keep it in srv/external/data.

A POST to Partners answers HTTP/1.1 405 Method Not Allowed

The entity is @readonly, so the dashboard only reads.

This is intended. The data lives in the API, not in the dashboard.

Clean up

Press Ctrl+C in both terminals to stop cds watch. The database lived only in memory. You can delete API_BUSINESS_PARTNER.edmx from the project folder; the import kept a copy in srv/external. The folder partners-s4 is yours to keep.

Where to go next