OData V4 Part 1: The OData V4 Contract — Metadata, Keys, Navigation, Queries
Free sample lesson · open to everyone
Read an OData V4 service like a contract: service document, $metadata, entity sets, keys (including IsActiveEntity for draft), navigation, and the query options $select, $filter, $orderby, $top, $skip, $count, $expand and $search.
In this lessonShow sectionsHide sections
Visual overview
The situation: a real project scenario
A reporting tool pulled purchase requests via OData without $select and with $expand=_Item on every call. Each response contained all header fields plus all items, and nightly extraction took hours and sometimes timed out.
After reading the $metadata with the integration team, they changed the calls to $select only the six needed fields, filtered on LastChangedAt for delta loads, paged with $top/$skip, and expanded items only when needed. Extraction time fell to minutes, and the load on the backend dropped accordingly.
Explanation
This series covers OData V4 across six parts: this lesson covers the OData V4 contract; Part 2 covers publishing a RAP service with an OData V4 binding; Part 3 covers testing CRUD requests, payloads and responses; Part 4 covers actions, functions and batch; Part 5 covers troubleshooting HTTP errors; and Part 6 covers ETags and optimistic concurrency.
OData is a standard REST protocol for typed business data over HTTP. RAP generates the OData service directly from the projections and behavior definitions, so no HTTP handlers need to be written by hand — but understanding the contract itself is still essential for testing, integrating and troubleshooting.
A RAP OData V4 UI service URL looks like /sap/opu/odata4/sap/zui_purchreq_o4/srvd/sap/zui_purchreq/0001/, where zui_purchreq_o4 is the binding, zui_purchreq is the service definition, and 0001 is the service version. A GET on the root returns the service document, which lists the entity sets.
$metadata is the actual contract. A GET on …/$metadata returns XML describing the EntityType structure (Property entries with name, type, nullable, maxLength, plus Key), the EntitySet collections that get queried (such as PurchaseRequest), NavigationProperty relationships (like _Item) along with their NavigationPropertyBinding to target sets, Action and Function operations (such as approve), and Annotations covering UI and semantics vocabularies for Fiori. Every client relies on this contract, and changing it can break clients, a concern covered further in Service Definitions Part 3.
Keys and entity addressing follow a consistent pattern: a single entity is addressed as …/PurchaseRequest(RequestUUID=<uuid>), and with draft enabled, as …/PurchaseRequest(RequestUUID=<uuid>,IsActiveEntity=true) for the active version or false for the draft. Navigation looks like …/PurchaseRequest(…)/_Item.
Query options shape what comes back. $select=RequestID,Description limits the response to those properties. $filter=Status eq 'S' and TotalAmount gt 1000 uses operators like eq, ne, gt, ge, lt, le, and, or, not, and functions like contains(Description,'chair') and startswith(…). $orderby=CreatedAt desc sets the sort order. $top=20&$skip=40 handles paging. $count=true returns the total number of matching rows as @odata.count. $expand=_Item($select=ItemNo,Quantity;$filter=Quantity gt 5) includes related entities with their own nested options. And $search=chair performs free-text search where the entity supports it.
The JSON response for a list looks like { "@odata.context": "…$metadata#PurchaseRequest", "@odata.count": 57, "value": [ { "RequestID": "4711", … } ] }, while single entities are returned directly as an object, carrying "@odata.etag" when an ETag is defined, as covered in Part 6.
In one minute, V4 versus V2: V4 is the current standard for RAP, using JSON by default, a richer query syntax with nested $expand options, cleaner actions and functions, and better batch support, while V2 remains relevant mainly for older clients, covered separately in the OData V2 series.
In practice: open the service binding, copy the service URL, and append $metadata; find the EntityType PurchaseRequestType, its keys, and the _Item navigation; and run the queries from the code example in a browser or REST client. With that, $metadata can be read and any read query written by hand.
OData V4 expert API lab
OData V4 metadata is the API contract
Service document
Entry point: available entity sets
$metadata
Entity types, keys and operations
Entity set
Queryable Request resources
Illustrative GET request
/Requests?$select=RequestId,StatusReturn only the named properties that exist in the service contract.
A non-draft entity need not expose the IsActiveEntity discriminator.
Code example
### Service root (UI binding, version 0001)### {root} = /sap/opu/odata4/sap/zui_purchreq_o4/srvd/sap/zui_purchreq/0001 ### 1. Service document and contractGET {root}/ # service document: lists the entity sets exposed by this serviceGET {root}/$metadata # EDMX contract: entity types, keys, nav properties, annotations ### 2. List with only needed fields, filter, order, paging and total countGET {root}/PurchaseRequest?$select=RequestID,Description,Status,TotalAmount,CurrencyCode &$filter=Status eq 'S' and TotalAmount gt 1000 # filtering happens server-side, not after the data arrives &$orderby=TotalAmount desc &$top=20&$skip=0&$count=true # $top/$skip = paging window, $count = total rows for a pager UI ### 3. One entity (draft-enabled BO: two key parts)GET {root}/PurchaseRequest(RequestUUID=4c2a…-…,IsActiveEntity=true) # IsActiveEntity picks the active record, not a draft ### 4. Navigation and expand with nested optionsGET {root}/PurchaseRequest(RequestUUID=4c2a…,IsActiveEntity=true)/_Item # follow the composition down to the child itemsGET {root}/PurchaseRequest?$select=RequestID &$expand=_Item($select=ItemNo,Quantity,Price;$filter=Quantity gt 5;$orderby=ItemNo) # one round trip instead of N+1 calls ### 5. Text functions and searchGET {root}/PurchaseRequest?$filter=contains(Description,'chair') # substring match on one named fieldGET {root}/PurchaseRequest?$search=chair # free-text search across indexed fields ### Response (shortened)# {# "@odata.context": "$metadata#PurchaseRequest(RequestID,Description,Status,TotalAmount,CurrencyCode)",# "@odata.count": 57,# "value": [# { "RequestID": "4711", "Description": "Office chairs", "Status": "S",# "TotalAmount": 3600.00, "CurrencyCode": "EUR" }# ]# }Watch out
Common mistakes
- Querying without $select and transferring every field.
- Forgetting IsActiveEntity for draft-enabled entities and getting "not found".
- Expanding to-many navigation everywhere.
- Paging without a stable $orderby.
- Treating $metadata as optional instead of the contract.
- Using V2 syntax (e.g. substringof) in V4 requests.
Do this
Best practices
- Start every integration by reading $metadata.
- Always $select; filter server-side; page with $top/$skip and $orderby.
- Use $count=true for UIs that show totals.
- Use nested $expand options to limit expanded data.
- Use V4 for new RAP services; V2 only for legacy clients.
In the interview
Interview angle
Questions: "What is in OData $metadata?", "Explain $select, $filter and $expand", "How do you address a draft instance in OData V4?"
Strong answer: $metadata defines entity types, keys, properties, entity sets, navigation, actions and annotations — the contract. Query options: $select for fields, $filter with operators and functions, $orderby, $top/$skip paging, $count, $expand with nested options, $search. Draft entities include IsActiveEntity in the key. Mention the RAP V4 URL anatomy (binding, service definition, version).
Practice this lesson
Quick check
0 of 3 questions attempted
Question 1 of 3
A third-party integration requests every property and expands three to-many navigations on each call. What should change first?
Question 2 of 3
Which OData V4 query returns only two fields of submitted requests with a total count?
Question 3 of 3
What is the best source for the public entity-set and property names of an OData V4 service?
Practise this in an interview
Answer in your own words and get instant feedback, the way an interviewer would judge it.
1Explain the OData V4 contract: what does $metadata contain and which query options exist?
2Why should you inspect OData V4 metadata before manually constructing CRUD requests?
See it in code
Each example starts from a requirement, shows the design, then the code step by step with a plain-language explanation of every part.
Was this lesson helpful?
Keep exploring
Interview questions on this topic
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.