You are on page 1of 8

API NOTES STEP BY STEP CREATED BY - BIJAYA KRUSHNA ROUT

LINKEDIN PROFILES- https://www.linkedin.com/in/bijayasap/


API(Application Programming Interface).
In the context of SAP ABAP, an API (Application Programming Interface) refers to a set of rules and
protocols that allows different software systems to communicate and interact with each other. APIs
provide a standardized way for applications to access and use the functionality provided by other
systems or services.

APIs define the methods, data formats, and conventions that developers can use to interact with a
software system, service, or platform.

API stands for Application Programming Interface. It is a set of rules and protocols that allows
different software applications to communicate with each other. APIs define how software
components should interact, specifying the methods, parameters, data formats, and authentication
mechanisms to be used.

In SAP, there are various APIs available that provide access to different functionalities and data
within the SAP system. These APIs enable developers to integrate external systems with SAP,
automate business processes, retrieve and manipulate data, and perform various other operations

SAP ABAP offers various APIs that developers can use to integrate SAP systems with external
systems, extend SAP functionalities, or develop custom applications. Some common SAP ABAP APIs
include:

BAPI (Business Application Programming Interface): BAPIs are pre-defined function modules that
provide access to SAP business objects and processes. They allow external systems to create, read,
update, and delete data in SAP systems.

BAPI (Business Application Programming Interface): BAPIs are a set of standard interfaces provided
by SAP to access and modify data in SAP systems. They encapsulate SAP business logic and can be
called from external systems or within SAP programs.

SAP Business Application Programming Interface (BAPI): Provides a set of standard interfaces to
access and manipulate SAP business objects.

RFC (Remote Function Call): RFC enables communication between SAP systems or between SAP and
external systems. It allows the execution of remote function modules and the exchange of data
between systems.

RFC (Remote Function Call): RFC enables communication between SAP systems and external systems
through remote function calls. It allows programs running on different systems to exchange data and
trigger remote function modules.

IDoc (Intermediate Document): IDoc is a standard format for exchanging data between different SAP
systems or between SAP and external systems. It defines the structure and content of the data being
exchanged.
Web Services: SAP ABAP supports the creation and consumption of web services, which are APIs that
use standard web protocols such as HTTP and XML to enable communication between systems over
a network.

OData (Open Data Protocol): OData is a RESTful API protocol that allows easy and standardized
access to data from SAP systems. It enables data retrieval, modification, and manipulation using
HTTP-based operations.

OData: OData is a protocol that allows for the creation and consumption of RESTful APIs for data
access and manipulation. SAP provides OData services that expose SAP system data to external
applications and enable easy integration.

SAP OData API: Allows accessing SAP data and functionality through the Open Data Protocol (OData),
a web protocol for building and consuming RESTful APIs.

SAP NetWeaver RFC SDK: Enables remote function calls to SAP systems using Remote Function Call
(RFC) protocol.

SAP NetWeaver Gateway: SAP NetWeaver Gateway is a technology that provides a framework for
building and consuming OData services. It allows developers to expose SAP system data and
business processes as OData services.

SAP Cloud Platform APIs: SAP Cloud Platform offers various APIs for different services and
capabilities, such as integration, analytics, mobile services, machine learning, and more. These APIs
enable developers to extend and integrate SAP applications in the cloud.

SAP Cloud Platform APIs: Provide access to various services and capabilities offered by the SAP Cloud
Platform, such as authentication, storage, messaging, etc.

SAP Integration Suite APIs: Enable integration and connectivity between SAP and other systems,
providing capabilities like message processing, transformation, and routing.

These are just a few examples of the APIs available in SAP ABAP. Depending on your specific
requirements, you can choose the appropriate API to integrate SAP systems with other applications,
services, or platforms. SAP documentation and development resources provide more details and
examples on how to use these APIs effectively.

These APIs offer different methods and protocols for integration, and their usage depends on the
specific requirements and technologies involved in the integration scenario. Developers can leverage
these APIs to build custom applications, integrate SAP with external systems, or extend SAP
functionality. The availability and usage of APIs may vary depending on the SAP product and version
being used.
API SYNTAX :-

The syntax of an API depends on the specific API being used. Different APIs may have different
syntaxes based on their purpose and functionality. Here is a general example of API syntax:

API Endpoint: APIs typically have a base URL or endpoint that represents the location or resource
being accessed. It usually starts with "https://" followed by the domain and path.

