How-toSAP S/4HANAInventory Management / OData APIs

API_MATERIAL_STOCK_SRV: reading SAP stock by plant and storage location with OData

Question
api_material_stock

Also searched as

  • API_MATERIAL_STOCK_SRV
  • sap odata material stock api
  • A_MatlStkInAcctMod example

Short answer

API_MATERIAL_STOCK_SRV is the released OData V2 service for reading material stock in S/4HANA. Its main entity set, A_MatlStkInAcctMod, returns stock quantities by material, plant, storage location, batch and stock type. Activate the service, authorize a technical user and filter with $filter on Material and Plant.

Applies to: SAP S/4HANA on-premise and private cloud (service path /sap/opu/odata/sap/API_MATERIAL_STOCK_SRV). Check the SAP Business Accelerator Hub for the exact entities in your release.

Call the material stock service

  1. 1Check the service in the SAP Business Accelerator Hub (api.sap.com) to confirm the entity sets available for your release.
  2. 2Activate the service in /IWFND/MAINT_SERVICE on the gateway (or the embedded gateway): Add Service, select API_MATERIAL_STOCK_SRV, and assign the system alias and package.
  3. 3Create a technical user and a role that includes the service in the authorization defaults (S_SERVICE) and read access to the stock data for the plants it may see.
  4. 4Test in /IWFND/GW_CLIENT with a request such as the example below, using HTTP GET and the header Accept: application/json.
  5. 5From your integration, authenticate (basic over TLS or OAuth/SAML as set up by your basis team) and call the same URL with $filter, $select and $top.
  6. 6For writes you do not use this service: stock changes happen through goods movements (material document APIs), never by posting to the stock entities.

Entities and sample requests

The commonly used entity set is A_MatlStkInAcctMod, which exposes one row per material, plant, storage location, batch, special stock indicator and stock type, with the quantity in the base unit. Typical key properties are Material, Plant, StorageLocation, Batch, Supplier, Customer, WBSElementInternalID, SDDocument, SDDocumentItem, InventorySpecialStockType and InventoryStockType, and the quantity field is MatlWrhsStkQtyInMatBaseUnit with MaterialBaseUnit. A separate entity set exposes the material level view. Confirm the names in the metadata document before coding.

Always filter. An unfiltered call on a production system returns a large result and will time out or hit the server's limits. Combine $filter on Material and Plant with $select for the columns you need and use $top and $skip for paging.

GET /sap/opu/odata/sap/API_MATERIAL_STOCK_SRV/$metadata
GET /sap/opu/odata/sap/API_MATERIAL_STOCK_SRV/A_MatlStkInAcctMod
  ?$filter=Material eq 'FG-1000' and Plant eq '1010'
  &$select=Material,Plant,StorageLocation,Batch,InventoryStockType,MatlWrhsStkQtyInMatBaseUnit,MaterialBaseUnit
  &$format=json

Stock types and how to interpret quantities

Quantities are split by stock type. Unrestricted use, quality inspection and blocked stock are separate rows, identified by InventoryStockType. If your integration shows less than the MMBE total, you are probably filtering on one type, or you are not including special stock such as consignment or project stock.

Remember that the service reads the current stock, not the stock at a past date. For historical balances use the material document tables or an analytical service intended for stock over time. Also keep in mind that unconfirmed goods movements and reservations are not stock, so availability (ATP) is a different calculation.

Authorization, performance and common errors

A 403 or 401 usually means the user lacks the service authorization or the role has no start authorization for the service, while empty results with a 200 often mean the plant authorization (M_MSEG_WWE or related) hides the data. A 404 means the service is not activated on the gateway you are calling, or the system alias is wrong. Check the gateway error log in /IWFND/ERROR_LOG for the real cause.

For performance, avoid polling thousands of materials. Call once per plant with a filter or use a delta mechanism: subscribe to material document events in your integration layer and refresh only materials that moved. Keep API traffic off the main application server when you can by using a dedicated gateway or a rate limit.

Try it here, free

SAP OData Service URL Builder

Open full tool

Generate a correct SAP Gateway OData V2 request URL with query options, test it in a browser or REST client, then reuse it in your code.

