OData and SAP Gateway
|
This content was generated with the assistance of AI and should be verified against the official documentation before being relied on in production. |
Almost every SAP user interface and almost every integration with an SAP system talks to the back end in one way: an
ordinary web request to an address, answered with data. The rules for those requests are a standard called OData. A
newcomer meets it the first day: an SAP Fiori app reads its list from an OData address, a script that creates a sales
order sends a request to one, and the older SAP Gateway component is the part of an SAP back end that publishes such
addresses. This page explains what OData is and how version 2 (V2) and version 4 (V4) differ, how to read the
description every service offers about itself, how to ask for exactly the rows and fields you want, how a write request
proves it is not forged, and how a service becomes reachable in an SAP system. The request listings were run for real
against the CAP lab of this site (CAP Node.js 10.2, @sap/cds 10.2.2) on 2026-11-13. The lab serves V4, so every V2
and Gateway-specific step is described and labeled "requires an SAP system; not run here". The page applies to SAP
S/4HANA on premise 2025 FPS02 and its SAP Gateway, to the ABAP environments that serve OData with RAP (ABAP for Cloud
Development release 9.20, SAP S/4HANA Cloud Public Edition 2608 and SAP BTP ABAP environment 2608), and to CAP on SAP
BTP.
Before you read
-
What Is SAP: the products that publish OData services.
-
Architecture and the Virtual Data Model: the layers behind a service.
-
CAP Getting Started: the project and the
cds watchserver used for the requests on this page. -
Projections, Service Definitions and OData V4 Bindings: how an ABAP business object becomes an OData V4 service.
Key terms
-
OData: a standard that defines how to read and change data with ordinary HTTP requests and URLs.
-
SAP Gateway: the component of an ABAP system that publishes OData services and manages them.
-
Metadata document: the machine-readable description of a service, served at the address ending in
$metadata. -
System query option: a URL option that starts with a dollar sign, such as
$filter, and shapes the answer. -
CSRF token: a one-time value a client must send with a write request so that a forged request from another web page is rejected.
-
SAP Gateway Service Builder (SEGW): the tool in an ABAP system for building V2 services by hand.
What OData is
OData is a standard for APIs that work on data. Its own web site says that OData "has been standardized by OASIS" and
approved as an ISO/IEC International Standard. A client asks for an entity set, which is a collection of
things of one kind such as Claims, by putting its name in the URL after the address of the service, and the server
answers with JSON (or XML). The HTTP method says what to do: GET reads, POST creates, PATCH or PUT changes and DELETE
removes. Because every service follows the same rules, one client library can talk to every service, which is why SAP
Fiori apps, SAP Build tools, and non-SAP tools such as spreadsheets all use it.
The parts of a request are always the same: the service root (for example /odata/v4/expense/), the entity set
(Claims), optionally a key to pick one row, and system query options after a question mark. Everything that follows
shows these parts at work.
Source: OData: Documentation
OData V2 and V4
SAP supports two versions that matter to a newcomer. The SAP Learning course on technical implementation says that SAP Gateway supports OData V2 since AS ABAP 7.00 and OData V4 since AS ABAP 7.50, and that version 3 was skipped in SAP Gateway and is therefore not supported. V2 is the older one and is what most existing SAP S/4HANA APIs and many SuccessFactors APIs still speak. V4 is the version that the standards body OASIS maintains today, and it is what RAP and CAP serve. The two are different protocols, not two settings of one, so a client written for V2 does not work unchanged against a V4 service.
| Topic | OData V2 | OData V4 |
|---|---|---|
JSON response |
The result is wrapped in an object with one property named |
The result has |
Count with the rows |
The option |
The option |
Batch |
One POST to |
One POST to |
Path in an SAP system |
|
|
Source: V2 facts from OData V2: JSON Format, OData V2: URI Conventions and OData V2: Batch Processing; V4 facts from OData V4.01: URL Conventions and the real answers on this page; support dates from SAP Learning: Configuring SAP Gateway; the V4 transaction name /IWFND/V4_ADMIN and the V2 transaction /IWFND/MAINT_SERVICE from the same SAP Learning lesson
The V2 behavior of a service on an SAP system was not run here. The V4 rows were checked against the lab.
The lab service used on this page
The lab project modules/ROOT/examples/sap-lab/expenses has one entity, Claims. To show $expand, this page extends
the lab, in a copy of it, with a second entity, Items, the lines of a claim, and a composition from claims to items.
Nothing else changed. The lab table is a reduced version of the expense claim of the running example (it has a title,
a status and an amount only), and the extension is likewise a teaching model, not an SAP table. This is db/schema.cds
of the lab copy:
using { cuid, managed } from '@sap/cds/common';
namespace sap.guide.expenses;
entity Claims : cuid, managed {
title : String(80);
status : String(1) default 'N';
amount : Decimal(9,2);
items : Composition of many Items on items.claim = $self;
}
entity Items : cuid {
claim : Association to Claims;
category : String(20);
amount : Decimal(9,2);
}
Source: the lab file db/schema.cds extended in a copy of the lab, modeling described at CAP Getting Started
Three claims (Train ticket 45.50, Hotel Madrid 180.00, Taxi 12.30) and three items (a ticket, a room, a breakfast) were
loaded from CSV files, and the server was started with PORT=4377 npx cds-serve. The service
ExpenseService was mounted at /odata/v4/expense. In the requests below, B stands for
http://localhost:4377/odata/v4/expense. In a shell, the dollar signs of the options are written \$ so that the
shell does not treat them as variables, and spaces are written %20.
The metadata document
Every OData service describes itself. The address of the service root with $metadata added returns an XML document
(in OData, the EDMX document) that lists the entity sets, their properties and types, and how entities link to each
other. A client reads it to learn what it can ask. The first lines of the real answer for the lab:
curl -s "$B/\$metadata"
<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="4.0" xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx">
<edmx:Reference Uri="https://sap.github.io/odata-vocabularies/vocabularies/Common.xml">
<edmx:Include Alias="Common" Namespace="com.sap.vocabularies.Common.v1"/>
</edmx:Reference>
...
<EntityContainer Name="EntityContainer">
<EntitySet Name="Claims" EntityType="ExpenseService.Claims">
<NavigationPropertyBinding Path="items" Target="Items"/>
Source: real answer of the request above (shortened: the references to vocabularies and most of the document are left out), described at CAP Getting Started
Three things are worth reading: Version="4.0" says which protocol version this is, each EntitySet is a collection
you can request, and a NavigationPropertyBinding named items says that you can go from a claim to its items. The
references to vocabularies at the top name sets of annotations that SAP defines, which Fiori apps use to build screens;
the page on SAP Fiori Elements shows them at work. The service root without
$metadata lists the entity sets:
curl -s "$B/"
{"@odata.context":"$metadata","@odata.metadataEtag":"W/\"rYo2HLPGKfx+kHcv3rxvkRslHzpSPCBT+qCoycs2Dzc=\"","value":[{"name":"Claims","url":"Claims"},{"name":"Items","url":"Items"}]}
Source: real answer of the request above, CAP Getting Started
Query options: $filter, $select, $orderby and $count
System query options shape the answer on the server, so only the rows and fields you need cross the network. The
OData URL conventions define $filter (keep matching rows), $select (keep named properties), $orderby (sort),
$top and $skip (page through the rows), $expand (include related entities) and $count (the number of items). The
first request keeps the claims above 20, returns only title and amount, sorts by amount descending and asks for the
count:
curl -s "$B/Claims?\$filter=amount%20gt%2020&\$select=title,amount&\$orderby=amount%20desc&\$count=true"
{"@odata.context":"$metadata#Claims","@odata.count":2,"value":[{"title":"Hotel Madrid","amount":"180.00","ID":"22222222-2222-2222-2222-222222222222","IsActiveEntity":true},{"title":"Train ticket","amount":"45.50","ID":"11111111-1111-1111-1111-111111111111","IsActiveEntity":true}]}
Source: real answer of the request above, option names from OData V4.01: URL Conventions
The Taxi claim (12.30) is left out and the two others are in descending order of amount. @odata.count is 2: the count
of the rows that matched. The key ID and IsActiveEntity came back although $select did not name them. The lab
service has the annotation @odata.draft.enabled, which adds draft handling, and CAP always returns the key. Paging
with $top and $skip:
curl -s "$B/Claims?\$top=1&\$skip=1&\$select=title&\$orderby=title"
{"@odata.context":"$metadata#Claims","value":[{"title":"Taxi","ID":"33333333-3333-3333-3333-333333333333","IsActiveEntity":true}]}
Source: real answer of the request above, OData V4.01: URL Conventions
The titles in order are Hotel Madrid, Taxi, Train ticket; skipping one and taking one gives Taxi. The path suffix
/$count returns only the number of items in the collection:
curl -s "$B/Claims/\$count"
3
Source: real answer of the request above, OData V4.01: URL Conventions
The same request against a V2 service would read $inlinecount=allpages and answer inside the d wrapper. That
variant was not run here (requires an SAP system; not run here). Asking for a property that does not exist is a client
error, not an empty answer:
curl -s -i "$B/Claims?\$select=nosuch"
HTTP/1.1 400 Bad Request
OData-Version: 4.0
Content-Type: application/json; charset=utf-8
{"error":{"message":"Property \"nosuch\" does not exist in \"ExpenseService.Claims\"","code":"400","@Common.numericSeverity":4}}
Source: real answer of the request above (shortened: the date, correlation and connection headers are left out), OData V4.01: URL Conventions
Following links with $expand
A claim has items. Without $expand a client would ask for the claims, then once per claim for its items. The option
$expand asks for the related rows inside the same answer, and $select inside the parentheses limits their
properties:
curl -s "$B/Claims?\$select=title,amount&\$expand=items(\$select=category,amount)&\$filter=IsActiveEntity%20eq%20true"
{"@odata.context":"$metadata#Claims","value":[{"title":"Train ticket","amount":"45.50","items":[{"category":"Ticket","amount":"45.50","ID":"a0000000-0000-0000-0000-000000000003","IsActiveEntity":true}],"ID":"11111111-1111-1111-1111-111111111111","IsActiveEntity":true},{"title":"Hotel Madrid","amount":"180.00","items":[{"category":"Room","amount":"150.00","ID":"a0000000-0000-0000-0000-000000000001","IsActiveEntity":true},{"category":"Breakfast","amount":"30.00","ID":"a0000000-0000-0000-0000-000000000002","IsActiveEntity":true}],"ID":"22222222-2222-2222-2222-222222222222","IsActiveEntity":true},{"title":"Taxi","amount":"12.30","items":[],"ID":"33333333-3333-3333-3333-333333333333","IsActiveEntity":true}]}
Source: real answer of the request above, option names from OData V4.01: URL Conventions
The hotel claim carries its two lines inside it, and the taxi claim has an empty list. The filter on IsActiveEntity
is there because the lab service is draft enabled: a claim being edited exists twice, once as a draft and once as the
saved version, and the filter asks for the saved ones only (see
Draft Handling in RAP for the same idea in ABAP).
Batch requests
Each web request costs a round trip. A batch request packs several requests into one POST to $batch, so a screen
that needs three lists loads them in one trip. In V2 the body is multipart/mixed text, with read requests and change
sets (groups of writes that succeed or fail together), and the answer is a 202 Accepted. CAP’s V4 service also takes a
JSON body, which is easier to read:
curl -s -i -X POST "$B/\$batch" -H 'Content-Type: application/json' -d '{"requests":[{"id":"1","method":"GET","url":"Claims?$select=title&$top=1"},{"id":"2","method":"GET","url":"Items?$select=category&$top=1"}]}'
HTTP/1.1 200 OK
OData-Version: 4.0
Content-Type: application/json
{"responses":[{"id":"1","status":200,"headers":{...},"body":{"@odata.context":"$metadata#Claims","value":[{"title":"Train ticket","ID":"11111111-1111-1111-1111-111111111111","IsActiveEntity":true}]}},{"id":"2","status":200,"headers":{...},"body":{"@odata.context":"$metadata#Items","value":[{"category":"Room","ID":"a0000000-0000-0000-0000-000000000001","IsActiveEntity":true}]}}]}
Source: real answer of the request above (shortened: the date and correlation headers are left out and the inner headers are shown as three dots), V2 format from OData V2: Batch Processing
Each answer carries the id of its request and its own status, so one failing part does not hide the others. The
multipart V2 form was not run here (requires an SAP system; not run here).
Writes and the CSRF token
A browser sends the cookies of a site along with every request to it, even a request started by a page of another site.
A malicious page could therefore make your browser send a write request to an SAP system where you are logged in. The
defense that SAP Gateway uses is the CSRF token. A client first sends a harmless read with the header
x-csrf-token: fetch. The server answers with a token in the x-csrf-token response header and starts a session. The
client then sends the token, with the same session cookie, on every POST, PUT, PATCH or DELETE. A missing, expired or
mismatched token is answered with 403 Forbidden, and fetching a fresh token usually fixes it. A token on its own is
not enough: it belongs to the session, so the cookie has to travel with it.
Source: SAP Gateway: Cross-Site Request Forgery Protection (SAP NetWeaver 7.5, an older page), which describes the fetch request, the cookie and the 403 answer
Requires an SAP system; not run here:
curl -s -i -c cookies.txt -H 'x-csrf-token: fetch' -u USER "https://<host>/sap/opu/odata/sap/<SERVICE>/"
curl -s -i -b cookies.txt -X POST -H 'x-csrf-token: <token from the first answer>' -H 'Content-Type: application/json' -d '{...}' -u USER "https://<host>/sap/opu/odata/sap/<SERVICE>/<EntitySet>"
Source: the sequence described at SAP Gateway: Cross-Site Request Forgery Protection (SAP NetWeaver 7.5, an older page); the host, service, entity set, user and body are placeholders, and the commands were written from that description and not run because they need an SAP system
The first command stores the session cookie in cookies.txt (-c) and the second sends it back (-b) together with
the token read from the first answer’s headers. The same fetch header was sent to the lab, to see what a CAP service
does with it:
curl -s -i -H 'x-csrf-token: fetch' "$B/Claims?\$top=1&\$select=title"
HTTP/1.1 200 OK
OData-Version: 4.0
Content-Type: application/json; charset=utf-8; ieee754compatible=true
Content-Length: 138
Connection: keep-alive
Keep-Alive: timeout=5
{"@odata.context":"$metadata#Claims","value":[{"title":"Train ticket","ID":"11111111-1111-1111-1111-111111111111","IsActiveEntity":true}]}
Source: real answer of the request above (shortened: the date and correlation headers are left out), compare SAP Gateway: Cross-Site Request Forgery Protection (SAP NetWeaver 7.5, an older page)
The answer holds no x-csrf-token header, so a script that insists on reading one fails against this server although
the request worked. A client written for SAP Gateway therefore needs to treat the token as something a particular
server may or may not send. A write to the lab went through without a token. The draft-enabled Claims creates a draft
first:
curl -s -i -X POST -H 'Content-Type: application/json' -d '{"title":"Parking","amount":8.5}' "$B/Claims"
HTTP/1.1 201 Created
OData-Version: 4.0
location: Claims.drafts(100653e5-8cd7-402b-9a42-fc4f8ff91fa6)
Content-Type: application/json; charset=utf-8; ieee754compatible=true
{"@odata.context":"$metadata#Claims/$entity","ID":"100653e5-8cd7-402b-9a42-fc4f8ff91fa6","createdAt":"2026-11-13T14:54:20.673Z","createdBy":"anonymous","modifiedAt":"2026-11-13T14:54:20.673Z","modifiedBy":"anonymous","title":"Parking","status":"N","amount":"8.50","HasActiveEntity":false,"DraftMessages":[],"HasDraftEntity":false,"IsActiveEntity":false}
Source: real answer of the request above (shortened: the date, correlation and connection headers are left out), draft behavior described at CAP Getting Started
The answer is 201 Created, IsActiveEntity is false and the location header names a draft. The createdBy value
anonymous comes from the mocked authentication of the lab. This request is the shape of the SAP Gateway one above,
but the behavior of an SAP system with the token is only described here.
SAP Gateway
SAP Gateway is the part of an ABAP system that publishes OData services. The SAP Learning course describes two deployment options. In the hub deployment, a front-end server is the single access point to one or more back-end systems and routes to them; the course recommends it for SAP Business Suite. In the embedded deployment, SAP Gateway runs in the back-end server itself, so development has direct access to the data definitions, and the course names it the recommended setup for SAP S/4HANA. A system alias defines how the front-end server reaches a back end; in an embedded deployment, only the flag for the local gateway is set.
Two tools matter to a newcomer, one for building and one for publishing.
-
The SAP Gateway Service Builder, transaction
SEGW, is the tool for V2 services: a developer creates a project, defines entity types and entity sets, and implements the read and write methods in ABAP. The course describes it as the tool to register, manage and test OData services. -
The transaction
/IWFND/MAINT_SERVICE("Activate and Maintain Services") lists every V2 service registered in the system, in three areas: the service catalog, the ICF nodes (which include a service test) and the system aliases. A service is added with the Add Service function, with the system alias and the external service name, and is then active at its ICF node. -
V4 services are published through a service group and managed with
/IWFND/V4_ADMIN. In ABAP development, a RAP service binding creates the V4 service; the page on Projections, Service Definitions and OData V4 Bindings says that on premise the binding publishes only locally for tests and that the SAP Gateway tools activate the service in other systems.
Requires an SAP system; not run here:
1. Transaction /IWFND/MAINT_SERVICE, choose Add Service.
2. Enter the system alias (for an embedded system the alias that points to the local system) and the external service name.
3. Choose Get Services, select the service, choose Add Selected Services.
4. Check that the ICF node is active, then call the service root and the $metadata address in a browser.
Source: sequence from SAP Learning: Configuring SAP Gateway and the SAP help topic Activate OData Services; the button names and their order are not read from the help topic itself and may differ in your release, so check them on your system
New development, and the services of SAP’s own newer apps, are V4 and are built with RAP or CAP. The V2 tooling remains
the one you meet when you maintain older services. A book written before RAP may present SEGW and hand-written
ABAP methods as the way to write a service; today an ABAP developer can also define a business object and expose it with a
service definition and a binding, which needs no hand-written Gateway project.
Summary
-
OData is a standard for reading and changing data with HTTP; V2 and V4 are different protocols, and SAP Gateway has supported V2 since AS ABAP 7.00 and V4 since 7.50, with V3 not supported.
-
The
$metadataaddress describes a service; the options$filter,$select,$orderby,$top,$skip,$countand$expandshape the answer, and$batchpacks requests into one trip. -
V4 answers carry
valueand@odata.count; V2 answers used,resultsand__count. -
Writes to SAP Gateway need a CSRF token fetched with
x-csrf-token: fetchand sent with the session cookie; the lab’s CAP service sent none. -
SEGWand/IWFND/MAINT_SERVICEbuild and publish V2 services; V4 uses service groups and/IWFND/V4_ADMIN.
Try it
To do this by hand, follow the guide Query OData Services: it runs OData queries against a local service.