Wednesday, 17 September 2025

How to Create a REST API in SAP ABAP — Step-by-step Guide

How to Create a REST API in SAP ABAP — Step-by-step Guide

Summary: This tutorial shows how to build a REST API in SAP ABAP by creating a handler class, configuring SICF service, registering endpoints with cl_rest_router, implementing endpoint logic (example GET method), and testing the API. Based on an internal implementation reference.

Why expose REST APIs from SAP?

REST APIs allow SAP systems to integrate with web, mobile, and external services using standard HTTP and JSON payloads. Implementing REST endpoints in ABAP provides secure, reusable, and maintainable integration points for modern applications.

Prerequisites

  • Access to an SAP system with authorization to create classes (SE24) and SICF services (SICF).
  • Familiarity with ABAP object-oriented concepts and basic SAP transaction codes.
  • ABAP classes CL_REST_RESOURCE, CL_REST_ROUTER and utilities like /UI2/CL_JSON.

High-level overview (4 steps)

  1. Create an API handler class (e.g. ZCL_API_HANDLER).
  2. Register endpoint templates using cl_rest_router.
  3. Create and activate an SICF service (e.g. /sap/bc/zapi01).
  4. Implement endpoint logic in endpoint classes and test the API.

Step 1 — Create the API handler class

Create class ZCL_API_HANDLER in SE24, implement CL_REST_HTTP_HANDLER, and register endpoints.


DATA(lo_router) = NEW cl_rest_router( ).
lo_router->attach(
  EXPORTING
    iv_template      = '/ZFLIGHTLIST'
    iv_handler_class = 'ZCL_FLIGHT01' ).
ro_root_handler = lo_router.

Step 2 — Create and configure the SICF service

In SICF, create a new service node (e.g. ZAPI01) and attach your handler class ZCL_API_HANDLER. Activate the service.

Step 3 — Create endpoint class

Make class ZCL_FLIGHT01 inheriting from CL_REST_RESOURCE. Redefine GET/POST methods.


METHOD if_rest_resource~get.
  " Parse JSON request
  DATA(lo_entity) = mo_request->get_entity( ).
  DATA(lv_data)   = lo_entity->get_string_data( ).
  DATA: ls_params TYPE ty_flight.

  /ui2/cl_json=>deserialize( json = lv_data changing data = ls_params ).

  " Read SFLIGHT
  IF ls_params IS NOT INITIAL.
    SELECT * FROM sflight INTO TABLE @DATA(lt_flight)
      WHERE carrid = @ls_params-carrid
        AND connid = @ls_params-connid.
  ELSE.
    SELECT * FROM sflight INTO TABLE @lt_flight.
  ENDIF.

  " Build JSON response
  IF lt_flight IS NOT INITIAL.
    /ui2/cl_json=>serialize( data = lt_flight RECEIVING r_json = lv_result ).
    lv_result = '{"Status":"OK","Message":"SUCCESS","Data":' && lv_result && '}'.
  ELSE.
    lv_result = '{"Status":"ERROR","Message":"DATA NOT FOUND"}'.
  ENDIF.

  lo_response->set_string_data( iv_data = lv_result ).
  lo_response->set_header_field(
    EXPORTING iv_name = 'CONTENT-TYPE'
              iv_value = 'APPLICATION/JSON' ).
ENDMETHOD.

Step 4 — Test the API

Test with Postman or cURL. Example:


curl -u username:password -X GET "https://your-sap-host/sap/bc/zapi01/ZFLIGHTLIST" \
-H "Content-Type: application/json" \
-d '{"carrid":"AA","connid":"0017"}'

Security & Best Practices

  • Always use HTTPS and proper authentication.
  • Validate and sanitize inputs.
  • Return correct HTTP status codes.
  • Use pagination for large datasets.
  • Log requests and errors responsibly.

FAQ

Where do I register endpoints?

Inside cl_rest_router in the handler class.

Which transaction creates the web service?

SICF is used to create and activate the service node.

Which ABAP class handles JSON?

/UI2/CL_JSON is commonly used for JSON serialization and deserialization.

No comments:

Post a Comment

Belajar SAP ABAP RAP

  Belajar SAP ABAP RAP: Pengenalan dan Konsep Dasar Restful ABAP Programming Model Kalau kamu seorang ABAPer yang mulai terjun ke dunia SAP...