HTTP Method: APIs use HTTP methods to indicate the type of operation to be performed. The
commonly used methods are GET, POST, PUT, PATCH, and DELETE. The method is usually specified in
the API request.

Headers: API requests often include headers that provide additional information about the request.
Headers can include authorization tokens, content type, language, etc.

Request Parameters: APIs may require certain parameters to be passed in the request. These
parameters could be query parameters, path parameters, or request body parameters. The specific
parameters depend on the API and its functionality.

Response Format: APIs return responses in a specific format, such as JSON, XML, or HTML. The
response format is usually specified in the request headers or in the API documentation.

Response Status Codes: APIs use HTTP status codes to indicate the success or failure of a request.
Common status codes include 200 (OK), 201 (Created), 400 (Bad Request), 401 (Unauthorized), 404
(Not Found), and so on.

Response Data: APIs return data in the response body. The structure and format of the response
data depend on the API and the specific request. It could be a single object, an array of objects, or
other data structures.

It's important to refer to the documentation or specification of the specific API you are working with
to understand its syntax, required parameters, and expected responses. The documentation will
provide detailed information on how to construct API requests and interpret the responses for that
particular API.

HOW TO CREATE API

Creating an API involves several steps and considerations. Here is a general overview of the process:

Define the Purpose: Determine the purpose and functionality of the API. Identify the specific data or
services that the API will expose.

Design the API: Define the endpoints, request/response formats, and data structures. Determine the
authentication and authorization mechanisms, error handling, and any additional features required.
Choose the Technology Stack: Select the technology stack that best suits your requirements. This
may include choosing a programming language, framework, and any necessary tools or libraries.

Develop the API: Implement the API endpoints and the underlying logic using the chosen technology
stack. This involves writing the code, handling requests, processing data, and generating responses.

Implement Security: Implement security measures to protect the API, such as authentication (e.g.,
OAuth, API keys) and authorization (e.g., role-based access control).

Test the API: Perform thorough testing to ensure the API functions as expected. Test different
scenarios, input validations, error handling, and performance under load.

Document the API: Create comprehensive documentation that explains how to use the API, including
endpoint details, request/response formats, required parameters, and examples. This
documentation helps developers understand and integrate with your API.

Deploy the API: Deploy the API to a suitable environment, such as a web server, cloud platform, or
containerized environment. Configure the necessary infrastructure components to ensure the API is
accessible to clients.

Monitor and Maintain: Continuously monitor the API for performance, security, and availability.
Address any issues that arise and make updates or improvements as necessary.

It's important to note that creating an API can be complex and may require expertise in software
development, web technologies, and API design principles. Consider consulting with experienced
developers or utilizing existing frameworks and tools to simplify the process.

CREATE NEW API SAP ABAP CODE

Define the API's interface:

Create a new interface using the transaction code SE24.

Define the methods and parameters that represent the functionality of your API.

Save and activate the interface.

Implement the API's interface:

Create a new ABAP class that implements the interface you defined in the previous step.

Implement the methods of the interface with the desired logic for your API.

Save and activate the class.

Define the API's service:

Create a new service definition using the transaction code SICF.

Assign a unique name and description to the service.

Specify the handler class and the entry point method for your API.

Activate the service definition.


Assign the service to a package and a URL path:

Create a package using the transaction code SE80 or SE21.

Assign the service to the package using the transaction code SICF.

Specify the URL path for accessing your API.

Activate the changes.

Test the API:

Use a tool like Postman or a web browser to send requests to the URL path you defined for your API.

Verify that the API is functioning correctly by checking the responses and behavior of the
implemented methods.

Please note that the specific implementation details and steps may vary based on your requirements
and the version of SAP you are using. It's recommended to consult the SAP ABAP documentation and
relevant resources for detailed instructions and best practices for creating APIs in your specific SAP
ABAP environment.

API CREATE EXAMPLE:-

TRY.

* Code that may raise exceptions or errors

* API URL ENTRY.

DATA(lv_url) = |https://airport-info.p.rapidapi.com/airport?iata=LAX|.

DATA(o_client) TYPE REF TO if_http_client.

* CREATE HTTP OBJECT.

CL_HTTP_CLIENT=>create_by_url( EXPORTING URL = lv_url

IMPORTING CLIENT = o_client ).

