Skip to Content

Call an External API

test
0 %
Call an External API
Details
// Explore More Tutorials

Call an External API

04/10/2019

Point to an external API and display its output in the console.

You will learn

  • How to call an external API in an ABAP Class, using a specific destination for a communication arrangement


You will create a new destination for an existing communication arrangement, specifying the URL for an external API, user, password, and authentication.
You will then create a class that calls the API and displays the output from it in the console.

Throughout this tutorial, objects name include the suffix XXX. Always replace this with your group number or initials.

Step 1: Open the dashboard for your project

In ADT, select your project and choose Properties > ABAP Development > System URL.

Image depicting CJ-step1a-propertiesImage depicting CJ-step1b-url
Log on to answer question
Step 2: Optional: Check the communication system

Enter the URL in the browser and add /ui.
In the dashboard that appears, choose Create a Communication System.
The most important settings are:

  • Host Name
  • OAuth 2.0 Settings
  • User for Outbound Communication
Image depicting CJ-step2-comm-system-large
Log on to answer question
Step 3: Optional: Check the communication arrangement

Go back and choose Create a Communication Arrangement.
The most important settings are:

  • Scenario ID : SAP_COM_0276 (pre-delivered by SAP)
  • Service Instance Name: EXTERNAL_API_XXX
  • Service URL
Image depicting step3-comm-arr
Log on to answer question
Step 4: Create an ABAP class

Now, you will create an ABAP class that will call your destination, and which you can run in the console.

  1. In the ABAP Development Tools (ADT), in the Package Explorer, select your package and choose New > ABAP Class from the context menu:

    Image depicting step-4a-create-class
  2. Enter a name and description for your class and choose Next. Remember to change XXX to your group number:

    Image depicting step-4b-name-class
  3. Choose or create a transport request, then choose Finish:

    Image depicting step-4c-class-transport-request

The class is displayed in a new editor:

Image depicting step-4d-class-editor
Log on to answer question
Step 5: Add an interface statement

Now you will implement the class.
Add the following interfaces statement to the public section:

  INTERFACES if_oo_adt_classrun.

This enables you to run the class in the console.

Log on to answer question
Step 6: Implement the method
  1. Add the method implementation below for the method if_oo_adt_classrun~main. and wrap it in an exception.
    • i_name = the specific destination defined in the destination service instance (in the SAP Cloud Cockpit) EXTERNAL_API_XXX
    • i_service_instance_name = your communication arrangement (in the dashboard):

    Image depicting step6a-service-instance-name-in-Comm-Arr

        TRY.
            DATA(lo_destination) = cl_http_destination_provider=>create_by_cloud_destination(
              i_name                  = 'Z_CHUCKNORRIS_XXX'
              i_service_instance_name = 'EXTERNAL_API_XXX'
              i_authn_mode = if_a4c_cp_service=>service_specific ).
    
            DATA(lo_http_client) = cl_web_http_client_manager=>create_by_http_destination( i_destination = lo_destination ).
            DATA(lo_request) = lo_http_client->get_http_request( ).
            DATA(lo_response) = lo_http_client->execute( i_method = if_web_http_client=>get ).
              out->write( lo_response->get_text( ) ).
    
          CATCH cx_root INTO DATA(lx_exception).
            out->write( lx_exception->get_text( ) ).
        ENDTRY.
    
    
Log on to answer question
Step 7: Check, save, and activate
  1. Check your syntax (Ctrl+F2).
  2. Save (Ctrl+S) and activate (Ctrl+F3) your class.
Log on to answer question
Step 8: Run the class in the console

Run your class in the console (F9).

The output should look something like this:
Image depicting step-9-console

Log on to answer question
Step 9: Test yourself

Create the variable my_request using the DATA statement and the get_http_request method of the my_http_client object. Enter the correct text below:

How can you create a variable `my_request` using similar syntax to the code sample above. Choose the correct syntax, then choose **Submit Answer**:
×

Next Steps

Back to top