Common pitfalls

  • !Calling without a filter and getting timeouts or truncated pages.
  • !Reading the total from one stock type and calling it 'available'.
  • !Using a dialog user for integration: use a technical user with a minimal role.
  • !Hard coding entity names without reading $metadata, which differ between releases.
  • !Trying to change stock by posting to the entity set: use goods movement APIs.

How an ERP-grounded AI assistant handles this

If you want people to ask 'how much of part X is in plant 1010' without writing OData, a read-only assistant over this API or the underlying tables can translate the question into a filtered call and show the call it made. With ERPray the service user is limited to the roles you grant, and on-prem deployment keeps the stock figures and the model inside your network.

Frequently asked questions

What is API_MATERIAL_STOCK_SRV?

It is a released SAP OData V2 service that exposes material stock data in S/4HANA. You use it to read stock by material, plant, storage location, batch and stock type from external systems, dashboards or integrations.

How do I filter stock for one plant and storage location?

Append $filter=Plant eq '1010' and StorageLocation eq '0001' to the A_MatlStkInAcctMod request, add Material when you can, and use $select to limit columns. Use $top and $skip to page large results.

Why does the service return 404?

The service is probably not activated in /IWFND/MAINT_SERVICE on the system you call, or the URL or system alias is wrong. Check the gateway error log and the service catalog on that host.

Can I update stock through this API?

No. It is for reading. Stock changes come from goods movements posted with material document APIs or BAPIs, which create documents and update quantities and values consistently.

Related

How-to

How to Use BAPI_TRANSACTION_COMMIT Correctly

SAP's BAPI programming model separates business logic from the database commit on purpose, so a caller must explicitly call BAPI_TRANSACTION_COMMIT after a successful BAPI - usually with WAIT = 'X' - or the created object never actually persists. Skipping this step, or forgetting WAIT, is the most common reason a BAPI 'runs fine' but nothing gets saved.

How-to

SAP SE16N vs SQVI: Table Browser vs Ad Hoc Query

SE16N is a single-table browser: fast filtering, formatted display, and export from one table at a time, with an optional edit function for authorized users. SQVI (Quick Viewer) builds ad hoc queries that join multiple tables without writing ABAP, and can be saved as a reusable report. Use SE16N for a quick look at one table; use SQVI when you need data joined across two or more tables.

How-to

How to Read MD04 Exception Messages in SAP

The exception indicator in MD04 flags procurement elements whose date or quantity no longer matches the current demand situation - it is SAP's built-in change signal, read element by element in the detail screen, not just the header icon. Triage by exception group first, then decide to reschedule, cancel, or accept each one.

Error fix

SAP M7 021: Deficit of SL Stock Quantity

M7 021 fires during a goods issue, delivery, or confirmation when SAP checks the unrestricted-use (SL) stock for the exact plant, storage location and batch combination and finds less than what you are trying to post out. Fix the entry (batch, storage location, quantity) first; only enable negative stock or downgrade the message to a warning if the business genuinely needs to post ahead of a receipt.

Error fix

SAP IDoc Status 51: Application Document Not Posted

Status 51 tells you the IDoc reached the application layer and failed to post, but the status text itself is not the error - the real cause sits in the status record's long text or the linked application log. Fix the underlying data or configuration, then reprocess the IDoc through BD87 rather than editing the status.

Error fix

SAP Short Dump TIME_OUT: Maximum Runtime Exceeded

A TIME_OUT runtime error means a dialog (or RFC) work process ran longer than the maximum allowed runtime, controlled by profile parameter rdisp/max_wprun_time (600 seconds by default). The fix is almost always to move the long-running work to background processing or tune the underlying program, not to raise the timeout.

AI for ERP

AI for SAP S/4HANA, Running On-Prem or in Your Private Cloud

Run AI on SAP S/4HANA without sending ERP data to a public API. On-prem and private-cloud architecture, CDS views, OData, and honest deployment trade-offs.

AI for ERP

A Private LLM Grounded on Your ERP Data

How a private LLM answers questions on your ERP data: RAG plus text-to-SQL, role-based permissions inherited from the ERP, and where each fits.

Stuck on SAP S/4HANA?

Talk to engineers who work inside SAP S/4HANA every week, and who build private AI that answers these questions from your own ERP data.