IF o_client IS BOUND.

* SET HTTP METHOD.

o_client->request->set_method( if_http_request=>c0_request_method_get ).

* SET HEADERS

DATA(lr_headers) TYPE REF TO if_http_request_header.

lr_headers = o_client->request->get_header_fields( ).

lr_headers->set_header_field( name = 'Key1' value = 'Value1' ).

lr_headers->set_header_field( name = 'Key2' value = 'Value2' ).

...
* SEND REQUEST

DATA(lo_response) TYPE REF TO if_http_response.

lo_response = o_client->send( ).

* Process the response

ENDIF.

* Additional code

CATCH cx_root INTO DATA(lx_exception).

* Exception handling code

WRITE: 'An exception occurred:', lx_exception->get_text( ).

ENDTRY.

EXAPLAIN STEP BY STEP API SYNTAX:-

* API URL ENTRY.

If you're looking for an example of how to specify the API URL in SAP ABAP, you can use a variable to
store the URL and then pass it to the appropriate method. Here's an example:

DATA: lv_url TYPE string.

lv_url = 'https://api.example.com/endpoint'.

In the code snippet above, we define a variable lv_url of type string to store the API URL. You can
replace 'https://api.example.com/endpoint' with the actual URL of the API you want to call.

After storing the URL in the variable, you can use it as needed in your code. For example, you can
pass it to the method that performs the HTTP request.

Please note that the provided code snippet is a basic example. You will need to implement additional
logic to handle the request, process the response, and perform any additional operations required
by your specific use case.

* CREATE HTTP OBJECT

To create an HTTP object in SAP ABAP, you can use the cl_http_client class. Here's an example of
how to create an HTTP object:

Copy code

DATA: o_client TYPE REF TO cl_http_client.

CREATE OBJECT o_client.

In the above code snippet, we declare a reference variable o_client of type cl_http_client. The
CREATE OBJECT statement creates an instance of the cl_http_client class and assigns it to the
o_client variable.
After creating the HTTP object, you can use it to perform various HTTP operations, such as setting
headers, making requests, and handling responses.

Please note that the provided code snippet is a basic example of creating an HTTP object. Depending
on your requirements, you may need to provide additional configuration and parameters to the
CREATE OBJECT statement, such as specifying the target URL or authentication credentials.

Make sure to refer to the SAP ABAP documentation and relevant resources for detailed instructions
and best practices when working with HTTP objects in your specific SAP ABAP environment.

* SET HTTP METHOD.

To set the HTTP method for an HTTP request in SAP ABAP, you can use the SET_METHOD method of
the IF_HTTP_REQUEST interface. Here's an example of how to set the HTTP method:

o_client->request->set_method( if_http_request=>co_request_method_get ).

In the above code snippet, o_client refers to the HTTP client object you previously created. The
request attribute of the HTTP client object gives you access to the IF_HTTP_REQUEST interface. The
SET_METHOD method allows you to specify the desired HTTP method.

In the example above, the HTTP method is set to GET using the co_request_method_get constant
from the IF_HTTP_REQUEST interface. You can also use other constants such as
co_request_method_post, co_request_method_put, co_request_method_delete, etc., depending
on the specific HTTP method you want to use.

After setting the HTTP method, you can proceed with setting headers, sending the request, and
handling the response based on your specific use case.

Please note that the provided code snippet assumes you have already created the HTTP client object
(o_client) and that it is properly configured with the target URL and other required parameters.

* SEND REQUEST

To send an HTTP request in SAP ABAP, you can use the SEND method of the IF_HTTP_CLIENT
interface. Here's an example of how to send a request:

DATA: lo_response TYPE REF TO if_http_response.

lo_response = o_client->send( ).

In the above code snippet, o_client refers to the HTTP client object you previously created. The SEND
method is called on the HTTP client object to send the request.

The SEND method initiates the HTTP request and returns an instance of the IF_HTTP_RESPONSE
interface, which represents the response received from the server. You can assign the response
object to a variable (lo_response in the example) to further process the response.

After sending the request, you can use the response object to access the response data, status code,
headers, and perform other operations such as reading the response content or checking the status
of the request.
Please note that the provided code snippet assumes you have already created the HTTP client object
(o_client) and that it is properly configured with the target URL, method, headers, and any required
parameters.

Make sure to handle any exceptions or errors that may occur during the request and response
handling process.

You might also like